]> git.lyx.org Git - lyx.git/blob - src/support/debug.h
Update shortcuts in fr.po
[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 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                 CLIPBOARD  = (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         /// Return number of levels
112         int levelCount();
113
114         /// A function to convert debug level string names numerical values
115         Type value(std::string const & val);
116
117         /// A function to convert index of level to their numerical value
118         Type value(int val);
119
120         /// Return description of level
121         std::string const description(Type val);
122
123         /// Return name of level
124         std::string const name(Type val);
125
126         /// Display the tags and descriptions of the current debug level
127         void showLevel(std::ostream & os, Type level);
128
129         /// Show all the possible tags that can be used for debugging
130         void showTags(std::ostream & os);
131
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(): dt_(Debug::NONE), stream_(0), enabled_(true),
146                   second_stream_(0), second_enabled_(false) {}
147
148         /// Disable the stream completely
149         void disable();
150         /// Enable the stream after a possible call of disable()
151         void enable();
152
153         /// Ends output
154         void endl();
155
156         /// Returns stream
157         std::ostream & stream() { return *stream_; }
158         /// Returns stream
159         operator std::ostream &() { return *stream_; }
160         /// Sets stream
161         void setStream(std::ostream & os) { stream_ = &os; }
162         /// Is the stream enabled ?
163         bool enabled() const { return enabled_; }
164
165         /// Returns second stream
166         std::ostream & secondStream() { return *second_stream_; }
167         /// Sets second stream
168         void setSecondStream(std::ostream * os)
169                 { second_enabled_ = (second_stream_ = os); }
170         /// Is the second stream is enabled?
171         bool secondEnabled() { return second_enabled_; }
172
173         /// Sets the debug level
174         void setLevel(Debug::Type t) { dt_ = t; }
175         /// Returns the current debug level
176         Debug::Type level() const { return dt_; }
177         /// Returns true if t is part of the current debug level
178         bool debugging(Debug::Type t = Debug::ANY) const;
179
180         ///
181         static char const * stripName(char const *);
182
183 private:
184         /// The current debug level
185         Debug::Type dt_;
186         /// The real stream
187         std::ostream * stream_;
188         /// Is the stream enabled?
189         bool enabled_;
190         /// Next stream for output duplication
191         std::ostream * second_stream_;
192         /// Is the second stream enabled?
193         bool second_enabled_;
194 };
195
196 namespace support { class FileName; }
197
198 LyXErr & operator<<(LyXErr &, void const *);
199 LyXErr & operator<<(LyXErr &, char const *);
200 LyXErr & operator<<(LyXErr &, char);
201 LyXErr & operator<<(LyXErr &, int);
202 LyXErr & operator<<(LyXErr &, unsigned int);
203 LyXErr & operator<<(LyXErr &, long);
204 LyXErr & operator<<(LyXErr &, unsigned long);
205 #ifdef LYX_USE_LONG_LONG
206 LyXErr & operator<<(LyXErr &, long long);
207 LyXErr & operator<<(LyXErr &, unsigned long long);
208 #endif
209 LyXErr & operator<<(LyXErr &, double);
210 LyXErr & operator<<(LyXErr &, std::string const &);
211 LyXErr & operator<<(LyXErr &, docstring const &);
212 LyXErr & operator<<(LyXErr &, support::FileName const &);
213 LyXErr & operator<<(LyXErr &, std::ostream &(*)(std::ostream &));
214 LyXErr & operator<<(LyXErr &, std::ios_base &(*)(std::ios_base &));
215
216 extern LyXErr lyxerr;
217
218 } // namespace lyx
219
220 #if USE_BOOST_CURRENT_FUNCTION
221 #       include <boost/current_function.hpp>
222 #       define CURRENT_POSITION BOOST_CURRENT_FUNCTION ": "
223 #else
224 # define CURRENT_POSITION lyx::LyXErr::stripName(__FILE__) << " (" << __LINE__ << "): "
225 #endif
226
227 #define LYXERR(type, msg) \
228         do { \
229                 if (!lyx::lyxerr.debugging(type)) {} \
230                 else { lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); } \
231         } while (0)
232
233 #define LYXERR0(msg) \
234         do { \
235                 lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); \
236         } while (0)
237
238
239 #endif