]> git.lyx.org Git - lyx.git/blob - src/support/debug.h
no stl includes in debug.h
[lyx.git] / src / support / debug.h
1 // -*- C++ -*-
2 /**
3  * \file debug.h
4  *
5  * FIXME: It would be nice if, in lyx::use_gui mode, instead of
6  * outputting to the console, we would pipe all messages onto a file
7  * and visualise the contents dynamically in a Qt window if needed.
8  *
9  * This file is part of LyX, the document processor.
10  * Licence details can be found in the file COPYING.
11  *
12  * \author Lars Gullik Bjønnes
13  * \author Jean-Marc Lasgouttes
14  * \author Pavel Sanda
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #ifndef LYXDEBUG_H
20 #define LYXDEBUG_H
21
22 #include "support/strfwd.h"
23
24 namespace std {
25
26 class ios_base;
27
28 template<typename CharT, typename Traits> class basic_streambuf;
29 typedef basic_streambuf<char, char_traits<char> > streambuf;
30
31 }
32
33
34 namespace lyx {
35
36 ///  This is all the different debug levels that we have.
37 namespace Debug {
38         ///
39         enum Type {
40                 ///
41                 NONE = 0,
42                 ///
43                 INFO       = (1 << 0),   // 1
44                 ///
45                 INIT       = (1 << 1),   // 2
46                 ///
47                 KEY        = (1 << 2),   // 4
48                 ///
49                 GUI        = (1 << 3),   // 8
50                 ///
51                 PARSER     = (1 << 4),   // 16
52                 ///
53                 LYXRC      = (1 << 5),   // 32
54                 ///
55                 KBMAP      = (1 << 6),   // 64
56                 ///
57                 LATEX      = (1 << 7),   // 128
58                 ///
59                 MATHED     = (1 << 8),   // 256 // Alejandro, please use this.
60                 ///
61                 FONT       = (1 << 9),   // 512
62                 ///
63                 TCLASS     = (1 << 10),  // 1024
64                 ///
65                 LYXVC      = (1 << 11),  // 2048
66                 ///
67                 LYXSERVER  = (1 << 12),  // 4096
68                 ///
69                 UNDO       = (1 << 13),  // 8192
70                 ///
71                 ACTION     = (1 << 14),   // 16384
72                 ///
73                 LYXLEX     = (1 << 15),
74                 ///
75                 DEPEND     = (1 << 16),
76                 ///
77                 INSETS     = (1 << 17),
78                 ///
79                 FILES      = (1 << 18),
80                 ///
81                 WORKAREA   = (1 << 19),
82                 ///
83                 INSETTEXT  = (1 << 20),
84                 ///
85                 GRAPHICS   = (1 << 21),
86                 /// change tracking
87                 CHANGES    = (1 << 22),
88                 ///
89                 EXTERNAL   = (1 << 23),
90                 ///
91                 PAINTING   = (1 << 24),
92                 ///
93                 SCROLLING  = (1 << 25),
94                 ///
95                 MACROS     = (1 << 26),
96                 ///     rtl-related
97                 RTL        = (1 << 27),
98                 ///     locale related
99                 LOCALE     = (1 << 28),
100                 ///     selection
101                 SELECTION  = (1 << 29),
102                 /// Find and Replace
103                 FIND       = (1 << 30),
104                 ///
105                 DEBUG      = (1 << 31),
106                 ///
107                 ANY = 0xffffffff
108         };
109
110
111         // Return number of levels
112         int levelCount();
113  
114
115         /** A function to convert symbolic string names on debug levels
116             to their numerical value.
117         */
118         Type value(std::string const & val);
119
120         /** A function to convert index of level to their numerical value.
121         */
122         Type value(int val);
123
124         /// Return description of level
125         std::string const description(Type val);
126
127         /// Return name of level
128         std::string const name(Type val);
129
130
131         /** Display the tags and descriptions of the current debug level
132             of ds
133         */
134         void showLevel(std::ostream & os, Type level);
135
136         /** show all the possible tags that can be used for debugging */
137         void showTags(std::ostream & os);
138
139 } // namespace Debug
140
141
142 inline void operator|=(Debug::Type & d1, Debug::Type d2)
143 {
144         d1 = static_cast<Debug::Type>(d1 | d2);
145 }
146
147
148 class LyXErr
149 {
150 public:
151         LyXErr(): enabled_(true), second_used_(false) {}
152         /// Disable the stream completely
153         void disable();
154         /// Enable the stream after a possible call of disable()
155         void enable();
156         ///
157         bool enabled() const { return enabled_; }
158         /// Returns true if t is part of the current debug level.
159         bool debugging(Debug::Type t = Debug::ANY) const;
160         /// Ends output
161         void endl();
162         /// Sets stream
163         void setStream(std::ostream & os) { stream_ = &os; }
164         /// Sets stream
165         std::ostream & stream() { return *stream_; }
166         /// Sets the debug level to t.
167         void setLevel(Debug::Type t) { dt = t; }
168         /// Returns the current debug level.
169         Debug::Type level() const { return dt; }
170         /// Returns stream
171         operator std::ostream &() { return *stream_; }
172         /// Returns second_used_
173         bool second_used() { return second_used_; }
174         // Returns second stream
175         std::ostream & second() { return *second_; };
176         /// Sets the second stream
177         void setSecond(std::ostream * os) { second_used_ = (second_ = os); }
178
179 private:
180         /// The current debug level
181         Debug::Type dt;
182         /// Is the stream enabled?
183         bool enabled_;
184         /// The real stream
185         std::ostream * stream_;
186         /// Next stream for output duplication
187         std::ostream * second_;
188         /// Is the second stream enabled?
189         bool second_used_;
190 };
191
192 namespace support { class FileName; }
193
194 LyXErr & operator<<(LyXErr &, void const *);
195 LyXErr & operator<<(LyXErr &, char const *);
196 LyXErr & operator<<(LyXErr &, char);
197 LyXErr & operator<<(LyXErr &, int);
198 LyXErr & operator<<(LyXErr &, unsigned int);
199 LyXErr & operator<<(LyXErr &, long);
200 LyXErr & operator<<(LyXErr &, unsigned long);
201 LyXErr & operator<<(LyXErr &, double);
202 LyXErr & operator<<(LyXErr &, std::string const &);
203 LyXErr & operator<<(LyXErr &, docstring const &);
204 LyXErr & operator<<(LyXErr &, support::FileName const &);
205 LyXErr & operator<<(LyXErr &, std::ostream &(*)(std::ostream &));
206 LyXErr & operator<<(LyXErr &, std::ios_base &(*)(std::ios_base &));
207
208 extern LyXErr lyxerr;
209
210 } // namespace lyx
211
212 #if USE_BOOST_CURRENT_FUNCTION
213 #       include <boost/current_function.hpp>
214 #       define CURRENT_POSITION BOOST_CURRENT_FUNCTION ": "
215 #else
216 # define CURRENT_POSITION __FILE__ << "(" << __LINE__ << "): "
217 #endif
218
219 #define LYXERR(type, msg) \
220         do { \
221                 if (!lyx::lyxerr.debugging(type)) {} \
222                 else { lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); } \
223         } while (0)
224
225 #define LYXERR0(msg) \
226         do { \
227                 lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); \
228         } while (0)
229
230
231 #endif