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/debug.h"
17 #include "support/convert.h"
18 #include "support/FileName.h"
19 #include "support/gettext.h"
20 #include "support/lstrings.h"
21 #include "support/ProgressInterface.h"
28 using namespace lyx::support;
34 struct DebugErrorItem {
41 DebugErrorItem errorTags[] = {
42 { Debug::NONE, "none", N_("No debugging messages")},
43 { Debug::INFO, "info", N_("General information")},
44 { Debug::INIT, "init", N_("Program initialisation")},
45 { Debug::KEY, "key", N_("Keyboard events handling")},
46 { Debug::GUI, "gui", N_("GUI handling")},
47 { Debug::PARSER, "parser", N_("Lyxlex grammar parser")},
48 { Debug::LYXRC, "lyxrc", N_("Configuration files reading")},
49 { Debug::KBMAP, "kbmap", N_("Custom keyboard definition")},
50 { Debug::LATEX, "latex", N_("LaTeX generation/execution")},
51 { Debug::MATHED, "mathed", N_("Math editor")},
52 { Debug::FONT, "font", N_("Font handling")},
53 { Debug::TCLASS, "tclass", N_("Textclass files reading")},
54 { Debug::LYXVC, "lyxvc", N_("Version control")},
55 { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
56 { Debug::UNDO, "undo", N_("Undo/Redo mechanism")},
57 { Debug::ACTION, "action", N_("User commands")},
58 { Debug::LYXLEX, "lyxlex", N_("The LyX Lexer")},
59 { Debug::DEPEND, "depend", N_("Dependency information")},
60 { Debug::INSETS, "insets", N_("LyX Insets")},
61 { Debug::FILES, "files", N_("Files used by LyX")},
62 { Debug::WORKAREA, "workarea", N_("Workarea events")},
63 { Debug::CLIPBOARD, "clipboard", N_("Clipboard handling")},
64 { Debug::GRAPHICS, "graphics", N_("Graphics conversion and loading")},
65 { Debug::CHANGES, "changes", N_("Change tracking")},
66 { Debug::EXTERNAL, "external", N_("External template/inset messages")},
67 { Debug::PAINTING, "painting", N_("RowPainter profiling")},
68 { Debug::SCROLLING, "scrolling", N_("Scrolling debugging")},
69 { Debug::MACROS, "macros", N_("Math macros")},
70 { Debug::RTL, "rtl", N_("RTL/Bidi")},
71 { Debug::LOCALE, "locale", N_("Locale/Internationalisation")},
72 { Debug::SELECTION, "selection", N_("Selection copy/paste mechanism")},
73 { Debug::FIND, "find", N_("Find and replace mechanism")},
74 { Debug::DEBUG, "debug", N_("Developers' general debug messages")},
75 { Debug::ANY, "any", N_("All debugging messages")},
76 { Debug::ANY, "all", N_("All debugging messages")}
80 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
85 int Debug::levelCount()
91 Debug::Type Debug::value(int idx)
93 if (idx > 0 && idx < numErrorTags)
94 return errorTags[idx].level;
99 string const Debug::description(Debug::Type val)
101 for (int i = 0 ; i < numErrorTags ; ++i) {
102 if (errorTags[i].level == val)
103 return errorTags[i].desc;
105 return "unknown level";
109 string const Debug::name(Debug::Type val)
111 for (int i = 0 ; i < numErrorTags ; ++i) {
112 if (errorTags[i].level == val)
113 return errorTags[i].name;
115 return "unknown level";
119 Debug::Type Debug::value(string const & val)
121 Type l = Debug::NONE;
124 size_t const st = v.find(',');
125 string const tmp = ascii_lowercase(v.substr(0, st));
130 l |= static_cast<Type>(convert<int>(tmp));
132 // Search for an explicit name
133 for (DebugErrorItem const & item : errorTags)
134 if (tmp == item.name) {
138 if (st == string::npos)
146 string Debug::badValue(string const & val)
150 size_t const st = v.find(',');
151 string const tmp = ascii_lowercase(v.substr(0, st));
155 if (!tmp.empty() && !isStrInt(tmp)) {
156 // Search for an explicit name
158 for (DebugErrorItem const & item : errorTags)
159 if (tmp == item.name) {
166 if (st == string::npos)
170 return empty_string();
174 void Debug::showLevel(ostream & os, Debug::Type level)
176 // Show what features are traced
177 for (DebugErrorItem const & item : errorTags) {
178 if (item.level != Debug::ANY
179 && item.level != Debug::NONE
180 && item.level & level) {
181 // avoid to_utf8(_(...)) re-entrance problem
182 docstring const s = _(item.desc);
183 os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
184 from_utf8(item.name), s))
192 void Debug::showTags(ostream & os)
194 for (DebugErrorItem const & item : errorTags)
195 os << setw(10) << static_cast<unsigned int>(item.level)
196 << setw(13) << item.name
197 << " " << to_utf8(_(item.desc)) << '\n';
202 void LyXErr::disable()
208 void LyXErr::enable()
214 bool LyXErr::debugging(Debug::Type t) const
223 stream() << std::endl;
225 secondStream() << std::endl;
230 char const * LyXErr::stripName(char const * n)
232 string const name = n;
233 // find the last occurrence of /src/ in name
234 size_t pos = name.rfind("/src/");
235 if (pos == string::npos)
236 pos = name.rfind("\\src\\");
237 if (pos == string::npos)
244 // It seems not possible to instantiate operator template out of class body
246 LyXErr & toStream(LyXErr & l, T t)
250 if (l.secondEnabled()) {
251 l.secondStream() << t;
252 ProgressInterface::instance()->lyxerrFlush();
259 LyXErr & operator<<(LyXErr & l, void const * t)
260 { return toStream(l, t); }
261 LyXErr & operator<<(LyXErr & l, char const * t)
262 { return toStream(l, t); }
263 LyXErr & operator<<(LyXErr & l, char t)
264 { return toStream(l, t); }
265 LyXErr & operator<<(LyXErr & l, int t)
266 { return toStream(l, t); }
267 LyXErr & operator<<(LyXErr & l, unsigned int t)
268 { return toStream(l, t); }
269 LyXErr & operator<<(LyXErr & l, long t)
270 { return toStream(l, t); }
271 LyXErr & operator<<(LyXErr & l, unsigned long t)
272 { return toStream(l, t); }
273 #ifdef HAVE_LONG_LONG_INT
274 LyXErr & operator<<(LyXErr & l, long long t)
275 { return toStream(l, t); }
276 LyXErr & operator<<(LyXErr & l, unsigned long long t)
277 { return toStream(l, t); }
279 LyXErr & operator<<(LyXErr & l, double t)
280 { return toStream(l, t); }
281 LyXErr & operator<<(LyXErr & l, string const & t)
282 { return toStream(l, t); }
283 LyXErr & operator<<(LyXErr & l, docstring const & t)
284 { return toStream(l, to_utf8(t)); }
285 LyXErr & operator<<(LyXErr & l, FileName const & t)
286 { return toStream(l, t); }
287 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
288 { return toStream(l, t); }
289 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
290 { return toStream(l, t); }
293 // The global instance