]> git.lyx.org Git - lyx.git/blob - src/support/DebugStream.h
remove commented HAVE_SSTREAM code
[lyx.git] / src / support / DebugStream.h
1 // -*- C++ -*-
2
3 // Created by Lars Gullik Bjønnes
4 // Copyright 1999 Lars Gullik Bjønnes (larsbj@lyx.org)
5 // Released into the public domain.
6
7 // Implemented and tested on g++ 2.7.2.3
8
9 // Primarily developed for use in the LyX Project http://www.lyx.org/
10 // but should be adaptable to any project.
11
12 #ifndef DEBUGSTREAM_H
13 #define DEBUGSTREAM_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "LOstream.h"
20
21 // I don't really like this, but too please doc++...(Lgb)
22 using std::ostream;
23
24 #ifdef TEST_DEBUGSTREAM
25 #include <string>
26 struct Debug {
27         enum type {
28                 NONE = 0,
29                 INFO       = (1 << 0),   // 1
30                 WARN       = (1 << 1),   // 2
31                 CRIT       = (1 << 2)   // 4
32         };
33         static const type ANY = type(INFO | WARN | CRIT);
34         static Debug::type value(string const & val) {
35                 if (val == "NONE") return Debug::NONE;
36                 if (val == "INFO") return Debug::INFO;
37                 if (val == "WARN") return Debug::WARN;
38                 if (val == "CRIT") return Debug::CRIT;
39                 return Debug::NONE;
40         }
41 };
42 #endif
43
44 /** DebugStream is a ostream intended for debug output.
45     It has also support for a logfile. Debug output is output to cerr
46     and if the logfile is set, to the logfile.
47
48     Example of Usage:
49     DebugStream debug;
50     debug.level(Debug::INFO);
51     debug.debug(Debug::WARN) << "WARN\n";
52     debug[Debug::INFO] << "INFO\n";
53     debug << "Always\n";
54
55     Will output:
56     INFO
57     Always
58
59     If you want to have debug output from time critical code you should 
60     use this construct:
61     if (debug.debugging(Debug::INFO)) {
62          debug << "...debug output...\n";
63     }
64     
65     To give debug info even if no debug (NONE) is requested:
66     debug << "... always output ...\n";
67
68     To give debug output regardless of what debug level is set (!NONE):
69     debug.debug() << "...on debug output...\n";
70     debug[Debug::ANY] << "...on debug output...\n";
71
72     To give debug output when a specific debug level is set (INFO):
73     debug.debug(Debug::INFO) << "...info...\n";
74     debug[Debug::INFO] << "...info...\n";
75
76     To give debug output when either on of debug levels is set (INFO or CRIT):
77     debug.debug(Debug::type(Debug::INFO | Debug::CRIT)) << "...info/crit...\n";
78     debug[Debug::type(Debug::INFO | Debug::CRIT)] << "...info/crit...\n";
79
80 */
81 class DebugStream : public ostream
82 {
83 // This workaround is needed only for gcc 2.8.1 (and possibly egcs
84 // 1.0.x), which generates a compiler error when subclassing from
85 // std::. (JMarc)
86 public:
87         /// Constructor, sets the debug level to t.
88         explicit DebugStream(Debug::type t = Debug::NONE);
89         
90         /// Constructor, sets the log file to f, and the debug level to t.
91         explicit
92         DebugStream(char const * f, Debug::type t = Debug::NONE);
93
94         ///
95         ~DebugStream();
96
97         /// Sets the debug level to t.
98         void level(Debug::type t) {
99                 dt = Debug::type(t & Debug::ANY);
100         }
101
102         /// Returns the current debug level.
103         Debug::type level() const {
104                 return dt;
105         }
106
107         /// Adds t to the current debug level.
108         void addLevel(Debug::type t) {
109                 dt = Debug::type(dt | t);
110         }
111
112         /// Deletes t from the current debug level.
113         void delLevel(Debug::type t) {
114                 dt = Debug::type(dt & ~t);
115         }
116
117         /// Sets the debugstreams' logfile to f.
118         void logFile(char const * f);
119         
120         /// Returns true if t is part of the current debug level.
121         bool debugging(Debug::type t = Debug::ANY) const
122         {
123                 if (dt & t) return true;
124                 return false;
125         }
126
127         
128         /** Returns the no-op stream if t is not part of the
129             current debug level otherwise the real debug stream
130             is used.
131         */
132         std::ostream & debug(Debug::type t = Debug::ANY) {
133                 if (dt & t) return *this;
134                 return nullstream;
135         }
136
137         
138         /** This is an operator to give a more convenient use:
139             dbgstream[Debug::INFO] << "Info!\n";
140         */
141         std::ostream & operator[](Debug::type t) {
142                 return debug(t);
143         }
144 private:
145         /// The current debug level
146         Debug::type dt;
147         /// The no-op stream.
148         std::ostream nullstream;
149         ///
150         struct debugstream_internal;
151         ///
152         debugstream_internal * internal;
153 };
154
155 #endif
156
157