]> Dogcows Code - chaz/yoink/blob - src/Moof/Log.hh
reformatting
[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 an logError() and exits if
28 * 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 class Log
47 {
48 public:
49
50 enum Level
51 {
52 NONE = 0, ///< Disable all logging.
53 ERRORR = 1, ///< Log only errors.
54 WARNING = 2, ///< Log warnings and errors.
55 INFO = 3, ///< Log everything.
56 };
57
58 static void setLevel(Level level);
59 static Level getLevel();
60
61
62 Log(Level level, const char* type) :
63 mLevel(level),
64 mType(type) /* only pass literal strings */ {}
65
66
67 template <typename T>
68 void operator () (const T& item)
69 {
70 *this << item << std::endl;
71 }
72
73 private:
74
75 template <typename T> friend std::ostream& operator << (Log&, const T&);
76
77 static Level gLevel;
78
79 Level mLevel;
80 const char* mType;
81 };
82
83
84 extern std::ostream& log;
85 extern std::ostream& nullLog;
86
87 extern Log logError;
88 extern Log logWarning;
89 extern Log logInfo;
90
91
92 template <typename T>
93 inline std::ostream& operator << (Log& logObj, const T& item)
94 {
95 if (Log::gLevel < logObj.mLevel) return nullLog;
96 return log << logObj.mType << item;
97 }
98
99
100 class Script;
101 void importLogFunctions(Script& script);
102
103
104 } // namespace Mf
105
106 #endif // _MOOF_LOG_H_
107
This page took 0.037804 seconds and 4 git commands to generate.