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