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