]> Dogcows Code - chaz/yoink/blob - src/moof/log.cc
testing improved runloop scheduling
[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 class log log_error(log::error);
85 class log log_warning(log::warning);
86 class log log_info(log::info);
87 class log log_debug(log::debug);
88
89
90 static int log_script(script& script, enum log::level level)
91 {
92 static class log* logs[] = {0, &log_error, &log_warning, &log_info, &log_debug};
93
94 script::slot param = script[1];
95
96 while (!param.is_none())
97 {
98 (*logs[level])(param);
99 ++param.index;
100 }
101
102 return 0;
103 }
104
105 void log::import(script& script)
106 {
107 script.import_function("LogError",
108 boost::bind(log_script, _1, log::error));
109 script.import_function("LogWarning",
110 boost::bind(log_script, _1, log::warning));
111 script.import_function("LogInfo",
112 boost::bind(log_script, _1, log::info));
113 script.import_function("print",
114 boost::bind(log_script, _1, log::info));
115 script.import_function("LogDebug",
116 boost::bind(log_script, _1, log::debug));
117 }
118
119
120 } // namespace moof
121
This page took 0.033834 seconds and 4 git commands to generate.