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