]> git.lyx.org Git - features.git/blob - src/support/DebugStream.h
Replace LString.h with support/std_string.h,
[features.git] / src / support / DebugStream.h
1 // -*- C++ -*-
2 /**
3  * \file DebugStream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DEBUGSTREAM_H
13 #define DEBUGSTREAM_H
14
15 #include "std_ostream.h"
16
17 #ifdef TEST_DEBUGSTREAM
18 #include <string>
19 struct Debug {
20         enum type {
21                 NONE = 0,
22                 INFO       = (1 << 0),   // 1
23                 WARN       = (1 << 1),   // 2
24                 CRIT       = (1 << 2)   // 4
25         };
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;
32                 return Debug::NONE;
33         }
34 };
35 #endif
36
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.
40
41     Example of Usage:
42     DebugStream debug;
43     debug.level(Debug::INFO);
44     debug.debug(Debug::WARN) << "WARN\n";
45     debug[Debug::INFO] << "INFO\n";
46     debug << "Always\n";
47
48     Will output:
49     INFO
50     Always
51
52     If you want to have debug output from time critical code you should
53     use this construct:
54     if (debug.debugging(Debug::INFO)) {
55          debug << "...debug output...\n";
56     }
57
58     To give debug info even if no debug (NONE) is requested:
59     debug << "... always output ...\n";
60
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";
64
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";
68
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";
72
73 */
74 class DebugStream : public std::ostream
75 {
76 public:
77         /// Constructor, sets the debug level to t.
78         explicit DebugStream(Debug::type t = Debug::NONE);
79
80         /// Constructor, sets the log file to f, and the debug level to t.
81         explicit
82         DebugStream(char const * f, Debug::type t = Debug::NONE);
83
84         ///
85         ~DebugStream();
86
87         /// Sets the debug level to t.
88         void level(Debug::type t) {
89                 dt = Debug::type(t & Debug::ANY);
90         }
91
92         /// Returns the current debug level.
93         Debug::type level() const {
94                 return dt;
95         }
96
97         /// Adds t to the current debug level.
98         void addLevel(Debug::type t) {
99                 dt = Debug::type(dt | t);
100         }
101
102         /// Deletes t from the current debug level.
103         void delLevel(Debug::type t) {
104                 dt = Debug::type(dt & ~t);
105         }
106
107         /// Sets the debugstreams' logfile to f.
108         void logFile(char const * f);
109
110         /// Returns true if t is part of the current debug level.
111         bool debugging(Debug::type t = Debug::ANY) const
112         {
113                 if (dt & t) return true;
114                 return false;
115         }
116
117
118         /** Returns the no-op stream if t is not part of the
119             current debug level otherwise the real debug stream
120             is used.
121         */
122         std::ostream & debug(Debug::type t = Debug::ANY) {
123                 if (dt & t) return *this;
124                 return nullstream;
125         }
126
127
128         /** This is an operator to give a more convenient use:
129             dbgstream[Debug::INFO] << "Info!\n";
130         */
131         std::ostream & operator[](Debug::type t) {
132                 return debug(t);
133         }
134 private:
135         /// The current debug level
136         Debug::type dt;
137         /// The no-op stream.
138         std::ostream nullstream;
139         ///
140         struct debugstream_internal;
141         ///
142         debugstream_internal * internal;
143 };
144
145 #endif