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 // must be linked with -rdynamic
27 #ifdef LYX_CALLSTACK_PRINTING
36 using namespace lyx::support;
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")}
87 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
92 int Debug::levelCount()
98 Debug::Type Debug::value(int idx)
100 if (idx > 0 && idx < numErrorTags)
101 return errorTags[idx].level;
106 string const Debug::description(Debug::Type val)
108 for (int i = 0 ; i < numErrorTags ; ++i) {
109 if (errorTags[i].level == val)
110 return errorTags[i].desc;
112 return "unknown level";
116 string const Debug::name(Debug::Type val)
118 for (int i = 0 ; i < numErrorTags ; ++i) {
119 if (errorTags[i].level == val)
120 return errorTags[i].name;
122 return "unknown level";
126 Debug::Type Debug::value(string const & val)
128 Type l = Debug::NONE;
131 size_t const st = v.find(',');
132 string const tmp = ascii_lowercase(v.substr(0, st));
137 l |= static_cast<Type>(convert<int>(tmp));
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;
145 if (st == string::npos)
153 void Debug::showLevel(ostream & os, Debug::Type level)
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))
171 void Debug::showTags(ostream & os)
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';
181 void LyXErr::disable()
187 void LyXErr::enable()
193 bool LyXErr::debugging(Debug::Type t) const
202 stream() << std::endl;
204 secondStream() << std::endl;
209 // It seems not possible to instantiate operator template out of class body
211 LyXErr & toStream(LyXErr & l, T t)
215 if (l.secondEnabled()) {
216 l.secondStream() << t;
217 ProgressInterface::instance()->lyxerrFlush();
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); }
252 // The global instance
256 void Debug::printCallStack()
258 #ifdef LYX_CALLSTACK_PRINTING
259 const int depth = 50;
261 // get void*'s for all entries on the stack
263 size_t size = backtrace(array, depth);
265 char** messages = backtrace_symbols(array, size);
267 for (size_t i = 0; i < size && messages != NULL; i++) {
268 fprintf(stderr, "[LyX's bt]: (%d) %s\n", i, messages[i]);