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