1 /**
2  * Retrograde Engine
3  *
4  * Authors:
5  *  Mike Bierlee, m.bierlee@lostmoment.com
6  * Copyright: 2014-2021 Mike Bierlee
7  * License:
8  *  This software is licensed under the terms of the MIT license.
9  *  The full terms of the license can be found in the LICENSE.txt file.
10  */
11 
12 module retrograde.logging;
13 
14 import std.experimental.logger;
15 import std.stdio;
16 
17 class StdoutLogger : Logger {
18 
19     private File stdoutFile, stderrFile;
20 
21     this(LogLevel logLevel = LogLevel.info) {
22         super(logLevel);
23         stdoutFile = stdout;
24         stderrFile = stderr;
25     }
26 
27     protected override void writeLogMsg(ref LogEntry payload) {
28         File* outFile = &stdoutFile;
29         if (payload.logLevel == LogLevel.error) {
30             outFile = &stderrFile;
31         }
32 
33         auto logLevelLabel = "";
34         switch (payload.logLevel) {
35             case LogLevel.warning:
36                 logLevelLabel = "WARNING: ";
37                 break;
38 
39             case LogLevel.error:
40                 logLevelLabel = "ERROR: ";
41                 break;
42 
43             case LogLevel.critical:
44                 logLevelLabel = "CRITICAL: ";
45                 break;
46 
47             case LogLevel.fatal:
48                 logLevelLabel = "FATAL: ";
49                 break;
50 
51             default:
52                 break;
53         }
54 
55         outFile.writeln(logLevelLabel ~ payload.msg);
56         outFile.flush();
57     }
58 
59     public bool stdoutIsAvailable() {
60         try {
61             stdoutFile.writeln();
62             stdoutFile.flush();
63             return true;
64         } catch (Exception e) {
65             return false;
66         }
67     }
68 }