Mercurial > projects > doodle
annotate doodle/core/logging.d @ 58:c63719604adb
Beginnings of creating a rectangle...
author | "David Bryant <bagnose@gmail.com>" |
---|---|
date | Mon, 09 Aug 2010 21:43:24 +0930 |
parents | 0eaf39fda206 |
children | 523269b36711 ab745d8b10e5 |
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 { | |
55 mixin(defineEnum!("Severity", | |
56 "TRACE", "INFO", "MESSAGE", "WARNING", "ERROR", "FATAL")); | |
57 | |
51 | 58 string severityString(in Severity s) { |
38 | 59 switch (s) { |
60 case Severity.TRACE: | |
48 | 61 return modifierString(Modifier.DIM) ~ fgColorString(Color.CYAN); |
38 | 62 case Severity.INFO: |
48 | 63 return modifierString(Modifier.UNDERLINE) ~ fgColorString(Color.GREEN); |
38 | 64 case Severity.MESSAGE: |
48 | 65 return fgColorString(Color.YELLOW); |
38 | 66 case Severity.WARNING: |
48 | 67 return modifierString(Modifier.BRIGHT) ~ fgColorString(Color.MAGENTA); |
38 | 68 case Severity.ERROR: |
48 | 69 return modifierString(Modifier.BRIGHT) ~ fgColorString(Color.RED); |
38 | 70 case Severity.FATAL: |
48 | 71 return modifierString(Modifier.BRIGHT) ~ bgColorString(Color.RED) ~ fgColorString(Color.WHITE); |
40 | 72 default: |
73 assert(0); | |
38 | 74 } |
75 assert(0); | |
76 } | |
77 | |
78 void log(in Severity severity, in string message) { | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
51
diff
changeset
|
79 writeln(severityString(severity), message, modifierString(Modifier.RESET)); |
38 | 80 } |
81 | |
82 enum Modifier { | |
83 RESET = 0, | |
84 BRIGHT = 1, | |
85 DIM = 2, // does nothing in gnome-terminal | |
86 UNDERLINE = 3, // does nothing in gnome-terminal | |
87 BLINK = 5, // does nothing in gnome-terminal | |
88 REVERSE = 7, | |
89 HIDDEN = 8 | |
90 } | |
91 | |
92 enum Color { | |
93 BLACK = 0, | |
94 RED = 1, | |
95 GREEN = 2, | |
96 YELLOW = 3, | |
97 BLUE = 4, | |
98 MAGENTA = 5, | |
99 CYAN = 6, | |
100 WHITE = 7 | |
101 } | |
102 | |
51 | 103 string modifierString(in Modifier m) { return std.string.format("\033[%dm", 0 + m); } |
104 string fgColorString(in Color c) { return std.string.format("\033[%dm", 30 + c); } | |
105 string bgColorString(in Color c) { return std.string.format("\033[%dm", 40 + c); } | |
38 | 106 |
51 | 107 private string right(in string str, in int n) { |
38 | 108 int pos = str.length < n ? 0 : str.length - n; |
109 return str[pos..$]; | |
110 } | |
111 } |