]> Dogcows Code - chaz/yoink/blob - src/moof/log.hh
the massive refactoring effort
[chaz/yoink] / src / moof / log.hh
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 #ifndef _MOOF_LOG_HH_
13 #define _MOOF_LOG_HH_
14
15 /**
16 * \file log.hh
17 * Functions related to logging the process.
18 * The logging functions are log_error(), log_warning(), and log_info(),
19 * listed from most critical to least critical.
20 */
21
22 #include <cstdlib> // exit
23 #include <iostream>
24
25
26 #undef ASSERT
27 #if NDEBUG
28 #define ASSERT(X)
29 #else
30 /**
31 * Macro which tests an assertion and issues a log_error() and exits if the
32 * assertion is false.
33 * \param X test to perform
34 */
35 #define ASSERT(X) if (!(X)) moof::log_error \
36 << "false assertion at " << __FILE__ << ":" << __LINE__ << ", " \
37 << #X, exit(1)
38 #endif
39
40
41 namespace moof {
42
43
44 class script;
45
46
47 /**
48 * A class for handling a log priority. There are two ways to log
49 * messages: by treating a log object as a function whose parameters are
50 * printed with default spacing, or by treating a log object as an output
51 * stream. Either way, any object can be printed to the log as long as
52 * there is an override for the ostream insertion operator.
53 */
54 class log
55 {
56 public:
57
58 /**
59 * A type for the level or priority of a log message.
60 */
61 enum level
62 {
63 none = 0, ///< Disable all logging.
64 error = 1, ///< Log only errors.
65 warning = 2, ///< Log warnings and errors.
66 info = 3, ///< Log everything.
67 };
68
69
70 /**
71 * Set the lowest-priority log message that will be outputted to the
72 * log. Any logging with a lower priority will be ignored.
73 * \param level The log level.
74 */
75 static void level(level level);
76
77 /**
78 * Get the current lowest-priority log level. If unchanged, the
79 * default level is info.
80 * \return The log level.
81 */
82 static enum level level();
83
84
85 /**
86 * Import log functions to a script.
87 * \param The script.
88 */
89 static void import(script& script);
90
91
92 /**
93 * Construct a log with a certain priority and prefix string.
94 * \param level The log level.
95 * \param prefix The string printed before each log message.
96 */
97 log(enum level level, const char* prefix) :
98 level_(level),
99 prefix_(prefix) /* only pass literal strings */ {}
100
101
102 template <class A>
103 void operator () (const A& a)
104 {
105 *this << a << std::endl;
106 }
107
108 template <class A, class B>
109 void operator () (const A& a, const B& b)
110 {
111 *this << a << " " << b << std::endl;
112 }
113
114 template <class A, class B, class C>
115 void operator () (const A& a, const B& b, const C& c)
116 {
117 *this << a << " " << b << " " << c << std::endl;
118 }
119
120 template <class A, class B, class C, class D>
121 void operator () (const A& a, const B& b, const C& c, const D& d)
122 {
123 *this << a << " " << b << " " << c << " " << d << std::endl;
124 }
125
126 template <class A, class B, class C, class D, class E>
127 void operator () (const A& a,
128 const B& b,
129 const C& c,
130 const D& d,
131 const E& e)
132 {
133 *this << a << " "
134 << b << " "
135 << c << " "
136 << d << " "
137 << e << std::endl;
138 }
139
140
141 private:
142
143 template <class T> friend std::ostream& operator << (log&, const T&);
144
145 static enum level gLevel;
146
147 enum level level_;
148 const char* prefix_;
149 };
150
151
152 extern std::ostream& log;
153 extern std::ostream& null_log;
154
155 extern class log log_error;
156 extern class log log_warning;
157 extern class log log_info;
158
159
160 template <class T>
161 inline std::ostream& operator << (class log& log, const T& item)
162 {
163 if (log::gLevel < log.level_) return null_log;
164 return moof::log << log.prefix_ << item;
165 }
166
167
168 } // namespace moof
169
170 #endif // _MOOF_LOG_HH_
171
This page took 0.035348 seconds and 4 git commands to generate.