]> Dogcows Code - chaz/yoink/blob - src/moof/log.hh
e6e1a80c980400475e4476e271448f5bfdf75e00
[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 };
54
55
56 /**
57 * Set the lowest-priority log message that will be outputted to the
58 * log. Any logging with a lower priority will be ignored.
59 * \param level The log level.
60 */
61 static void level(level level);
62
63 /**
64 * Get the current lowest-priority log level. If unchanged, the
65 * default level is info.
66 * \return The log level.
67 */
68 static enum level level();
69
70
71 /**
72 * Import log functions to a script.
73 * \param The script.
74 */
75 static void import(script& script);
76
77
78 /**
79 * Construct a log with a certain priority and prefix string.
80 * \param level The log level.
81 * \param prefix The string printed before each log message.
82 */
83 log(enum level level);
84
85
86 /**
87 * Output this to end a line. This is equivalent to std::endl except
88 * this will also reset terminal format attributes.
89 */
90 static struct endl_
91 {
92 } endl;
93
94
95 template <class A>
96 void operator () (const A& a)
97 {
98 *this << a << endl;
99 }
100
101 template <class A, class B>
102 void operator () (const A& a, const B& b)
103 {
104 *this << a << " " << b << endl;
105 }
106
107 template <class A, class B, class C>
108 void operator () (const A& a, const B& b, const C& c)
109 {
110 *this << a << " " << b << " " << c << endl;
111 }
112
113 template <class A, class B, class C, class D>
114 void operator () (const A& a, const B& b, const C& c, const D& d)
115 {
116 *this << a << " " << b << " " << c << " " << d << endl;
117 }
118
119 template <class A, class B, class C, class D, class E>
120 void operator () (const A& a,
121 const B& b,
122 const C& c,
123 const D& d,
124 const E& e)
125 {
126 *this << a << " "
127 << b << " "
128 << c << " "
129 << d << " "
130 << e << endl;
131 }
132
133
134 private:
135
136 template <class T> friend std::ostream& operator << (log&, const T&);
137 friend std::ostream& operator << (log&, endl_);
138
139 static enum level global_level_;
140
141 enum level level_;
142 std::string prefix_;
143 };
144
145
146 extern std::ostream& log;
147 extern std::ostream& null_log;
148
149 extern class log log_error;
150 extern class log log_warning;
151 extern class log log_info;
152
153
154 template <class T>
155 inline std::ostream& operator << (class log& log, const T& item)
156 {
157 if (log::global_level_ < log.level_) return null_log;
158 return moof::log << log.prefix_ << item;
159 }
160
161 std::ostream& operator << (class log& log, log::endl_ endl);
162 std::ostream& operator << (std::ostream& stream, log::endl_ endl);
163
164
165 } // namespace moof
166
167 #endif // _MOOF_LOG_HH_
168
This page took 0.035578 seconds and 3 git commands to generate.