diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-06-26 05:10:58 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-06-26 05:10:58 +0000 |
commit | aa82dc64b397b6ca0d336d91638d4f4b849e3667 (patch) | |
tree | c17f285bf17f94812748424b854e8c3e437c25c2 /src/logger.h | |
parent | 3f26c19b0b7a9fa73c58189788972ea43b72f014 (diff) | |
download | libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.gz libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.bz2 libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.xz libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.zip |
Fixed a minor bug in FString, and added the Logger and a test...it's cool, and
a decent replacement for multilog now that we use runit.
Diffstat (limited to 'src/logger.h')
-rw-r--r-- | src/logger.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..f8e1692 --- /dev/null +++ b/src/logger.h | |||
@@ -0,0 +1,112 @@ | |||
1 | #ifndef BU_LOGGER_H | ||
2 | #define BU_LOGGER_H | ||
3 | |||
4 | #include "bu/singleton.h" | ||
5 | #include "bu/fstring.h" | ||
6 | |||
7 | namespace Bu | ||
8 | { | ||
9 | /** | ||
10 | * Simple logging facility. All output goes straight to stdout, unlike the | ||
11 | * old multi-log system. Generally we expect any program complex enough to | ||
12 | * want to use this will have other facilities for processing the logging | ||
13 | * output, but if we need it we can add other output methods. | ||
14 | * | ||
15 | * Currently implemented as a singleton to avoid clutter with globals, you | ||
16 | * generally never want to use the logging system directly, it's annoying. | ||
17 | * Instead use the handy macros lineLog, setLogMask, setLogFormat, and | ||
18 | * setLogLevel. They do all the real work for you. | ||
19 | * | ||
20 | * In the log format, you can specify extra information that will be written | ||
21 | * to the log with every message, and extras in printf style. Use %X flags | ||
22 | * where X is one of the following: | ||
23 | * - L - Logging level of the log message (not the current mask) | ||
24 | * - y - Full year | ||
25 | * - m - Month | ||
26 | * - d - Day of month | ||
27 | * - h - Hour (24-hour format) | ||
28 | * - M - Minutes | ||
29 | * - s - Seconds | ||
30 | * - f - Source file | ||
31 | * - l - Line number | ||
32 | * - F - function name | ||
33 | * - t - Text of message (usually important) | ||
34 | * | ||
35 | * You can include anything extra that you would like, a newline will always | ||
36 | * be added automatically, so no need to worry about that. You can also | ||
37 | * include any extra printf style formatting that you would like, for | ||
38 | * example: "%h:%02M:%02s" for the time 4:02:09 instead of 4:2:9. | ||
39 | * | ||
40 | * It's generally handy to create an enum of values you use as levels during | ||
41 | * program execution (such as error, warning, info, debug, etc). These | ||
42 | * levels should be treated as bitflags, and the most desirable messages, | ||
43 | * i.e. serious errors and the like should be low order (0x01), and the much | ||
44 | * less desirable messages, like debugging info, should be higher order | ||
45 | * (0xF0). During operation you can then set either an explicit mask, | ||
46 | * selecting just the levels that you would like to see printed, or set the | ||
47 | * mask using the setLevel helper function, which simulates verbosity | ||
48 | * levels, enabling every flag lower order than the highest order set bit | ||
49 | * passed. I.E. if you had the following enumerated levels: | ||
50 | * | ||
51 | *@code | ||
52 | enum { | ||
53 | logError = 0x01, | ||
54 | logWarning = 0x02, | ||
55 | logInfo = 0x04, | ||
56 | logDebug = 0x08 | ||
57 | }; | ||
58 | @endcode | ||
59 | * And you set the mask with setMask( logInfo ) the only messages you would | ||
60 | * see are the ones catagorized logInfo. However, if you used | ||
61 | * setLevel( logInfo ) then you would see logInfo, logWarning, and logError | ||
62 | * type messages, since they are lower order. | ||
63 | */ | ||
64 | class Logger : public Bu::Singleton<Bu::Logger> | ||
65 | { | ||
66 | friend class Bu::Singleton<Bu::Logger>; | ||
67 | private: | ||
68 | Logger(); | ||
69 | virtual ~Logger(); | ||
70 | |||
71 | public: | ||
72 | void log( int nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...); | ||
73 | |||
74 | void setFormat( const Bu::FString &str ); | ||
75 | void setMask( int n ); | ||
76 | void setLevel( int n ); | ||
77 | |||
78 | private: | ||
79 | Bu::FString sLogFormat; | ||
80 | int nLevelMask; | ||
81 | }; | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Use Bu::Logger to log a message at the given level and with the given message | ||
86 | * using printf style formatting, and include extra data such as the current | ||
87 | * file, line number, and function. | ||
88 | */ | ||
89 | #define lineLog( nLevel, sFrmt, ...) \ | ||
90 | Bu::Logger::getInstance().log( nLevel, __FILE__, __PRETTY_FUNCTION__, __LINE__, sFrmt, ##__VA_ARGS__ ) | ||
91 | |||
92 | /** | ||
93 | * Set the Bu::Logger logging mask directly. See Bu::Logger::setMask for | ||
94 | * details. | ||
95 | */ | ||
96 | #define setLogMask( nLevel ) \ | ||
97 | Bu::Logger::getInstance().setMask( nLevel ) | ||
98 | |||
99 | /** | ||
100 | * Set the Bu::Logger format. See Bu::Logger::setFormat for details. | ||
101 | */ | ||
102 | #define setLogFormat( sFrmt ) \ | ||
103 | Bu::Logger::getInstance().setFormat( sFrmt ) | ||
104 | |||
105 | /** | ||
106 | * Set the Bu::Logger logging mask simulating levels. See Bu::Logger::setLevel | ||
107 | * for details. | ||
108 | */ | ||
109 | #define setLogLevel( nLevel ) \ | ||
110 | Bu::Logger::getInstance().setLevel( nLevel ) | ||
111 | |||
112 | #endif | ||