]> Dogcows Code - chaz/yoink/blob - src/moof/log.cc
build system enhancements
[chaz/yoink] / src / moof / log.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <fstream>
13
14 #if !defined(__WIN32)
15 #include <termios.h>
16 #else
17 inline int isatty(int dummy) { return 0; }
18 #endif
19
20
21 #include "log.hh"
22 #include "script.hh"
23
24
25 namespace moof {
26
27
28 enum log::level log::global_level_ = log::info;
29
30
31 void log::level(enum level level)
32 {
33 global_level_ = level;
34 }
35
36 enum log::level log::level()
37 {
38 return global_level_;
39 }
40
41
42 log::log(enum level level) :
43 level_(level)
44 {
45 if (isatty(1) == 0)
46 switch (level)
47 {
48 case log::error: prefix_ = " error: "; break;
49 case log::warning: prefix_ = "warning: "; break;
50 case log::info: prefix_ = " info: "; break;
51 case log::debug: prefix_ = " debug: "; break;
52 case log::none: break;
53 }
54 else
55 switch (level)
56 {
57 case log::error: prefix_ = "\033[30;101m error: "; break;
58 case log::warning: prefix_ = "\033[30;103mwarning: "; break;
59 case log::info: prefix_ = " info: "; break;
60 case log::debug: prefix_ = "\033[2m debug: "; break;
61 case log::none: break;
62 }
63 }
64
65 std::ostream& operator << (class log& log, log::endl_ endl)
66 {
67 if (log::global_level_ < log.level_) return null_log;
68 if (isatty(1) == 0) return moof::log << std::endl;
69 else return moof::log << "\033[0m" << std::endl;
70 }
71
72 std::ostream& operator << (std::ostream& stream, log::endl_ endl)
73 {
74 if (isatty(1) == 0) return stream << std::endl;
75 else return stream << "\033[0m" << std::endl;
76 }
77
78
79 std::ostream& log(std::cout);
80
81 static std::ofstream null_log_;
82 std::ostream& null_log(null_log_);
83
84
85 // These objects are intentionally not deconstructed so that logging will
86 // still be available after this module has been cleaned up.
87 class log& log_error(*new class log(log::error));
88 class log& log_warning(*new class log(log::warning));
89 class log& log_info(*new class log(log::info));
90 class log& log_debug(*new class log(log::debug));
91
92
93 static int log_script(script& script, enum log::level level)
94 {
95 static class log* logs[] = {
96 0,
97 &log_error,
98 &log_warning,
99 &log_info,
100 &log_debug
101 };
102
103 script::slot param = script[1];
104
105 while (!param.is_none())
106 {
107 (*logs[level])(param);
108 ++param.index;
109 }
110
111 return 0;
112 }
113
114 void log::import(script& script)
115 {
116 script.import_function("LogError",
117 boost::bind(log_script, _1, log::error));
118 script.import_function("LogWarning",
119 boost::bind(log_script, _1, log::warning));
120 script.import_function("LogInfo",
121 boost::bind(log_script, _1, log::info));
122 script.import_function("print",
123 boost::bind(log_script, _1, log::info));
124 script.import_function("LogDebug",
125 boost::bind(log_script, _1, log::debug));
126 }
127
128
129 } // namespace moof
130
This page took 0.034445 seconds and 4 git commands to generate.