]> Dogcows Code - chaz/yoink/blob - src/moof/log.hh
remove some unused stlplus modules
[chaz/yoink] / src / moof / log.hh
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #ifndef _MOOF_LOG_HH_
11 #define _MOOF_LOG_HH_
12
13 /**
14 * \file log.hh
15 * Functions related to logging the process. The logging functions are
16 * log_error(), log_warning(), and log_info(), listed from most critical to
17 * least critical.
18 */
19
20 #include <cstdlib> // exit
21 #include <iostream>
22 #include <string>
23
24
25 namespace moof {
26
27
28 // forward declarations
29 class script;
30
31 /**
32 * A class for handling a log priority. There are two ways to log
33 * messages: by treating a log object as a function whose parameters are
34 * printed with default spacing, or by treating a log object as an output
35 * stream. Either way, any object can be printed to the log as long as
36 * there is an override for the ostream insertion operator.
37 */
38 class log
39 {
40 public:
41
42 /**
43 * A type for the level or priority of a log message.
44 */
45 enum level
46 {
47 none = 0, ///< Disable all logging.
48 error = 1, ///< Log only errors.
49 warning = 2, ///< Log warnings and errors.
50 info = 3, ///< Log everything.
51 debug = 4, ///< Log absolutely everything.
52 };
53
54 /**
55 * Set the lowest-priority log message that will be outputted to the
56 * log. Any logging with a lower priority will be ignored.
57 * \param level The log level.
58 */
59 static void level(level level);
60
61 /**
62 * Get the current lowest-priority log level. If unchanged, the
63 * default level is info.
64 * \return The log level.
65 */
66 static enum level level();
67
68 /**
69 * Import log functions to a script.
70 * \param The script.
71 */
72 static void import(script& script);
73
74 /**
75 * Construct a log with a certain priority and prefix string.
76 * \param level The log level.
77 * \param prefix The string printed before each log message.
78 */
79 log(enum level level);
80
81 /**
82 * Output this to end a line. This is equivalent to std::endl except
83 * this will also reset terminal format attributes.
84 */
85 static struct endl_
86 {
87 } endl;
88
89 template <class A>
90 void operator () (const A& a)
91 {
92 *this << a << endl;
93 }
94
95 template <class A, class B>
96 void operator () (const A& a, const B& b)
97 {
98 *this << a << " " << b << endl;
99 }
100
101 template <class A, class B, class C>
102 void operator () (const A& a, const B& b, const C& c)
103 {
104 *this << a << " " << b << " " << c << endl;
105 }
106
107 template <class A, class B, class C, class D>
108 void operator () (const A& a, const B& b, const C& c, const D& d)
109 {
110 *this << a << " " << b << " " << c << " " << d << endl;
111 }
112
113 template <class A, class B, class C, class D, class E>
114 void operator () (const A& a,
115 const B& b,
116 const C& c,
117 const D& d,
118 const E& e)
119 {
120 *this << a << " " << b << " " << c << " " << d << " " << e <<
121 endl;
122 }
123
124 private:
125
126 template <class T> friend std::ostream& operator << (log&, const T&);
127 friend std::ostream& operator << (log&, endl_);
128
129 static enum level global_level_;
130
131 enum level level_;
132 std::string prefix_;
133 };
134
135 extern std::ostream& log;
136 extern std::ostream& null_log;
137
138 extern class log& log_error;
139 extern class log& log_warning;
140 extern class log& log_info;
141 extern class log& log_debug;
142
143
144 template <class T>
145 inline std::ostream& operator << (class log& log, const T& item)
146 {
147 if (log::global_level_ < log.level_) return null_log;
148 return moof::log << log.prefix_ << item;
149 }
150
151 std::ostream& operator << (class log& log, log::endl_ endl);
152 std::ostream& operator << (std::ostream& stream, log::endl_ endl);
153
154
155 } // namespace moof
156
157 #endif // _MOOF_LOG_HH_
158
This page took 0.036281 seconds and 4 git commands to generate.