Mercurial > projects > doodle
annotate doodle/core/logging.d @ 101:523269b36711
Update to dmd 2.050
author | David Bryant <bagnose@gmail.com> |
---|---|
date | Thu, 28 Oct 2010 16:35:11 +1030 |
parents | c63719604adb |
children | 7abaf5c3959f |
rev | line source |
---|---|
38 | 1 module doodle.core.logging; |
2 | |
3 private { | |
4 import std.stdio; | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
5 import std.traits; |
38 | 6 } |
7 | |
8 public { | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
9 void trace(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
10 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
11 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
12 log(Severity.TRACE, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
13 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 14 } |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
15 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
16 void info(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
17 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
18 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
19 log(Severity.INFO, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
20 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 21 } |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
22 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
23 void message(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
24 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
25 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
26 log(Severity.MESSAGE, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
27 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 28 } |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
29 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
30 void warning(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
31 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
32 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
33 log(Severity.WARNING, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
34 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 35 } |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
36 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
37 void error(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
38 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
39 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
40 log(Severity.ERROR, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
41 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 42 } |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
43 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
44 void fatal(string file = __FILE__, int line = __LINE__, S...)(S args) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
45 static assert(S.length > 0); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
46 static assert(isSomeString!(S[0])); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
47 log(Severity.FATAL, |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
48 std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, std.string.format(args))); |
38 | 49 assert(0); |
50 } | |
51 } | |
52 | |
53 private { | |
101 | 54 enum Severity { TRACE, INFO, MESSAGE, WARNING, ERROR, FATAL } |
38 | 55 |
51 | 56 string severityString(in Severity s) { |
38 | 57 switch (s) { |
58 case Severity.TRACE: | |
48 | 59 return modifierString(Modifier.DIM) ~ fgColorString(Color.CYAN); |
38 | 60 case Severity.INFO: |
48 | 61 return modifierString(Modifier.UNDERLINE) ~ fgColorString(Color.GREEN); |
38 | 62 case Severity.MESSAGE: |
48 | 63 return fgColorString(Color.YELLOW); |
38 | 64 case Severity.WARNING: |
48 | 65 return modifierString(Modifier.BRIGHT) ~ fgColorString(Color.MAGENTA); |
38 | 66 case Severity.ERROR: |
48 | 67 return modifierString(Modifier.BRIGHT) ~ fgColorString(Color.RED); |
38 | 68 case Severity.FATAL: |
48 | 69 return modifierString(Modifier.BRIGHT) ~ bgColorString(Color.RED) ~ fgColorString(Color.WHITE); |
40 | 70 default: |
71 assert(0); | |
38 | 72 } |
73 assert(0); | |
74 } | |
75 | |
76 void log(in Severity severity, in string message) { | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
77 writeln(severityString(severity), message, modifierString(Modifier.RESET)); |
38 | 78 } |
79 | |
80 enum Modifier { | |
81 RESET = 0, | |
82 BRIGHT = 1, | |
83 DIM = 2, // does nothing in gnome-terminal | |
84 UNDERLINE = 3, // does nothing in gnome-terminal | |
85 BLINK = 5, // does nothing in gnome-terminal | |
86 REVERSE = 7, | |
87 HIDDEN = 8 | |
88 } | |
89 | |
90 enum Color { | |
91 BLACK = 0, | |
92 RED = 1, | |
93 GREEN = 2, | |
94 YELLOW = 3, | |
95 BLUE = 4, | |
96 MAGENTA = 5, | |
97 CYAN = 6, | |
98 WHITE = 7 | |
99 } | |
100 | |
51 | 101 string modifierString(in Modifier m) { return std.string.format("\033[%dm", 0 + m); } |
102 string fgColorString(in Color c) { return std.string.format("\033[%dm", 30 + c); } | |
103 string bgColorString(in Color c) { return std.string.format("\033[%dm", 40 + c); } | |
38 | 104 |
51 | 105 private string right(in string str, in int n) { |
38 | 106 int pos = str.length < n ? 0 : str.length - n; |
107 return str[pos..$]; | |
108 } | |
109 } |