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
28 using std::streamsize;
35 #ifdef TEST_DEBUGSTREAM
50 static const type ANY = type(INFO | WARN | CRIT);
52 /** A function to convert symbolic string names on debug levels
53 to their numerical value.
55 static Debug::type value(std::string const & val) {
56 if (val == "NONE") return Debug::NONE;
57 if (val == "INFO") return Debug::INFO;
58 if (val == "WARN") return Debug::WARN;
59 if (val == "CRIT") return Debug::CRIT;
66 /** DebugStream is a ostream intended for debug output. It has also support
67 for a logfile. Debug output is output to cerr and if the logfile is set,
72 debug.level(Debug::INFO);
73 debug.debug(Debug::WARN) << "WARN\n";
74 debug[Debug::INFO] << "INFO\n";
81 If you want to have debug output from time critical code you should
83 if (debug.debugging(Debug::INFO)) {
84 debug << "...debug output...\n";
87 To give debug info even if no debug (NONE) is requested:
88 debug << "... always output ...\n";
90 To give debug output regardless of what debug level is set (!NONE):
91 debug.debug() << "...on debug output...\n";
92 debug[Debug::ANY] << "...on debug output...\n";
94 To give debug output when a specific debug level is set (INFO):
95 debug.debug(Debug::INFO) << "...info...\n";
96 debug[Debug::INFO] << "...info...\n";
98 To give debug output when either on of debug levels is set (INFO or CRIT):
99 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
100 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
103 class DebugStream : public ostream {
105 /// Constructor, sets the debug level to t.
106 DebugStream(Debug::type t = Debug::NONE);
108 /// Constructor, sets the log file to f, and the debug level to t.
109 DebugStream(char const * f, Debug::type t = Debug::NONE);
112 virtual ~DebugStream();
114 /// Sets the debug level to t.
115 void level(Debug::type t) {
116 dt = Debug::type(t & Debug::ANY);
119 /// Returns the current debug level.
120 Debug::type level() const {
124 /// Adds t to the current debug level.
125 void addLevel(Debug::type t) {
126 dt = Debug::type(dt | t);
129 /// Deletes t from the current debug level.
130 void delLevel(Debug::type t) {
131 dt = Debug::type(dt & ~t);
134 /// Sets the debugstreams' logfile to f.
135 void logFile(char const * f);
137 /// Returns true if t is part of the current debug level.
138 bool debugging(Debug::type t = Debug::ANY) const
140 if (dt & t) return true;
145 /** Returns the no-op stream if t is not part of the
146 current debug level otherwise the real debug stream
149 ostream & debug(Debug::type t = Debug::ANY) {
150 if (dt & t) return *this;
155 /** This is an operator to give a more convenient use:
156 dbgstream[Debug::INFO] << "Info!\n";
158 ostream & operator[](Debug::type t) {
162 /// The current debug level
164 /// The no-op stream.
166 struct debugstream_internal;
167 debugstream_internal * internal;