]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
11dd243e1ae0deecb1cac5f372d28610f5f1196f
[lyx.git] / src / support / debug.cpp
1 /**
2  * \file debug.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Pavel Sanda
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/convert.h"
16 #include "support/debug.h"
17 #include "support/gettext.h"
18 #include "support/lstrings.h"
19 #include "support/FileName.h"
20 #include "support/ProgressInterface.h"
21
22 #include <iostream>
23 #include <iomanip>
24
25 //#define LYX_CALLSTACK_PRINTING
26 #ifdef LYX_CALLSTACK_PRINTING
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <execinfo.h>
30 #endif
31
32
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 namespace {
40
41 struct ErrorItem {
42         Debug::Type level;
43         char const * name;
44         char const * desc;
45 };
46
47
48 ErrorItem errorTags[] = {
49         { Debug::NONE,      "none",      N_("No debugging messages")},
50         { Debug::INFO,      "info",      N_("General information")},
51         { Debug::INIT,      "init",      N_("Program initialisation")},
52         { Debug::KEY,       "key",       N_("Keyboard events handling")},
53         { Debug::GUI,       "gui",       N_("GUI handling")},
54         { Debug::PARSER,    "parser",    N_("Lyxlex grammar parser")},
55         { Debug::LYXRC,     "lyxrc",     N_("Configuration files reading")},
56         { Debug::KBMAP,     "kbmap",     N_("Custom keyboard definition")},
57         { Debug::LATEX,     "latex",     N_("LaTeX generation/execution")},
58         { Debug::MATHED,    "mathed",    N_("Math editor")},
59         { Debug::FONT,      "font",      N_("Font handling")},
60         { Debug::TCLASS,    "tclass",    N_("Textclass files reading")},
61         { Debug::LYXVC,     "lyxvc",     N_("Version control")},
62         { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
63         { Debug::UNDO,      "undo",      N_("Undo/Redo mechanism")},
64         { Debug::ACTION,    "action",    N_("User commands")},
65         { Debug::LYXLEX,    "lyxlex",    N_("The LyX Lexer")},
66         { Debug::DEPEND,    "depend",    N_("Dependency information")},
67         { Debug::INSETS,    "insets",    N_("LyX Insets")},
68         { Debug::FILES,     "files",     N_("Files used by LyX")},
69         { Debug::WORKAREA,  "workarea",  N_("Workarea events")},
70         { Debug::INSETTEXT, "insettext", N_("Insettext/tabular messages")},
71         { Debug::GRAPHICS,  "graphics",  N_("Graphics conversion and loading")},
72         { Debug::CHANGES,   "changes",   N_("Change tracking")},
73         { Debug::EXTERNAL,  "external",  N_("External template/inset messages")},
74         { Debug::PAINTING,  "painting",  N_("RowPainter profiling")},
75         { Debug::SCROLLING, "scrolling", N_("Scrolling debugging")},
76         { Debug::MACROS,    "macros",    N_("Math macros")},
77         { Debug::RTL,       "rtl",       N_("RTL/Bidi")},
78         { Debug::LOCALE,    "locale",    N_("Locale/Internationalisation")},
79         { Debug::SELECTION, "selection", N_("Selection copy/paste mechanism")},
80         { Debug::FIND,      "find",      N_("Find and replace mechanism")},
81         { Debug::DEBUG,     "debug",     N_("Developers' general debug messages")},
82         { Debug::ANY,       "any",       N_("All debugging messages")}
83 };
84
85
86 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
87
88 } // namespace anon
89
90
91 int Debug::levelCount()
92 {
93         return numErrorTags;
94 }
95
96
97 Debug::Type Debug::value(int idx)
98 {
99         if (idx > 0 && idx < numErrorTags)
100                 return errorTags[idx].level;
101         return Debug::NONE;
102 }
103
104
105 string const Debug::description(Debug::Type val)
106 {
107         for (int i = 0 ; i < numErrorTags ; ++i) {
108                 if (errorTags[i].level == val)
109                         return errorTags[i].desc;
110         }
111         return "unknown level";
112 }
113
114
115 string const Debug::name(Debug::Type val)
116 {
117         for (int i = 0 ; i < numErrorTags ; ++i) {
118                 if (errorTags[i].level == val)
119                         return errorTags[i].name;
120         }
121         return "unknown level";
122 }
123
124
125 Debug::Type Debug::value(string const & val)
126 {
127         Type l = Debug::NONE;
128         string v = val;
129         while (!v.empty()) {
130                 size_t const st = v.find(',');
131                 string const tmp = ascii_lowercase(v.substr(0, st));
132                 if (tmp.empty())
133                         break;
134                 // Is it a number?
135                 if (isStrInt(tmp))
136                         l |= static_cast<Type>(convert<int>(tmp));
137                 else
138                 // Search for an explicit name
139                 for (int i = 0 ; i < numErrorTags ; ++i)
140                         if (tmp == errorTags[i].name) {
141                                 l |= errorTags[i].level;
142                                 break;
143                         }
144                 if (st == string::npos)
145                 break;
146                 v.erase(0, st + 1);
147         }
148         return l;
149 }
150
151
152 void Debug::showLevel(ostream & os, Debug::Type level)
153 {
154         // Show what features are traced
155         for (int i = 0; i < numErrorTags; ++i) {
156                 if (errorTags[i].level != Debug::ANY
157                       && errorTags[i].level != Debug::NONE
158                       && errorTags[i].level & level) {
159                         // avoid to_utf8(_(...)) re-entrance problem
160                         docstring const s = _(errorTags[i].desc);
161                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
162                                         from_utf8(errorTags[i].name), s))
163                            << '\n';
164                 }
165         }
166         os.flush();
167 }
168
169
170 void Debug::showTags(ostream & os)
171 {
172         for (int i = 0; i != numErrorTags ; ++i)
173                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
174                    << setw(13) << errorTags[i].name
175                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
176         os.flush();
177 }
178
179
180 void LyXErr::disable()
181 {
182         enabled_ = false;
183 }
184
185
186 void LyXErr::enable()
187 {
188         enabled_ = true;
189 }
190
191
192 bool LyXErr::debugging(Debug::Type t) const
193 {
194         return (dt_ & t);
195 }
196
197
198 void LyXErr::endl()
199 {
200         if (enabled_) {
201                 stream() << std::endl;
202                 if (second_enabled_)
203                         secondStream() << std::endl;
204         }
205 }
206
207
208 // It seems not possible to instantiate operator template out of class body
209 template<class T>
210 LyXErr & toStream(LyXErr & l, T t)      
211 {
212         if (l.enabled()){
213                 l.stream() << t;
214                 if (l.secondEnabled()) {
215                         l.secondStream() << t;
216                         ProgressInterface::instance()->lyxerrFlush();
217                 }
218         }
219         return l;
220 }
221
222
223 LyXErr & operator<<(LyXErr & l, void const * t)
224 { return toStream(l, t); }
225 LyXErr & operator<<(LyXErr & l, char const * t)
226 { return toStream(l, t); }
227 LyXErr & operator<<(LyXErr & l, char t)
228 { return toStream(l, t); }
229 LyXErr & operator<<(LyXErr & l, int t)
230 { return toStream(l, t); }
231 LyXErr & operator<<(LyXErr & l, unsigned int t)
232 { return toStream(l, t); }
233 LyXErr & operator<<(LyXErr & l, long t)
234 { return toStream(l, t); }
235 LyXErr & operator<<(LyXErr & l, unsigned long t)
236 { return toStream(l, t); }
237 LyXErr & operator<<(LyXErr & l, double t)
238 { return toStream(l, t); }
239 LyXErr & operator<<(LyXErr & l, string const & t)
240 { return toStream(l, t); }
241 LyXErr & operator<<(LyXErr & l, docstring const & t)
242 { return toStream(l, to_utf8(t)); }
243 LyXErr & operator<<(LyXErr & l, FileName const & t)
244 { return toStream(l, t); }
245 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
246 { return toStream(l, t); }
247 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
248 { return toStream(l, t); }
249
250
251 // The global instance
252 LyXErr lyxerr;
253
254
255 void Debug::printCallStack()
256 {
257 #ifdef LYX_CALLSTACK_PRINTING
258         const int depth = 50;
259         
260         // get void*'s for all entries on the stack
261         void* array[depth];
262         size_t size = backtrace(array, depth);
263         
264         char** messages = backtrace_symbols(array, size);
265         
266         for (size_t i = 0; i < size && messages != NULL; i++) {
267                 fprintf(stderr, "[LyX's bt]: (%d) %s\n", i, messages[i]);
268         }
269 #endif
270 }
271
272
273 } // namespace lyx