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