]> git.lyx.org Git - lyx.git/blob - src/support/debug.h
Add _v_() debug helper maco
[lyx.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 from value. In case of aliases,
134         /// this returns the first entry found
135         std::string const name(Type val);
136
137         /// Return name of level from index, in case of aliases
138         /// this is unambiguous
139         std::string const realName(int i);
140
141         /// Display the tags and descriptions of the current debug level
142         void showLevel(std::ostream & os, Type level);
143
144         /// Show all the possible tags that can be used for debugging
145         void showTags(std::ostream & os);
146
147
148 } // namespace Debug
149
150
151 inline void operator|=(Debug::Type & d1, Debug::Type d2)
152 {
153         d1 = static_cast<Debug::Type>(d1 | d2);
154 }
155
156
157 class LyXErr
158 {
159 public:
160         LyXErr(): dt_(Debug::NONE), stream_(0), enabled_(true),
161                   second_stream_(0), second_enabled_(false) {}
162
163         /// Disable the stream completely
164         void disable();
165         /// Enable the stream after a possible call of disable()
166         void enable();
167
168         /// Ends output
169         void endl();
170
171         /// Returns stream
172         std::ostream & stream() { return *stream_; }
173         /// Returns stream
174         operator std::ostream &() { return *stream_; }
175         /// Sets stream
176         void setStream(std::ostream & os) { stream_ = &os; }
177         /// Is the stream enabled ?
178         bool enabled() const { return enabled_; }
179
180         /// Returns second stream
181         std::ostream & secondStream() { return *second_stream_; }
182         /// Sets second stream
183         void setSecondStream(std::ostream * os)
184                 { second_enabled_ = (second_stream_ = os); }
185         /// Is the second stream is enabled?
186         bool secondEnabled() const { return second_enabled_; }
187
188         /// Sets the debug level
189         void setLevel(Debug::Type t) { dt_ = t; }
190         /// Returns the current debug level
191         Debug::Type level() const { return dt_; }
192         /// Returns true if t is part of the current debug level
193         bool debugging(Debug::base_type t = Debug::ANY) const;
194
195         ///
196         static char const * stripName(char const *);
197
198 private:
199         /// The current debug level
200         Debug::Type dt_;
201         /// The real stream
202         std::ostream * stream_;
203         /// Is the stream enabled?
204         bool enabled_;
205         /// Next stream for output duplication
206         std::ostream * second_stream_;
207         /// Is the second stream enabled?
208         bool second_enabled_;
209 };
210
211 namespace support { class FileName; }
212
213 LyXErr & operator<<(LyXErr &, void const *);
214 LyXErr & operator<<(LyXErr &, char const *);
215 LyXErr & operator<<(LyXErr &, char);
216 LyXErr & operator<<(LyXErr &, int);
217 LyXErr & operator<<(LyXErr &, unsigned int);
218 LyXErr & operator<<(LyXErr &, long);
219 LyXErr & operator<<(LyXErr &, unsigned long);
220 #ifdef HAVE_LONG_LONG_INT
221 LyXErr & operator<<(LyXErr &, long long);
222 LyXErr & operator<<(LyXErr &, unsigned long long);
223 #endif
224 LyXErr & operator<<(LyXErr &, double);
225 LyXErr & operator<<(LyXErr &, std::string const &);
226 LyXErr & operator<<(LyXErr &, docstring const &);
227 LyXErr & operator<<(LyXErr &, support::FileName const &);
228 LyXErr & operator<<(LyXErr &, std::ostream &(*)(std::ostream &));
229 LyXErr & operator<<(LyXErr &, std::ios_base &(*)(std::ios_base &));
230
231 extern LyXErr lyxerr;
232
233 } // namespace lyx
234
235 #if USE__func__
236 #       define CURRENT_POSITION __func__ ": "
237 #else
238 # define CURRENT_POSITION lyx::LyXErr::stripName(__FILE__) << " (" << __LINE__ << "): "
239 #endif
240
241 #define LYXERR(type, msg) \
242         do { \
243                 if (!lyx::lyxerr.debugging(type)) {} \
244                 else { lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); } \
245         } while (0)
246
247 #define LYXERR_NOENDL(type, msg) \
248         do { \
249                 if (!lyx::lyxerr.debugging(type)) {} \
250                 else { lyx::lyxerr << CURRENT_POSITION << msg; } \
251         } while (0)
252
253 #define LYXERR_NOPOS(type, msg) \
254         do { \
255                 if (!lyx::lyxerr.debugging(type)) {} \
256                 else { lyx::lyxerr << msg; lyx::lyxerr.endl(); } \
257         } while (0)
258
259 #define LYXERR0(msg) \
260         do { \
261                 lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); \
262         } while (0)
263
264
265 /** Helper debug macro for quick and dirty logging. For example
266  *   int var = 2;
267  *   LYXERR0(_v_(var) << _v_(var + 1));
268  * yields
269  *   var=2 var + 1=3
270  * Not a great typesetting, but it is handy in debugging sessions.
271  */
272 #define _v_(var) #var"=" << var << " "
273
274 #endif