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.
21 #ifdef TEST_DEBUGSTREAM
36 static const type ANY = type(INFO | WARN | CRIT);
38 /** A function to convert symbolic string names on debug levels
39 to their numerical value.
41 static Debug::type value(string const & val) {
42 if (val == "NONE") return Debug::NONE;
43 if (val == "INFO") return Debug::INFO;
44 if (val == "WARN") return Debug::WARN;
45 if (val == "CRIT") return Debug::CRIT;
52 /** DebugStream is a ostream intended for debug output. It has also support
53 for a logfile. Debug output is output to cerr and if the logfile is set,
58 debug.level(Debug::INFO);
59 debug.debug(Debug::WARN) << "WARN\n";
60 debug[Debug::INFO] << "INFO\n";
67 If you want to have debug output from time critical code you should
69 if (debug.debugging(Debug::INFO)) {
70 debug << "...debug output...\n";
73 To give debug info even if no debug (NONE) is requested:
74 debug << "... always output ...\n";
76 To give debug output regardless of what debug level is set (!NONE):
77 debug.debug() << "...on debug output...\n";
78 debug[Debug::ANY] << "...on debug output...\n";
80 To give debug output when a specific debug level is set (INFO):
81 debug.debug(Debug::INFO) << "...info...\n";
82 debug[Debug::INFO] << "...info...\n";
84 To give debug output when either on of debug levels is set (INFO or CRIT):
85 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
86 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
90 // This workaround is needed only for gcc 2.8.1 (and possibly egcs
91 // 1.0.x), which generates a compiler error when subclassing from
93 #ifdef CXX_WORKING_NAMESPACES
94 class DebugStream : public std::ostream
96 class DebugStream : public ostream
100 /// Constructor, sets the debug level to t.
101 explicit DebugStream(Debug::type t = Debug::NONE);
103 /// Constructor, sets the log file to f, and the debug level to t.
105 DebugStream(char const * f, Debug::type t = Debug::NONE);
108 virtual ~DebugStream();
110 /// Sets the debug level to t.
111 void level(Debug::type t) {
112 dt = Debug::type(t & Debug::ANY);
115 /// Returns the current debug level.
116 Debug::type level() const {
120 /// Adds t to the current debug level.
121 void addLevel(Debug::type t) {
122 dt = Debug::type(dt | t);
125 /// Deletes t from the current debug level.
126 void delLevel(Debug::type t) {
127 dt = Debug::type(dt & ~t);
130 /// Sets the debugstreams' logfile to f.
131 void logFile(char const * f);
133 /// Returns true if t is part of the current debug level.
134 bool debugging(Debug::type t = Debug::ANY) const
136 if (dt & t) return true;
141 /** Returns the no-op stream if t is not part of the
142 current debug level otherwise the real debug stream
145 std::ostream & debug(Debug::type t = Debug::ANY) {
146 if (dt & t) return *this;
151 /** This is an operator to give a more convenient use:
152 dbgstream[Debug::INFO] << "Info!\n";
154 std::ostream & operator[](Debug::type t) {
158 /// The current debug level
160 /// The no-op stream.
161 std::ostream nullstream;
162 struct debugstream_internal;
163 debugstream_internal * internal;