]> git.lyx.org Git - lyx.git/blob - src/support/DebugStream.h
lyxserver cleanup patch + andre's small patches
[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 #ifdef TEST_DEBUGSTREAM
22 #include <string>
23 struct Debug {
24         enum type {
25                 NONE = 0,
26                 INFO       = (1 << 0),   // 1
27                 WARN       = (1 << 1),   // 2
28                 CRIT       = (1 << 2)   // 4
29         };
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;
36                 return Debug::NONE;
37         }
38 };
39 #endif
40
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.
44
45     Example of Usage:
46     DebugStream debug;
47     debug.level(Debug::INFO);
48     debug.debug(Debug::WARN) << "WARN\n";
49     debug[Debug::INFO] << "INFO\n";
50     debug << "Always\n";
51
52     Will output:
53     INFO
54     Always
55
56     If you want to have debug output from time critical code you should 
57     use this construct:
58     if (debug.debugging(Debug::INFO)) {
59          debug << "...debug output...\n";
60     }
61     
62     To give debug info even if no debug (NONE) is requested:
63     debug << "... always output ...\n";
64
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";
68
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";
72
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";
76
77 */
78 class DebugStream : public std::ostream
79 {
80 public:
81         /// Constructor, sets the debug level to t.
82         explicit DebugStream(Debug::type t = Debug::NONE);
83         
84         /// Constructor, sets the log file to f, and the debug level to t.
85         explicit
86         DebugStream(char const * f, Debug::type t = Debug::NONE);
87
88         ///
89         ~DebugStream();
90
91         /// Sets the debug level to t.
92         void level(Debug::type t) {
93                 dt = Debug::type(t & Debug::ANY);
94         }
95
96         /// Returns the current debug level.
97         Debug::type level() const {
98                 return dt;
99         }
100
101         /// Adds t to the current debug level.
102         void addLevel(Debug::type t) {
103                 dt = Debug::type(dt | t);
104         }
105
106         /// Deletes t from the current debug level.
107         void delLevel(Debug::type t) {
108                 dt = Debug::type(dt & ~t);
109         }
110
111         /// Sets the debugstreams' logfile to f.
112         void logFile(char const * f);
113         
114         /// Returns true if t is part of the current debug level.
115         bool debugging(Debug::type t = Debug::ANY) const
116         {
117                 if (dt & t) return true;
118                 return false;
119         }
120
121         
122         /** Returns the no-op stream if t is not part of the
123             current debug level otherwise the real debug stream
124             is used.
125         */
126         std::ostream & debug(Debug::type t = Debug::ANY) {
127                 if (dt & t) return *this;
128                 return nullstream;
129         }
130
131         
132         /** This is an operator to give a more convenient use:
133             dbgstream[Debug::INFO] << "Info!\n";
134         */
135         std::ostream & operator[](Debug::type t) {
136                 return debug(t);
137         }
138 private:
139         /// The current debug level
140         Debug::type dt;
141         /// The no-op stream.
142         std::ostream nullstream;
143         ///
144         struct debugstream_internal;
145         ///
146         debugstream_internal * internal;
147 };
148
149 #endif
150
151