3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
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"
25 //#define LYX_CALLSTACK_PRINTING
26 #ifdef LYX_CALLSTACK_PRINTING
35 using namespace lyx::support;
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")}
86 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
91 int Debug::levelCount()
97 Debug::Type Debug::value(int idx)
99 if (idx > 0 && idx < numErrorTags)
100 return errorTags[idx].level;
105 string const Debug::description(Debug::Type val)
107 for (int i = 0 ; i < numErrorTags ; ++i) {
108 if (errorTags[i].level == val)
109 return errorTags[i].desc;
111 return "unknown level";
115 string const Debug::name(Debug::Type val)
117 for (int i = 0 ; i < numErrorTags ; ++i) {
118 if (errorTags[i].level == val)
119 return errorTags[i].name;
121 return "unknown level";
125 Debug::Type Debug::value(string const & val)
127 Type l = Debug::NONE;
130 size_t const st = v.find(',');
131 string const tmp = ascii_lowercase(v.substr(0, st));
136 l |= static_cast<Type>(convert<int>(tmp));
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;
144 if (st == string::npos)
152 void Debug::showLevel(ostream & os, Debug::Type level)
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))
170 void Debug::showTags(ostream & os)
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';
180 void LyXErr::disable()
186 void LyXErr::enable()
192 bool LyXErr::debugging(Debug::Type t) const
201 stream() << std::endl;
203 secondStream() << std::endl;
208 // It seems not possible to instantiate operator template out of class body
210 LyXErr & toStream(LyXErr & l, T t)
214 if (l.secondEnabled()) {
215 l.secondStream() << t;
216 ProgressInterface::instance()->lyxerrFlush();
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); }
251 // The global instance
255 void Debug::printCallStack()
257 #ifdef LYX_CALLSTACK_PRINTING
258 const int depth = 50;
260 // get void*'s for all entries on the stack
262 size_t size = backtrace(array, depth);
264 char** messages = backtrace_symbols(array, size);
266 for (size_t i = 0; i < size && messages != NULL; i++) {
267 fprintf(stderr, "[LyX's bt]: (%d) %s\n", i, messages[i]);