1 #ifndef LOGGER_HH |
1 #ifndef LOGGER_HH |
2 #define LOGGER_HH |
2 #define LOGGER_HH |
3 |
3 |
4 #include <ClanLib/network.h> |
4 #include "Types.hh" |
5 |
5 |
6 #include <iostream> |
6 #include <iostream> |
7 |
7 |
8 /** |
8 /** |
9 * Message severity, WARN and above should go to stderr, INFO and DEBUG to stdout |
9 * Utility class used for logging, use Engine::log() to construct these. Implements the same streaming behaviour as |
|
10 * std::ostream, but the construtor prints out a header, and the destructor a newline. |
10 * |
11 * |
11 * @see Engine::log |
12 * Useage example: |
12 */ |
13 * |
13 enum LogLevel { |
14 * \code |
14 FATAL, |
15 * Engine::log(INFO, "foo.bar") << "value of quux=" << quux; |
15 ERROR, |
16 * |
16 WARN, |
17 * \endcode |
17 INFO, |
18 * |
18 DEBUG, |
19 * This will result in output like the following to stdout: |
19 }; |
20 * |
20 |
21 * \verbatim |
21 /** |
22 * INFO [foo.bar] value of quux=5 |
22 * Utility class used for logging, use Engine::log to construct these |
23 * \endverbatim |
23 * |
24 * |
24 * @see Engine::log |
25 * @see Engine::log |
25 */ |
26 */ |
26 class Logger { |
27 class Logger { |
27 private: |
28 private: |
|
29 /** |
|
30 * The stream we output to |
|
31 */ |
28 std::ostream &stream; |
32 std::ostream &stream; |
29 enum LogLevel level; |
33 |
30 const char *module; |
34 /** |
|
35 * Do we even care? |
|
36 */ |
|
37 bool show; |
31 |
38 |
32 public: |
39 public: |
33 Logger (std::ostream &stream, enum LogLevel level, const char *module); |
40 /** |
34 |
41 * If the given level is smaller than the given maximum level, output the header. Otherwise, act as a |
35 template <typename T> Logger& operator<< (const T val) { |
42 * no-op. |
36 #ifndef NDEBUG |
43 */ |
37 stream << val; |
44 Logger (std::ostream &stream, LogLevel level, const char *module, LogLevel max_level); |
38 #else |
45 |
39 (void) val; |
46 /** |
40 #endif |
47 * Stream the given value to our output stream, just like with std::ostream, unless we're acting as a no-op. |
41 |
48 */ |
42 return *this; |
49 template <typename T> Logger& operator<< (const T val); |
43 } |
50 |
44 |
51 /** |
|
52 * Output the final newline |
|
53 */ |
45 ~Logger (void); |
54 ~Logger (void); |
46 }; |
55 }; |
47 |
56 |
48 std::ostream& operator<< (std::ostream &s, CL_NetComputer &c); |
57 /* |
49 std::ostream& operator<< (std::ostream &s, CL_NetObject_Server &obj); |
58 * Logger template method definition |
50 std::ostream& operator<< (std::ostream &s, CL_NetObject_Client &obj); |
59 */ |
|
60 template <typename T> |
|
61 inline Logger& Logger::operator<< (const T val) { |
|
62 if (show) |
|
63 stream << val; |
|
64 |
|
65 // chaining |
|
66 return *this; |
|
67 } |
51 |
68 |
52 #endif /* LOGGER_HH */ |
69 #endif /* LOGGER_HH */ |