aboutsummaryrefslogtreecommitdiff
path: root/src/logger.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-07-03 00:28:59 +0000
committerMike Buland <eichlan@xagasoft.com>2007-07-03 00:28:59 +0000
commitac517a2b7625e0aa0862679e961c6349f859ea3b (patch)
treee3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/logger.h
parentf8d4301e9fa4f3709258505941e37fab2eadadc6 (diff)
parentbd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff)
downloadlibbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to '')
-rw-r--r--src/logger.h112
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
7namespace 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