Branch data Line data Source code
1 : : // $Id: DebugLogger.h 4771 2007-08-11 05:50:24Z vern $
2 : : //
3 : : // A logger for (selective) debugging output. Only compiled in if DEBUG is
4 : : // defined.
5 : :
6 : : #ifndef debug_logger_h
7 : : #define debug_logger_h
8 : :
9 : : #ifdef DEBUG
10 : :
11 : : #include <stdio.h>
12 : :
13 : : // To add a new debugging stream, add a constant here as well as
14 : : // an entry to DebugLogger::streams in DebugLogger.cc.
15 : :
16 : : enum DebugStream {
17 : : DBG_SERIAL, // Serialization
18 : : DBG_RULES, // Signature matching
19 : : DBG_COMM, // Remote communication
20 : : DBG_STATE, // StateAccess logging
21 : : DBG_CHUNKEDIO, // ChunkedIO logging
22 : : DBG_COMPRESSOR, // Connection compressor
23 : : DBG_STRING, // String code
24 : : DBG_NOTIFIERS, // Notifiers (see StateAccess.h)
25 : : DBG_MAINLOOP, // Main IOSource loop
26 : : DBG_DPD, // Dynamic application detection framework
27 : : DBG_TM, // Time-machine packet input via Brocolli
28 : :
29 : : NUM_DBGS // Has to be last
30 : : };
31 : :
32 : : #define DBG_LOG(args...) debug_logger.Log(args)
33 : : #define DBG_LOG_VERBOSE(args...) \
34 : : if ( debug_logger.IsVerbose() ) \
35 : : debug_logger.Log(args)
36 : : #define DBG_PUSH(stream) debug_logger.PushIndent(stream)
37 : : #define DBG_POP(stream) debug_logger.PopIndent(stream)
38 : :
39 : : class DebugLogger {
40 : : public:
41 : : // Output goes to stderr per default.
42 : : DebugLogger(const char* filename = 0);
43 : : ~DebugLogger();
44 : :
45 : : void Log(DebugStream stream, const char* fmt, ...);
46 : :
47 : 358 : void PushIndent(DebugStream stream)
48 : 358 : { ++streams[int(stream)].indent; }
49 : 358 : void PopIndent(DebugStream stream)
50 : 358 : { --streams[int(stream)].indent; }
51 : :
52 : : void EnableStream(DebugStream stream)
53 : : { streams[int(stream)].enabled = true; }
54 : : void DisableStream(DebugStream stream)
55 : : { streams[int(stream)].enabled = false; }
56 : :
57 : : // Takes comma-seperated list of stream prefixes.
58 : : void EnableStreams(const char* streams);
59 : :
60 : 65 : bool IsEnabled(DebugStream stream) const
61 : 65 : { return streams[int(stream)].enabled; }
62 : :
63 : : void SetVerbose(bool arg_verbose) { verbose = arg_verbose; }
64 : 326717 : bool IsVerbose() const { return verbose; }
65 : :
66 : : private:
67 : : FILE* file;
68 : : bool verbose;
69 : :
70 : : struct Stream {
71 : : const char* prefix;
72 : : int indent;
73 : : bool enabled;
74 : : };
75 : :
76 : : static Stream streams[NUM_DBGS];
77 : : };
78 : :
79 : : extern DebugLogger debug_logger;
80 : :
81 : : #else
82 : : #define DBG_LOG(args...)
83 : : #define DBG_LOG_VERBOSE(args...)
84 : : #define DBG_PUSH(stream)
85 : : #define DBG_POP(stream)
86 : : #endif
87 : :
88 : : #endif
|