3 // Created by Lars Gullik Bjønnes
4 // Copyright 1999 Lars Gullik Bjønnes (larsbj@lyx.org)
5 // Released under the Gnu General Public License
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.
15 #ifdef _STANDARD_C_PLUS_PLUS
25 #ifdef TEST_DEBUGSTREAM
40 static const type ANY = type(INFO | WARN | CRIT);
42 /** A function to convert symbolic string names on debug levels
43 to their numerical value.
45 static Debug::type value(std::string const & val) {
46 if (val == "NONE") return Debug::NONE;
47 if (val == "INFO") return Debug::INFO;
48 if (val == "WARN") return Debug::WARN;
49 if (val == "CRIT") return Debug::CRIT;
56 /** DebugStream is a ostream intended for debug output. It has also support
57 for a logfile. Debug output is output to cerr and if the logfile is set,
62 debug.level(Debug::INFO);
63 debug.debug(Debug::WARN) << "WARN\n";
64 debug[Debug::INFO] << "INFO\n";
71 If you want to have debug output from time critical code you should
73 if (debug.debugging(Debug::INFO)) {
74 debug << "...debug output...\n";
77 To give debug info even if no debug (NONE) is requested:
78 debug << "... always output ...\n";
80 To give debug output regardless of what debug level is set (!NONE):
81 debug.debug() << "...on debug output...\n";
82 debug[Debug::ANY] << "...on debug output...\n";
84 To give debug output when a specific debug level is set (INFO):
85 debug.debug(Debug::INFO) << "...info...\n";
86 debug[Debug::INFO] << "...info...\n";
88 To give debug output when either on of debug levels is set (INFO or CRIT):
89 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
90 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
93 class DebugStream : public std::ostream {
95 /// Constructor, sets the debug level to t.
96 DebugStream(Debug::type t = Debug::NONE);
98 /// Constructor, sets the log file to f, and the debug level to t.
99 DebugStream(char const * f, Debug::type t = Debug::NONE);
102 virtual ~DebugStream();
104 /// Sets the debug level to t.
105 void level(Debug::type t) {
106 dt = Debug::type(t & Debug::ANY);
109 /// Returns the current debug level.
110 Debug::type level() const {
114 /// Adds t to the current debug level.
115 void addLevel(Debug::type t) {
116 dt = Debug::type(dt | t);
119 /// Deletes t from the current debug level.
120 void delLevel(Debug::type t) {
121 dt = Debug::type(dt & ~t);
124 /// Sets the debugstreams' logfile to f.
125 void logFile(char const * f);
127 /// Returns true if t is part of the current debug level.
128 bool debugging(Debug::type t = Debug::ANY) const
130 if (dt & t) return true;
135 /** Returns the no-op stream if t is not part of the
136 current debug level otherwise the real debug stream
139 std::ostream & debug(Debug::type t = Debug::ANY) {
140 if (dt & t) return *this;
145 /** This is an operator to give a more convenient use:
146 dbgstream[Debug::INFO] << "Info!\n";
148 std::ostream & operator[](Debug::type t) {
152 /// The current debug level
154 /// The no-op stream.
155 std::ostream nullstream;
156 struct debugstream_internal;
157 debugstream_internal * internal;