]> Dogcows Code - chaz/yoink/blob - src/moof/log.cc
fixed documentation about where to find licenses
[chaz/yoink] / src / moof / log.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #if HAVE_INCLUDE_H
11 #include "config.h"
12 #endif
13
14 #include <fstream>
15
16 #if PLATFORM_POSIX
17 #include <termios.h>
18 #else
19 inline int isatty(int dummy) { return 0; }
20 #endif
21
22 #include "log.hh"
23 #include "script.hh"
24
25
26 namespace moof {
27
28
29 enum log::level log::global_level_ = log::info;
30
31
32 void log::level(enum level level)
33 {
34 global_level_ = level;
35 }
36
37 enum log::level log::level()
38 {
39 return global_level_;
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: ";
58 break;
59 case log::warning: prefix_ = "\033[30;103mwarning: ";
60 break;
61 case log::info: prefix_ = " info: ";
62 break;
63 case log::debug: prefix_ = "\033[2m debug: ";
64 case log::none: break;
65 }
66 }
67
68 std::ostream& operator << (class log& log, log::endl_ endl)
69 {
70 if (log::global_level_ < log.level_) return null_log;
71 if (isatty(1) == 0) return moof::log << std::endl;
72 return moof::log << "\033[0m" << std::endl;
73 }
74
75 std::ostream& operator << (std::ostream& stream, log::endl_ endl)
76 {
77 if (isatty(1) == 0) return stream << std::endl;
78 return stream << "\033[0m" << std::endl;
79 }
80
81 std::ostream& log(std::cout);
82
83 static std::ofstream null_log_;
84 std::ostream& null_log(null_log_);
85
86 // These objects are intentionally not deconstructed so that logging will
87 // still be available after this module has been cleaned up.
88 class log& log_error(*new class log(log::error));
89 class log& log_warning(*new class log(log::warning));
90 class log& log_info(*new class log(log::info));
91 class log& log_debug(*new class log(log::debug));
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 (*logs[level])(param);
107 ++param.index;
108 }
109
110 return 0;
111 }
112
113 void log::import(script& script)
114 {
115 script.import_function("LogError",
116 boost::bind(log_script, _1, log::error));
117 script.import_function("LogWarning",
118 boost::bind(log_script, _1, log::warning));
119 script.import_function("LogInfo",
120 boost::bind(log_script, _1, log::info));
121 script.import_function("print",
122 boost::bind(log_script, _1, log::info));
123 script.import_function("LogDebug",
124 boost::bind(log_script, _1, log::debug));
125 }
126
127
128 } // namespace moof
129
This page took 0.039986 seconds and 5 git commands to generate.