]> git.lyx.org Git - features.git/blob - src/support/debug.h
FindAdv: Amend d09f5ce1: Added new debug level :findverbose
[features.git] / src / support / debug.h
1 // -*- C++ -*-
2 /**
3  * \file debug.h
4  *
5  * This file is part of LyX, the document processor.
6  * Licence details can be found in the file COPYING.
7  *
8  * \author Lars Gullik Bjønnes
9  * \author Jean-Marc Lasgouttes
10  * \author Pavel Sanda
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef LYXDEBUG_H
16 #define LYXDEBUG_H
17
18 #include "support/strfwd.h"
19
20 // Forward definitions do not work with libc++
21 // but ios_base has already been defined in strfwd
22 // if compiling with it
23 #ifndef USE_LLVM_LIBCPP
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 } // namespace std
32 #endif
33
34
35 // Make sure at compile time that sizeof(unsigned long long) >= 8
36 typedef char p__LINE__[ (sizeof(unsigned long long) > 7) ? 1 : -1];
37
38 namespace lyx {
39
40 ///  This is all the different debug levels that we have.
41 namespace Debug {
42         ///
43         typedef unsigned long long base_type;
44         enum Type : base_type {
45                 ///
46                 NONE = 0,
47                 ///
48                 INFO       = (1u << 0),   // 1
49                 ///
50                 INIT       = (1u << 1),   // 2
51                 ///
52                 KEY        = (1u << 2),   // 4
53                 ///
54                 GUI        = (1u << 3),   // 8
55                 ///
56                 PARSER     = (1u << 4),   // 16
57                 ///
58                 LYXRC      = (1u << 5),   // 32
59                 ///
60                 KBMAP      = (1u << 6),   // 64
61                 ///
62                 OUTFILE      = (1u << 7),   // 128
63                 ///
64                 MATHED     = (1u << 8),   // 256 // Alejandro, please use this.
65                 ///
66                 FONT       = (1u << 9),   // 512
67                 ///
68                 TCLASS     = (1u << 10),  // 1024
69                 ///
70                 LYXVC      = (1u << 11),  // 2048
71                 ///
72                 LYXSERVER  = (1u << 12),  // 4096
73                 ///
74                 UNDO       = (1u << 13),  // 8192
75                 ///
76                 ACTION     = (1u << 14),   // 16384
77                 ///
78                 LYXLEX     = (1u << 15),
79                 ///
80                 DEPEND     = (1u << 16),
81                 ///
82                 INSETS     = (1u << 17),
83                 ///
84                 FILES      = (1u << 18),
85                 ///
86                 WORKAREA   = (1u << 19),
87                 ///
88                 CLIPBOARD  = (1u << 20),
89                 ///
90                 GRAPHICS   = (1u << 21),
91                 /// change tracking
92                 CHANGES    = (1u << 22),
93                 ///
94                 EXTERNAL   = (1u << 23),
95                 ///
96                 PAINTING   = (1u << 24),
97                 ///
98                 SCROLLING  = (1u << 25),
99                 ///
100                 MACROS     = (1u << 26),
101                 ///     rtl-related
102                 RTL        = (1u << 27),
103                 ///     locale related
104                 LOCALE     = (1u << 28),
105                 ///     selection
106                 SELECTION  = (1u << 29),
107                 /// Find and Replace
108                 FIND       = (1u << 30),
109                 ///
110                 FINDVERBOSE= (1u << 31),
111                 ///
112                 DEBUG      = (1ULL << 32),
113                 ///
114                 ANY = 0x1ffffffff
115         };
116
117         /// Return number of levels
118         int levelCount();
119
120         /// A function to convert debug level string names numerical values
121         Type value(std::string const & val);
122
123         /// Check the validity of debug level names
124         /// \return the first bad level name
125         std::string badValue(std::string const & val);
126
127         /// A function to convert index of level to their numerical value
128         Type value(int val);
129
130         /// Return description of level
131         std::string const description(Type val);
132
133         /// Return name of level
134         std::string const name(Type val);
135
136         /// Display the tags and descriptions of the current debug level
137         void showLevel(std::ostream & os, Type level);
138
139         /// Show all the possible tags that can be used for debugging
140         void showTags(std::ostream & os);
141
142
143 } // namespace Debug
144
145
146 inline void operator|=(Debug::Type & d1, Debug::Type d2)
147 {
148         d1 = static_cast<Debug::Type>(d1 | d2);
149 }
150
151
152 class LyXErr
153 {
154 public:
155         LyXErr(): dt_(Debug::NONE), stream_(0), enabled_(true),
156                   second_stream_(0), second_enabled_(false) {}
157
158         /// Disable the stream completely
159         void disable();
160         /// Enable the stream after a possible call of disable()
161         void enable();
162
163         /// Ends output
164         void endl();
165
166         /// Returns stream
167         std::ostream & stream() { return *stream_; }
168         /// Returns stream
169         operator std::ostream &() { return *stream_; }
170         /// Sets stream
171         void setStream(std::ostream & os) { stream_ = &os; }
172         /// Is the stream enabled ?
173         bool enabled() const { return enabled_; }
174
175         /// Returns second stream
176         std::ostream & secondStream() { return *second_stream_; }
177         /// Sets second stream
178         void setSecondStream(std::ostream * os)
179                 { second_enabled_ = (second_stream_ = os); }
180         /// Is the second stream is enabled?
181         bool secondEnabled() const { return second_enabled_; }
182
183         /// Sets the debug level
184         void setLevel(Debug::Type t) { dt_ = t; }
185         /// Returns the current debug level
186         Debug::Type level() const { return dt_; }
187         /// Returns true if t is part of the current debug level
188         bool debugging(Debug::base_type t = Debug::ANY) const;
189
190         ///
191         static char const * stripName(char const *);
192
193 private:
194         /// The current debug level
195         Debug::Type dt_;
196         /// The real stream
197         std::ostream * stream_;
198         /// Is the stream enabled?
199         bool enabled_;
200         /// Next stream for output duplication
201         std::ostream * second_stream_;
202         /// Is the second stream enabled?
203         bool second_enabled_;
204 };
205
206 namespace support { class FileName; }
207
208 LyXErr & operator<<(LyXErr &, void const *);
209 LyXErr & operator<<(LyXErr &, char const *);
210 LyXErr & operator<<(LyXErr &, char);
211 LyXErr & operator<<(LyXErr &, int);
212 LyXErr & operator<<(LyXErr &, unsigned int);
213 LyXErr & operator<<(LyXErr &, long);
214 LyXErr & operator<<(LyXErr &, unsigned long);
215 #ifdef HAVE_LONG_LONG_INT
216 LyXErr & operator<<(LyXErr &, long long);
217 LyXErr & operator<<(LyXErr &, unsigned long long);
218 #endif
219 LyXErr & operator<<(LyXErr &, double);
220 LyXErr & operator<<(LyXErr &, std::string const &);
221 LyXErr & operator<<(LyXErr &, docstring const &);
222 LyXErr & operator<<(LyXErr &, support::FileName const &);
223 LyXErr & operator<<(LyXErr &, std::ostream &(*)(std::ostream &));
224 LyXErr & operator<<(LyXErr &, std::ios_base &(*)(std::ios_base &));
225
226 extern LyXErr lyxerr;
227
228 } // namespace lyx
229
230 #if USE__func__
231 #       define CURRENT_POSITION __func__ ": "
232 #else
233 # define CURRENT_POSITION lyx::LyXErr::stripName(__FILE__) << " (" << __LINE__ << "): "
234 #endif
235
236 #define LYXERR(type, msg) \
237         do { \
238                 if (!lyx::lyxerr.debugging(type)) {} \
239                 else { lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); } \
240         } while (0)
241
242 #define LYXERR_NOENDL(type, msg) \
243         do { \
244                 if (!lyx::lyxerr.debugging(type)) {} \
245                 else { lyx::lyxerr << CURRENT_POSITION << msg; } \
246         } while (0)
247
248 #define LYXERR_NOPOS(type, msg) \
249         do { \
250                 if (!lyx::lyxerr.debugging(type)) {} \
251                 else { lyx::lyxerr << msg; lyx::lyxerr.endl(); } \
252         } while (0)
253
254 #define LYXERR0(msg) \
255         do { \
256                 lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); \
257         } while (0)
258
259
260 #endif