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