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