1 /** 2 * Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved 3 * Authors: $(Gabriel Gheorghe) 4 * License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007) 5 * Source 6 * Documentation: 7 * Coverage: 8 */ 9 // TODO: Add current platform on log message. 10 module liberty.core.logger; 11 import liberty.core.utils : Singleton, IService; 12 /// All types of log that you can use when logging a message. 13 enum LogType : ubyte { 14 /// Used to log an information message. 15 Info = 0x00, 16 /// Used to log a warning message. 17 Warning = 0x01, 18 /// Used to log an error message. 19 Error = 0x02, 20 /// Used to log an exception message. 21 Exception = 0x03, 22 /// Used to log a debug message. Only works in debug mode. 23 Debug = 0x04, 24 /// Used to log a todo message. 25 Todo = 0x05 26 } 27 /// 28 final class Logger : Singleton!Logger, IService { 29 private bool _serviceRunning; 30 /// Start Logger service. 31 void startService() pure nothrow @safe @nogc { 32 _serviceRunning = true; 33 } 34 /// Stop Logger service. 35 void stopService() pure nothrow @safe @nogc { 36 _serviceRunning = false; 37 } 38 /// Restart Logger service. 39 void restartService() pure nothrow @safe @nogc { 40 stopService(); 41 startService(); 42 } 43 /// Returns true if Logger service is running. 44 bool isServiceRunning() pure nothrow const @safe @nogc { 45 return _serviceRunning; 46 } 47 /// Log an information message. It starts with the current time + " -> LOG_INFO: " 48 void info(string message) @safe { 49 log(LogType.Info, message); 50 } 51 /// Log a warning message. It starts with the current time + " -> LOG_WARNING: " 52 void warning(string message) @safe { 53 log(LogType.Warning, message); 54 } 55 /// Log an error message. It starts with the current time + " -> LOG_ERROR: " 56 void error(string message) @safe { 57 log(LogType.Error, message); 58 } 59 /// Log an exception message. It starts with the current time + " -> LOG_EXCEPTION: " 60 void exception(string message) @safe { 61 log(LogType.Exception, message); 62 } 63 /// Log a debug message. Only works in debug mode. It starts with the current time + " -> LOG_DEBUG: " 64 void console(string message) @safe { 65 log(LogType.Debug, message); 66 } 67 /// Log a todo message. It starts with the current time + " -> LOG_TODO: " 68 void todo(string message) @safe { 69 log(LogType.Todo, message); 70 } 71 private void log(LogType type, string message) @safe { 72 import std.stdio : writeln, File; 73 import std.datetime.systime : SysTime, Clock; 74 SysTime st = Clock.currTime(); 75 auto file = File("logs.txt", "a"); 76 scope (exit) file.close(); 77 synchronized { 78 if (_serviceRunning) { 79 final switch (type) with (LogType) { 80 case Info: 81 file.writeln(st.toISOExtString() ~ " -> LOG_INFO: " ~ message); 82 break; 83 case Warning: 84 file.writeln(st.toISOExtString() ~ " -> LOG_WARNING: " ~ message); 85 break; 86 case Error: 87 file.writeln(st.toISOExtString() ~ " -> LOG_ERROR: " ~ message); 88 break; 89 case Exception: 90 file.writeln(st.toISOExtString() ~ " -> LOG_EXCEPTION: " ~ message); 91 break; 92 case Debug: 93 debug writeln(st.toISOExtString() ~ " -> LOG_DEBUG: " ~ message); 94 debug file.writeln(st.toISOExtString() ~ " -> LOG_DEBUG: " ~ message); 95 break; 96 case Todo: 97 file.writeln(st.toISOExtString() ~ " -> LOG_TODO: " ~ message); 98 break; 99 } 100 } 101 } 102 } 103 } 104 /// 105 @safe unittest { 106 Logger.startService(); 107 scope (exit) Logger.stopService(); 108 Logger.console("Test message!"); 109 Logger.info("Info test message!"); 110 Logger.warning("Warning test message!"); 111 Logger.error("Error test message!"); 112 try { 113 immutable int x = 5; 114 if (x == 5) { 115 throw new Exception("x cannot be 5!"); 116 } 117 } catch (Exception e) { 118 Logger.exception("Exception test message!"); 119 } 120 Logger.todo("Todo test message!"); 121 }