1 module des.util.logsys.base;
2 
3 import std.conv : to;
4 import std.datetime : Clock;
5 import std..string : toUpper, format;
6 
7 import des.util.logsys.output;
8 import des.util.logsys.rule;
9 
10 ///
11 class LogSysException : Exception { this( string msg ) { super(msg); } }
12 
13 ///
14 enum LogLevel
15 {
16     OFF,   /// no output
17     FATAL, /// not recoverible error
18     ERROR, /// recoverible error (exceptions)
19     WARN,  /// warning (only if something wrong but not critical)
20     INFO,  /// info message (one-call functions messages)
21     DEBUG, /// debug message (detalied one-call functions messages)
22     TRACE  /// trace message (idle, foreach, etc)
23 };
24 
25 ///
26 struct LogMessage
27 {
28     string emitter; /// name of log message emmiter
29     ulong ts; /// timestamp
30 
31     /// log message level (without LogLevel.OFF)
32     enum Level : LogLevel 
33     {
34         FATAL = LogLevel.FATAL, ///
35         ERROR, ///
36         WARN, ///
37         INFO, ///
38         DEBUG, ///
39         TRACE ///
40     };
41 
42     Level level; /// log level
43     string message; ///
44 
45     ///
46     @disable this();
47 
48     ///
49     this( string emitter, ulong ts, Level level, string message ) pure nothrow @safe
50     {
51         this.emitter = emitter;
52         this.ts = ts;
53         this.level = level;
54         this.message = message;
55     }
56 }
57 
58 /++
59  nothrow format function, if first is string it try call string.format,
60  if it's failed return all args converted to string
61  +/
62 string ntFormat(Args...)( Args args ) nothrow
63 {
64     try
65     {
66         static if( is( Args[0] == string ) )
67         { try return format(args); catch{} }
68 
69         string res;
70         foreach( arg; args )
71             res ~= to!string(arg);
72         return res;
73     }
74     catch( Exception e )
75         return "[MESSAGE CTOR EXCEPTION]: " ~ e.msg;
76 }
77 
78 ///
79 LogLevel toLogLevel( string s ) { return to!LogLevel( s.toUpper ); }
80 
81 /++
82     Returns:
83     format( "[%016.9f][%5s][%s]: %s", lm.ts / 1e9f, lm.level, lm.emitter, lm.message );
84  +/
85 string defaultFormatLogMessage( in LogMessage lm )
86 {
87     return format( "[%016.9f][%5s][%s]: %s",
88                     lm.ts / 1e9f, lm.level, lm.emitter, lm.message );
89 }
90 
91 ///
92 static shared LogOutputHandler logoutput;
93 
94 ///
95 static shared Rule logrule;
96 
97 package:
98 
99 ulong __ts() nothrow @property
100 {
101     try return Clock.currAppTick().length;
102     catch(Exception e) return 0;
103 }
104 
105 string checkReservedName( string name ) nothrow
106 {
107     if( name == "debug" ) return "Debug";
108     return name;
109 }