]> git.lyx.org Git - lyx.git/blob - src/support/debug.h
4e62bcd5c1d1990aa52b22bec00d7367f8884a86
[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 }
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), enabled_(true), second_enabled_(false) {}
146         
147         /// Disable the stream completely
148         void disable();
149         /// Enable the stream after a possible call of disable()
150         void enable();
151
152         /// Ends output
153         void endl();
154
155         /// Returns stream
156         std::ostream & stream() { return *stream_; }
157         /// Returns stream
158         operator std::ostream &() { return *stream_; }
159         /// Sets stream
160         void setStream(std::ostream & os) { stream_ = &os; }
161         /// Is the stream enabled ?
162         bool enabled() const { return enabled_; }
163
164         /// Returns second stream
165         std::ostream & secondStream() { return *second_stream_; }
166         /// Sets second stream
167         void setSecondStream(std::ostream * os) 
168                 { second_enabled_ = (second_stream_ = os); }
169         /// Is the second stream is enabled?
170         bool secondEnabled() { return second_enabled_; }
171
172         /// Sets the debug level
173         void setLevel(Debug::Type t) { dt_ = t; }
174         /// Returns the current debug level
175         Debug::Type level() const { return dt_; }
176         /// Returns true if t is part of the current debug level
177         bool debugging(Debug::Type t = Debug::ANY) const;
178
179         ///
180         static char const * stripName(char const *);
181
182 private:
183         /// The current debug level
184         Debug::Type dt_;
185         /// The real stream
186         std::ostream * stream_;
187         /// Is the stream enabled?
188         bool enabled_;
189         /// Next stream for output duplication
190         std::ostream * second_stream_;
191         /// Is the second stream enabled?
192         bool second_enabled_;
193 };
194
195 namespace support { class FileName; }
196
197 LyXErr & operator<<(LyXErr &, void const *);
198 LyXErr & operator<<(LyXErr &, char const *);
199 LyXErr & operator<<(LyXErr &, char);
200 LyXErr & operator<<(LyXErr &, int);
201 LyXErr & operator<<(LyXErr &, unsigned int);
202 LyXErr & operator<<(LyXErr &, long);
203 LyXErr & operator<<(LyXErr &, unsigned long);
204 LyXErr & operator<<(LyXErr &, double);
205 LyXErr & operator<<(LyXErr &, std::string const &);
206 LyXErr & operator<<(LyXErr &, docstring const &);
207 LyXErr & operator<<(LyXErr &, support::FileName const &);
208 LyXErr & operator<<(LyXErr &, std::ostream &(*)(std::ostream &));
209 LyXErr & operator<<(LyXErr &, std::ios_base &(*)(std::ios_base &));
210
211 extern LyXErr lyxerr;
212
213 } // namespace lyx
214
215 #if USE_BOOST_CURRENT_FUNCTION
216 #       include <boost/current_function.hpp>
217 #       define CURRENT_POSITION BOOST_CURRENT_FUNCTION ": "
218 #else
219 # define CURRENT_POSITION lyx::LyXErr::stripName(__FILE__) << " (" << __LINE__ << "): "
220 #endif
221
222 #define LYXERR(type, msg) \
223         do { \
224                 if (!lyx::lyxerr.debugging(type)) {} \
225                 else { lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); } \
226         } while (0)
227
228 #define LYXERR0(msg) \
229         do { \
230                 lyx::lyxerr << CURRENT_POSITION << msg; lyx::lyxerr.endl(); \
231         } while (0)
232
233
234 #endif