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