]> Dogcows Code - chaz/yoink/blob - src/moof/log.cc
mesh and other random adjustments
[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 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::none: break;
52 }
53 else
54 switch (level)
55 {
56 case log::error: prefix_ = "\033[101m error: "; break;
57 case log::warning: prefix_ = "\033[103mwarning: "; break;
58 case log::info: prefix_ = " info: "; break;
59 case log::none: break;
60 }
61 }
62
63 std::ostream& operator << (class log& log, log::endl_ endl)
64 {
65 if (log::global_level_ < log.level_) return null_log;
66 if (isatty(1) == 0) return moof::log << std::endl;
67 else return moof::log << "\033[0m" << std::endl;
68 }
69
70 std::ostream& operator << (std::ostream& stream, log::endl_ endl)
71 {
72 if (isatty(1) == 0) return stream << std::endl;
73 else return stream << "\033[0m" << std::endl;
74 }
75
76
77 std::ostream& log(std::cout);
78
79 static std::ofstream null_log_;
80 std::ostream& null_log(null_log_);
81
82 class log log_error( log::error);
83 class log log_warning(log::warning);
84 class log log_info( log::info);
85
86
87 static int log_script(script& script, enum log::level level)
88 {
89 static class log* logs[] = {0, &log_error, &log_warning, &log_info};
90
91 script::slot param = script[1];
92
93 while (!param.is_none())
94 {
95 (*logs[level])(param);
96 ++param.index;
97 }
98
99 return 0;
100 }
101
102 void log::import(script& script)
103 {
104 script.import_function("LogError",
105 boost::bind(log_script, _1, log::error));
106 script.import_function("LogWarning",
107 boost::bind(log_script, _1, log::warning));
108 script.import_function("LogInfo",
109 boost::bind(log_script, _1, log::info));
110 script.import_function("print",
111 boost::bind(log_script, _1, log::info));
112 }
113
114
115 } // namespace moof
116
This page took 0.034041 seconds and 4 git commands to generate.