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
30 static const type ANY = type(INFO | WARN | CRIT);
31 static Debug::type value(string const & val) {
32 if (val == "NONE") return Debug::NONE;
33 if (val == "INFO") return Debug::INFO;
34 if (val == "WARN") return Debug::WARN;
35 if (val == "CRIT") return Debug::CRIT;
41 /** DebugStream is a ostream intended for debug output.
42 It has also support for a logfile. Debug output is output to cerr
43 and if the logfile is set, to the logfile.
47 debug.level(Debug::INFO);
48 debug.debug(Debug::WARN) << "WARN\n";
49 debug[Debug::INFO] << "INFO\n";
56 If you want to have debug output from time critical code you should
58 if (debug.debugging(Debug::INFO)) {
59 debug << "...debug output...\n";
62 To give debug info even if no debug (NONE) is requested:
63 debug << "... always output ...\n";
65 To give debug output regardless of what debug level is set (!NONE):
66 debug.debug() << "...on debug output...\n";
67 debug[Debug::ANY] << "...on debug output...\n";
69 To give debug output when a specific debug level is set (INFO):
70 debug.debug(Debug::INFO) << "...info...\n";
71 debug[Debug::INFO] << "...info...\n";
73 To give debug output when either on of debug levels is set (INFO or CRIT):
74 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
75 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
78 class DebugStream : public std::ostream
81 /// Constructor, sets the debug level to t.
82 explicit DebugStream(Debug::type t = Debug::NONE);
84 /// Constructor, sets the log file to f, and the debug level to t.
86 DebugStream(char const * f, Debug::type t = Debug::NONE);
91 /// Sets the debug level to t.
92 void level(Debug::type t) {
93 dt = Debug::type(t & Debug::ANY);
96 /// Returns the current debug level.
97 Debug::type level() const {
101 /// Adds t to the current debug level.
102 void addLevel(Debug::type t) {
103 dt = Debug::type(dt | t);
106 /// Deletes t from the current debug level.
107 void delLevel(Debug::type t) {
108 dt = Debug::type(dt & ~t);
111 /// Sets the debugstreams' logfile to f.
112 void logFile(char const * f);
114 /// Returns true if t is part of the current debug level.
115 bool debugging(Debug::type t = Debug::ANY) const
117 if (dt & t) return true;
122 /** Returns the no-op stream if t is not part of the
123 current debug level otherwise the real debug stream
126 std::ostream & debug(Debug::type t = Debug::ANY) {
127 if (dt & t) return *this;
132 /** This is an operator to give a more convenient use:
133 dbgstream[Debug::INFO] << "Info!\n";
135 std::ostream & operator[](Debug::type t) {
139 /// The current debug level
141 /// The no-op stream.
142 std::ostream nullstream;
144 struct debugstream_internal;
146 debugstream_internal * internal;