comparison doodle/core/logging.d @ 38:452915ecd1f4

Basic logging functionality
author David Bryant <bagnose@gmail.com>
date Sun, 27 Sep 2009 22:51:03 +0930
parents
children 1f97022e5c6d
comparison
equal deleted inserted replaced
37:bc0035393a81 38:452915ecd1f4
1 module doodle.core.logging;
2
3 private {
4 import std.stdio;
5 import std.typecons;
6 }
7
8 public {
9 void trace(string file = __FILE__, int line = __LINE__)(in string message) {
10 log(Severity.TRACE, std.string.format("%s(%d): TRACE: %s", right(file, 20), line, message));
11 }
12 void info(string file = __FILE__, int line = __LINE__)(in string message) {
13 log(Severity.INFO, std.string.format("%s(%d): INFO: %s", right(file, 20), line, message));
14 }
15 void message(string file = __FILE__, int line = __LINE__)(in string message) {
16 log(Severity.MESSAGE, std.string.format("%s(%d): MESSAGE: %s", right(file, 20), line, message));
17 }
18 void warning(string file = __FILE__, int line = __LINE__)(in string message) {
19 log(Severity.WARNING, std.string.format("%s(%d): WARNING: %s", right(file, 20), line, message));
20 }
21 void error(string file = __FILE__, int line = __LINE__)(in string message) {
22 log(Severity.ERROR, std.string.format("%s(%d): ERROR: %s", right(file, 20), line, message));
23 }
24 void fatal(string file = __FILE__, int line = __LINE__)(in string message) {
25 log(Severity.FATAL, std.string.format("%s(%d): FATAL: %s", right(file, 20), line, message));
26 assert(0);
27 }
28 }
29
30 private {
31 mixin(defineEnum!("Severity",
32 "TRACE", "INFO", "MESSAGE", "WARNING", "ERROR", "FATAL"));
33
34 string mod_string(in Severity s) {
35 switch (s) {
36 case Severity.TRACE:
37 return modifier_string(Modifier.DIM) ~ fg_color_string(Color.CYAN);
38 case Severity.INFO:
39 return modifier_string(Modifier.UNDERLINE) ~ fg_color_string(Color.GREEN);
40 case Severity.MESSAGE:
41 return fg_color_string(Color.YELLOW);
42 case Severity.WARNING:
43 return modifier_string(Modifier.BRIGHT) ~ fg_color_string(Color.MAGENTA);
44 case Severity.ERROR:
45 return modifier_string(Modifier.BRIGHT) ~ fg_color_string(Color.RED);
46 case Severity.FATAL:
47 return modifier_string(Modifier.BRIGHT) ~ bg_color_string(Color.RED) ~ fg_color_string(Color.WHITE);
48 }
49 assert(0);
50 }
51
52 void log(in Severity severity, in string message) {
53 write(mod_string(severity));
54 write(message);
55 writeln(modifier_string(Modifier.RESET));
56 }
57
58 enum Modifier {
59 RESET = 0,
60 BRIGHT = 1,
61 DIM = 2, // does nothing in gnome-terminal
62 UNDERLINE = 3, // does nothing in gnome-terminal
63 BLINK = 5, // does nothing in gnome-terminal
64 REVERSE = 7,
65 HIDDEN = 8
66 }
67
68 enum Color {
69 BLACK = 0,
70 RED = 1,
71 GREEN = 2,
72 YELLOW = 3,
73 BLUE = 4,
74 MAGENTA = 5,
75 CYAN = 6,
76 WHITE = 7
77 }
78
79 string modifier_string(Modifier m) { return std.string.format("\033[%dm", 0 + m); }
80 string fg_color_string(Color c) { return std.string.format("\033[%dm", 30 + c); }
81 string bg_color_string(Color c) { return std.string.format("\033[%dm", 40 + c); }
82
83 private const(char)[] right(in char[] str, int n) {
84 int pos = str.length < n ? 0 : str.length - n;
85 return str[pos..$];
86 }
87 }