-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.d
102 lines (85 loc) · 2.02 KB
/
logging.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import tango.core.sync.Mutex;
import tango.io.Stdout;
import tango.text.convert.Format;
interface LogConsumer
{
void log(char[]);
void error(char[]);
void warn(char[]);
void info(char[]);
}
class Logger
{
LogConsumer[] consumers;
bool to_console = false;
Mutex console_lock;
this()
{
console_lock = new Mutex();
consumers.length = 0;
}
void register(LogConsumer c)
{
consumers.length = consumers.length + 1;
consumers[length-1] = c;
}
private
{
void _console_print(char[] fmt, ...)
{
_console_print(_arguments, _argptr, fmt);
}
void _console_print(TypeInfo[] _arguments, void* _argptr, char[] fmt)
{
synchronized (console_lock)
{
Stderr(Stderr.layout.convert(_arguments, _argptr, fmt)).newline;
}
}
}
void console(char[] fmt, ...)
{
if (to_console)
{
_console_print(_arguments, _argptr, fmt);
}
}
void log(char[] fmt, ...)
{
char[] message = Format.convert(_arguments, _argptr, fmt);
foreach(LogConsumer con; consumers)
{
con.log(message);
}
if (to_console)
_console_print("log: {}", message);
}
void error(char[] fmt, ...)
{
char[] message = Format.convert(_arguments, _argptr, fmt);
foreach(LogConsumer con; consumers)
{
con.error(message);
}
if (to_console)
_console_print("Error: {}", message);
}
void warn(char[] fmt, ...)
{
char[] message = Format.convert(_arguments, _argptr, fmt);
foreach(LogConsumer con; consumers)
{
con.warn(message);
}
if (to_console)
_console_print("Warning: {}", message);
}
void info(char[] fmt, ...)
{
char[] message = Format.convert(_arguments, _argptr, fmt);
foreach(LogConsumer con; consumers)
{
con.info(message);
}
}
}