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
37 using namespace lyx::support;
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")}
88 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
93 int Debug::levelCount()
99 Debug::Type Debug::value(int idx)
101 if (idx > 0 && idx < numErrorTags)
102 return errorTags[idx].level;
107 string const Debug::description(Debug::Type val)
109 for (int i = 0 ; i < numErrorTags ; ++i) {
110 if (errorTags[i].level == val)
111 return errorTags[i].desc;
113 return "unknown level";
117 string const Debug::name(Debug::Type val)
119 for (int i = 0 ; i < numErrorTags ; ++i) {
120 if (errorTags[i].level == val)
121 return errorTags[i].name;
123 return "unknown level";
127 Debug::Type Debug::value(string const & val)
129 Type l = Debug::NONE;
132 size_t const st = v.find(',');
133 string const tmp = ascii_lowercase(v.substr(0, st));
138 l |= static_cast<Type>(convert<int>(tmp));
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;
146 if (st == string::npos)
154 void Debug::showLevel(ostream & os, Debug::Type level)
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))
172 void Debug::showTags(ostream & os)
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';
182 void LyXErr::disable()
188 void LyXErr::enable()
194 bool LyXErr::debugging(Debug::Type t) const
203 stream() << std::endl;
205 secondStream() << std::endl;
210 // It seems not possible to instantiate operator template out of class body
212 LyXErr & toStream(LyXErr & l, T t)
216 if (l.secondEnabled()) {
217 l.secondStream() << t;
218 ProgressInterface::instance()->lyxerrFlush();
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); }
253 // The global instance
257 void Debug::printCallStack()
259 #ifdef LYX_CALLSTACK_PRINTING
260 const int depth = 50;
262 // get void*'s for all entries on the stack
264 size_t size = backtrace(array, depth);
266 char** messages = backtrace_symbols(array, size);
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]
272 for (char *p = messages[i]; *p; ++p) {
276 } else if (*p == '+') {
282 char* demangled = abi::__cxa_demangle(mangled, 0, 0, &err);
284 fprintf(stderr, "[bt]: (%d) %s %s\n", i, messages[i], demangled);
285 free((void*)demangled);
287 fprintf(stderr, "[bt]: (%d) %s\n", i, orig.c_str());