1 module geario.logging.config;
2 
3 @safe:
4 public:
5 
6 struct LoggerConfig
7 {
8     string filename      = "./log.log";
9     string level         = "trace";
10     string appenderType = "ConsoleAppender";
11     string rollingType   = "SizeBasedRollover";
12     uint   maxSize       = 1024 * 1024 * 20;
13     uint   maxHistory    = 1024;
14 }
15 
16 /*
17  * Make class member with getter
18  */
19 template addVal(T, string name, string specificator)
20 {
21     const char[] member = "private " ~ T.stringof ~ " _" ~ name ~"; ";
22     const char[] getter = "@property nothrow pure " ~ specificator ~ " " ~ T.stringof ~ " " ~ name ~ "() { return _" ~ name ~ "; }";
23     const char[] addVal = member ~ getter;
24 }
25 
26 /**
27  * Make class member with getter and setter
28  *
29  */
30 template addVar(T, string name, string getterSpecificator, string setterSpecificator)
31 {
32     const char[] setter = "@property nothrow pure " ~ setterSpecificator ~ " void " ~ name ~ "(" ~ T.stringof ~ " var" ~ ") { _" ~ name ~ " = var; }";
33     const char[] addVar = addVal!(T, name, getterSpecificator) ~ setter;
34 }
35 
36 /**
37  * Logger exception
38  */
39 class LoggerException : Exception
40 {
41     @safe pure nothrow this(string exString)
42     {
43         super(exString);
44     }
45 }
46 
47 /**
48  * Log creation exception
49  */
50 class LoggerCreateException : LoggerException
51 {
52     @safe pure nothrow this(string exString)
53     {
54         super(exString);
55     }
56 }
57 
58 /*
59  * Level type
60  */
61 enum Level:int
62 {
63     Trace = 1,
64     Info = 2,
65     Warn = 3,
66     Error = 4,
67     Fatal = 5
68 }
69 
70 /*
71  * Convert level from string type to Level
72  */
73 Level toLevel(string str)
74 {
75     Level l;
76 
77     switch (str)
78     {
79         case "trace":
80             l = Level.Trace;
81             break;
82         case "info":
83             l = Level.Info;
84             break;
85         case "warn":
86             l = Level.Warn;
87             break;
88         case "error":
89             l = Level.Error;
90             break;
91         case "fatal":
92             l = Level.Fatal;
93             break;
94         default:
95             throw new LoggerCreateException("Error log level value: " ~ str);
96     }
97 
98     return l;
99 }
100 
101 /*
102  * Convert level from Level type to string
103  */
104 @safe
105 string levelToViewString(Level level)
106 {
107     string l;
108 
109     final switch (level)
110     {
111         case Level.Trace:
112             l = "TRACE";
113             break;
114         case Level.Info:
115             l = "INFO";
116             break;
117         case Level.Warn:
118             l = "WARN";
119             break;
120         case Level.Error:
121             l = "ERROR";
122             break;
123         case Level.Fatal:
124             l = "FATAL";
125             break;
126     }
127 
128     return l;
129 }
130