4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #ifdef TEST_DEBUGSTREAM
26 static const type ANY = type(INFO | WARN | CRIT);
27 static Debug::type value(string const & val) {
28 if (val == "NONE") return Debug::NONE;
29 if (val == "INFO") return Debug::INFO;
30 if (val == "WARN") return Debug::WARN;
31 if (val == "CRIT") return Debug::CRIT;
37 /** DebugStream is a ostream intended for debug output.
38 It has also support for a logfile. Debug output is output to cerr
39 and if the logfile is set, to the logfile.
43 debug.level(Debug::INFO);
44 debug.debug(Debug::WARN) << "WARN\n";
45 debug[Debug::INFO] << "INFO\n";
52 If you want to have debug output from time critical code you should
54 if (debug.debugging(Debug::INFO)) {
55 debug << "...debug output...\n";
58 To give debug info even if no debug (NONE) is requested:
59 debug << "... always output ...\n";
61 To give debug output regardless of what debug level is set (!NONE):
62 debug.debug() << "...on debug output...\n";
63 debug[Debug::ANY] << "...on debug output...\n";
65 To give debug output when a specific debug level is set (INFO):
66 debug.debug(Debug::INFO) << "...info...\n";
67 debug[Debug::INFO] << "...info...\n";
69 To give debug output when either on of debug levels is set (INFO or CRIT):
70 debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
71 debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
74 class DebugStream : public std::ostream
77 /// Constructor, sets the debug level to t.
78 explicit DebugStream(Debug::type t = Debug::NONE);
80 /// Constructor, sets the log file to f, and the debug level to t.
82 DebugStream(char const * f, Debug::type t = Debug::NONE);
87 /// Sets the debug level to t.
88 void level(Debug::type t) {
89 dt = Debug::type(t & Debug::ANY);
92 /// Returns the current debug level.
93 Debug::type level() const {
97 /// Adds t to the current debug level.
98 void addLevel(Debug::type t) {
99 dt = Debug::type(dt | t);
102 /// Deletes t from the current debug level.
103 void delLevel(Debug::type t) {
104 dt = Debug::type(dt & ~t);
107 /// Sets the debugstreams' logfile to f.
108 void logFile(char const * f);
110 /// Returns true if t is part of the current debug level.
111 bool debugging(Debug::type t = Debug::ANY) const
113 if (dt & t) return true;
118 /** Returns the no-op stream if t is not part of the
119 current debug level otherwise the real debug stream
122 std::ostream & debug(Debug::type t = Debug::ANY) {
123 if (dt & t) return *this;
128 /** This is an operator to give a more convenient use:
129 dbgstream[Debug::INFO] << "Info!\n";
131 std::ostream & operator[](Debug::type t) {
135 /// The current debug level
137 /// The no-op stream.
138 std::ostream nullstream;
140 struct debugstream_internal;
142 debugstream_internal * internal;