1 module grpc.logger; 2 import interop.headers; 3 import grpc.service.queue; 4 import std.datetime; 5 import core.thread; 6 7 enum Verbosity { 8 Debug = 0, 9 Info = 1, 10 Error = 2 11 }; 12 13 import std.array, std.format; 14 static shared Logger gLogger; 15 16 void INFO(string format, string file = __MODULE__, int line = __LINE__, A...)(lazy A args) @trusted { 17 if (gLogger.__minVerbosity <= Verbosity.Info) { 18 auto msg = appender!string; 19 formattedWrite(msg, format, args); 20 gLogger.log(Verbosity.Info, msg.data, file, line); 21 } 22 } 23 24 void DEBUG(string format, string file = __MODULE__, int line = __LINE__, A...)(lazy A args) @trusted { 25 debug { 26 if (gLogger.__minVerbosity <= Verbosity.Debug) { 27 auto msg = appender!string; 28 formattedWrite(msg, format, args); 29 gLogger.log(Verbosity.Debug, msg.data, file, line); 30 } 31 } 32 } 33 34 void ERROR(string format, string file = __MODULE__, int line = __LINE__, A...)(lazy A args) @trusted { 35 if (gLogger.__minVerbosity <= Verbosity.Error) { 36 auto msg = appender!string; 37 formattedWrite(msg, format, args); 38 gLogger.log(Verbosity.Error, msg.data, file, line); 39 } 40 } 41 42 class Logger { 43 private { 44 struct LogEvent { 45 SysTime time; 46 Verbosity v; 47 string message; 48 string source; 49 } 50 51 string _infoPath; 52 string _warningPath; 53 string _errorPath; 54 string _debugPath; 55 56 Verbosity __minVerbosity; 57 58 } 59 60 @property Verbosity minVerbosity() shared { 61 return __minVerbosity; 62 } 63 64 @property Verbosity minVerbosity(Verbosity _min) shared { 65 gpr_log_verbosity_init(); 66 gpr_set_log_verbosity(cast(gpr_log_severity)_min); 67 __minVerbosity = _min; 68 69 return _min; 70 } 71 72 73 74 void info(string message, string file = __MODULE__, int line = __LINE__) shared { 75 log(Verbosity.Info, message, file, line); 76 } 77 78 void debug_(string message, string file = __MODULE__, int line = __LINE__) shared{ 79 log(Verbosity.Debug, message, file, line); 80 } 81 82 void error(string message, string file = __MODULE__, int line = __LINE__) shared { 83 log(Verbosity.Error, message, file, line); 84 } 85 86 void log(Verbosity v, string message, string file = __MODULE__, int line = __LINE__) shared { 87 import std.string : toStringz; 88 const(char)* msg = message.toStringz; 89 const(char)* f = file.toStringz; 90 gpr_log_message(f, line, cast(gpr_log_severity)v, msg); 91 } 92 93 this(Verbosity _minVerbosity = Verbosity.Info, string info = "", string warning = "", string error = "", string debug_ = "") shared { 94 minVerbosity = _minVerbosity; 95 _infoPath = info; 96 _warningPath = warning; 97 _errorPath = error; 98 _debugPath = debug_; 99 } 100 101 shared static this() { 102 gLogger = new shared(Logger)(); 103 import core.exception; 104 core.exception.assertHandler = &assertHandler; 105 } 106 107 shared static ~this() { 108 destroy(gLogger); 109 } 110 } 111 112 import core.stdc.stdlib : abort; 113 import core.thread; 114 import core.time; 115 116 void assertHandler(string file, ulong line, string message) nothrow { 117 try { 118 ERROR!"ASSERT: %s at %s:%d"(message, file, line); 119 abort(); 120 } catch(Exception e) { 121 122 } 123 } 124