3 // Created by Lars Gullik Bjønnes
4 // Copyright 1999 Lars Gullik Bjønnes (larsbj@lyx.org)
5 // Released into the public domain.
7 // Implemented and tested on g++ 2.7.2.3
9 // Primarily developed for use in the LyX Project http://www.lyx.org/
10 // but should be adaptable to any project.
19 #ifdef TEST_DEBUGSTREAM
34 static const type ANY = type(INFO | WARN | CRIT);
36 /** A function to convert symbolic string names on debug levels
37 to their numerical value.
39 static Debug::type value(string const & val) {
40 if (val == "NONE") return Debug::NONE;
41 if (val == "INFO") return Debug::INFO;
42 if (val == "WARN") return Debug::WARN;
43 if (val == "CRIT") return Debug::CRIT;
50 /** DebugStream is a ostream intended for debug output. It has also support
51 for a logfile. Debug output is output to cerr and if the logfile is set,
56 debug.level(Debug::INFO);
57 debug.debug(Debug::WARN) << "WARN\n";
58 debug[Debug::INFO] << "INFO\n";
65 If you want to have debug output from time critical code you should
67 if (debug.debugging(Debug::INFO)) {
68 debug << "...debug output...\n";
71 To give debug info even if no debug (NONE) is requested:
72 debug << "... always output ...\n";
74 To give debug output regardless of what debug level is set (!NONE):
75 debug.debug() << "...on debug output...\n";
76 debug[Debug::ANY] << "...on debug output...\n";
78 To give debug output when a specific debug level is set (INFO):
79 debug.debug(Debug::INFO) << "...info...\n";
80 debug[Debug::INFO] << "...info...\n";
82 To give debug output when either on of debug levels is set (INFO or CRIT):
83 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
84 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
87 class DebugStream : public ostream {
89 /// Constructor, sets the debug level to t.
90 explicit DebugStream(Debug::type t = Debug::NONE);
92 /// Constructor, sets the log file to f, and the debug level to t.
93 DebugStream(char const * f, Debug::type t = Debug::NONE);
96 virtual ~DebugStream();
98 /// Sets the debug level to t.
99 void level(Debug::type t) {
100 dt = Debug::type(t & Debug::ANY);
103 /// Returns the current debug level.
104 Debug::type level() const {
108 /// Adds t to the current debug level.
109 void addLevel(Debug::type t) {
110 dt = Debug::type(dt | t);
113 /// Deletes t from the current debug level.
114 void delLevel(Debug::type t) {
115 dt = Debug::type(dt & ~t);
118 /// Sets the debugstreams' logfile to f.
119 void logFile(char const * f);
121 /// Returns true if t is part of the current debug level.
122 bool debugging(Debug::type t = Debug::ANY) const
124 if (dt & t) return true;
129 /** Returns the no-op stream if t is not part of the
130 current debug level otherwise the real debug stream
133 ostream & debug(Debug::type t = Debug::ANY) {
134 if (dt & t) return *this;
139 /** This is an operator to give a more convenient use:
140 dbgstream[Debug::INFO] << "Info!\n";
142 ostream & operator[](Debug::type t) {
146 /// The current debug level
148 /// The no-op stream.
150 struct debugstream_internal;
151 debugstream_internal * internal;