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