1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.util.log;
23 
24 private import std.experimental.logger;
25 private import std.conv;
26 
27 private import dxx.util;
28 
29 final class MsgLog : SyncNotificationSource,NotificationListener {
30     struct LogNotification {
31         LogLevel logLevel;
32         int line;
33         string file;
34         string funcName;
35         string prettyFuncName;
36         string moduleName;
37         string msg;
38     }
39     __gshared shared(MsgLog) _MSGLOG;
40 
41     shared static this() {
42         sharedLog.trace("MsgLog static this");
43         if(_MSGLOG is null) {
44             _MSGLOG = new MsgLog;
45             _MSGLOG.addNotificationListener(_MSGLOG);
46         }
47     }
48 
49     override synchronized void handleNotification(void* p) {
50         debug(Notify) {
51             sharedLog.trace("MsgLog handleNotification");
52         }
53         //LogNotification* n = cast(LogNotification*)p;
54         //if(n !is null) {
55         //}
56     }
57 
58 
59     alias logger = resolveInjector!Logger;
60 
61 
62     static void addLogNotificationListener(T)(T t) {
63         _MSGLOG.addNotificationListener(t);
64     }
65     static void removeLogNotificationListener(T)(T t) {
66         _MSGLOG.removeNotificationListener(t);
67     }
68 
69     nothrow 
70     static void sendLogNotification(LogNotification n) {
71         _MSGLOG.send!(LogNotification)(&n);
72     }
73 
74     nothrow 
75     static void trace(int line = __LINE__, string file = __FILE__,
76             string funcName = __FUNCTION__,
77             string prettyFuncName = __PRETTY_FUNCTION__,
78             string moduleName = __MODULE__, A...)(
79                 lazy A args) {
80         try {
81             sendLogNotification(LogNotification(LogLevel.trace,line,file,funcName,prettyFuncName,moduleName,args.to!string));
82             logger.trace!(line,file,funcName,prettyFuncName,moduleName,A)(args);        
83         } catch (Exception e) {
84             //sharedLog.error(e);
85         }
86         
87     }
88     nothrow 
89     static void warning(int line = __LINE__, string file = __FILE__,
90             string funcName = __FUNCTION__,
91             string prettyFuncName = __PRETTY_FUNCTION__,
92             string moduleName = __MODULE__, A...)(
93                 lazy A args) {
94         try {
95             sendLogNotification(LogNotification(LogLevel.warning,line,file,funcName,prettyFuncName,moduleName,args.to!string));
96             logger.warning!(line,file,funcName,prettyFuncName,moduleName,A)(args);
97         } catch (Exception) {
98         }
99     }
100     nothrow 
101     static void error(int line = __LINE__, string file = __FILE__,
102             string funcName = __FUNCTION__,
103             string prettyFuncName = __PRETTY_FUNCTION__,
104             string moduleName = __MODULE__, A...)(
105                 lazy A args) {
106         try {
107             sendLogNotification(LogNotification(LogLevel.error,line,file,funcName,prettyFuncName,moduleName,args.to!string));
108             logger.error!(line,file,funcName,prettyFuncName,moduleName,A)(args);
109         } catch (Exception) {
110         }
111     }
112     nothrow 
113     static void info(int line = __LINE__, string file = __FILE__,
114             string funcName = __FUNCTION__,
115             string prettyFuncName = __PRETTY_FUNCTION__,
116             string moduleName = __MODULE__, A...)(
117                 lazy A args) {
118         try {
119             sendLogNotification(LogNotification(LogLevel.info,line,file,funcName,prettyFuncName,moduleName,args.to!string));
120             logger.info!(line,file,funcName,prettyFuncName,moduleName,A)(args);
121         } catch (Exception) {
122         }
123     }
124     static void fatal(int line = __LINE__, string file = __FILE__,
125             string funcName = __FUNCTION__,
126             string prettyFuncName = __PRETTY_FUNCTION__,
127             string moduleName = __MODULE__, A...)(
128                 lazy A args) {
129         try {
130             sendLogNotification(LogNotification(LogLevel.fatal,line,file,funcName,prettyFuncName,moduleName,args.to!string));
131             logger.fatal!(line,file,funcName,prettyFuncName,moduleName,A)(args);
132         } catch (Exception) {
133         }
134     }
135 
136 };