1 module libasync.internals.logging;
2 
3 import std.experimental.logger;
4 
5 nothrow:
6 // The below is adapted for nothrow from
7 // https://github.com/dlang/phobos/blob/master/std/experimental/logger/core.d
8 // and is thus under Boost license
9 
10 template defaultLogFunction(LogLevel ll)
11 {
12     void defaultLogFunction(int line = __LINE__, string file = __FILE__,
13         string funcName = __FUNCTION__,
14         string prettyFuncName = __PRETTY_FUNCTION__,
15         string moduleName = __MODULE__, A...)(lazy A args) @trusted
16         if ((args.length > 0 && !is(Unqual!(A[0]) : bool)) || args.length == 0)
17     {
18         static if (isLoggingActiveAt!ll && ll >= moduleLogLevel!moduleName)
19         {
20             try stdThreadLocalLog.memLogFunctions!(ll).logImpl!(line, file, funcName,
21                 prettyFuncName, moduleName)(args);
22             catch (Throwable e) {}
23         }
24     }
25 
26     void defaultLogFunction(int line = __LINE__, string file = __FILE__,
27         string funcName = __FUNCTION__,
28         string prettyFuncName = __PRETTY_FUNCTION__,
29         string moduleName = __MODULE__, A...)(lazy bool condition, lazy A args) @trusted
30     {
31         static if (isLoggingActiveAt!ll && ll >= moduleLogLevel!moduleName)
32         {
33             try stdThreadLocalLog.memLogFunctions!(ll).logImpl!(line, file, funcName,
34                 prettyFuncName, moduleName)(condition, args);
35             catch (Throwable e) {}
36         }
37     }
38 }
39 
40 alias trace = defaultLogFunction!(LogLevel.trace);
41 alias info = defaultLogFunction!(LogLevel.info);
42 alias warning = defaultLogFunction!(LogLevel.warning);
43 alias error = defaultLogFunction!(LogLevel.error);
44 alias critical = defaultLogFunction!(LogLevel.critical);
45 
46 template defaultLogFunctionf(LogLevel ll)
47 {
48     void defaultLogFunctionf(int line = __LINE__, string file = __FILE__,
49         string funcName = __FUNCTION__,
50         string prettyFuncName = __PRETTY_FUNCTION__,
51         string moduleName = __MODULE__, A...)(lazy string msg, lazy A args) @trusted
52     {
53         static if (isLoggingActiveAt!ll && ll >= moduleLogLevel!moduleName)
54         {
55             try stdThreadLocalLog.memLogFunctions!(ll).logImplf!(line, file, funcName,
56                 prettyFuncName, moduleName)(msg, args);
57             catch (Throwable e) {}
58         }
59     }
60 
61     void defaultLogFunctionf(int line = __LINE__, string file = __FILE__,
62         string funcName = __FUNCTION__,
63         string prettyFuncName = __PRETTY_FUNCTION__,
64         string moduleName = __MODULE__, A...)(lazy bool condition, lazy string msg, lazy A args) @trusted
65     {
66         static if (isLoggingActiveAt!ll && ll >= moduleLogLevel!moduleName)
67         {
68             try stdThreadLocalLog.memLogFunctions!(ll).logImplf!(line, file, funcName,
69                 prettyFuncName, moduleName)(condition, msg, args);
70             catch (Throwable e) {}
71         }
72     }
73 }
74 
75 alias tracef = defaultLogFunctionf!(LogLevel.trace);
76 alias infof = defaultLogFunctionf!(LogLevel.info);
77 alias warningf = defaultLogFunctionf!(LogLevel.warning);
78 alias errorf = defaultLogFunctionf!(LogLevel.error);
79 alias criticalf = defaultLogFunctionf!(LogLevel.critical);