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::OUTFILE, "outfile", N_("Output source file generation/processing")},
51 { Debug::OUTFILE, "latex", N_("Output source file generation/processing")},
52 { Debug::MATHED, "mathed", N_("Math editor")},
53 { Debug::FONT, "font", N_("Font handling")},
54 { Debug::TCLASS, "tclass", N_("Textclass files reading")},
55 { Debug::LYXVC, "lyxvc", N_("Version control")},
56 { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
57 { Debug::UNDO, "undo", N_("Undo/Redo mechanism")},
58 { Debug::ACTION, "action", N_("User commands")},
59 { Debug::LYXLEX, "lyxlex", N_("The LyX Lexer")},
60 { Debug::DEPEND, "depend", N_("Dependency information")},
61 { Debug::INSETS, "insets", N_("LyX Insets")},
62 { Debug::FILES, "files", N_("Files used by LyX")},
63 { Debug::WORKAREA, "workarea", N_("Workarea events")},
64 { Debug::CLIPBOARD, "clipboard", N_("Clipboard handling")},
65 { Debug::GRAPHICS, "graphics", N_("Graphics conversion and loading")},
66 { Debug::CHANGES, "changes", N_("Change tracking")},
67 { Debug::EXTERNAL, "external", N_("External template/inset messages")},
68 { Debug::PAINTING, "painting", N_("RowPainter profiling")},
69 { Debug::SCROLLING, "scrolling", N_("Scrolling debugging")},
70 { Debug::MACROS, "macros", N_("Math macros")},
71 { Debug::RTL, "rtl", N_("RTL/Bidi")},
72 { Debug::LOCALE, "locale", N_("Locale/Internationalisation")},
73 { Debug::SELECTION, "selection", N_("Selection copy/paste mechanism")},
74 { Debug::FIND, "find", N_("Find and replace mechanism")},
75 { Debug::DEBUG, "debug", N_("Developers' general debug messages")},
76 { Debug::ANY, "any", N_("All debugging messages")},
77 { Debug::ANY, "all", N_("All debugging messages")}
81 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
86 int Debug::levelCount()
92 Debug::Type Debug::value(int idx)
94 if (idx > 0 && idx < numErrorTags)
95 return errorTags[idx].level;
100 string const Debug::description(Debug::Type val)
102 for (const auto & errorTag : errorTags) {
103 if (errorTag.level == val)
104 return errorTag.desc;
106 return "unknown level";
110 string const Debug::name(Debug::Type val)
112 for (const auto & errorTag : errorTags) {
113 if (errorTag.level == val)
114 return errorTag.name;
116 return "unknown level";
120 Debug::Type Debug::value(string const & val)
122 Type l = Debug::NONE;
125 size_t const st = v.find(',');
126 string const tmp = ascii_lowercase(v.substr(0, st));
131 l |= static_cast<Type>(convert<int>(tmp));
133 // Search for an explicit name
134 for (DebugErrorItem const & item : errorTags)
135 if (tmp == item.name) {
139 if (st == string::npos)
147 string Debug::badValue(string const & val)
151 size_t const st = v.find(',');
152 string const tmp = ascii_lowercase(v.substr(0, st));
156 if (!tmp.empty() && !isStrInt(tmp)) {
157 // Search for an explicit name
159 for (DebugErrorItem const & item : errorTags)
160 if (tmp == item.name) {
167 if (st == string::npos)
171 return empty_string();
175 void Debug::showLevel(ostream & os, Debug::Type level)
177 // Show what features are traced
178 for (DebugErrorItem const & item : errorTags) {
179 if (item.level != Debug::ANY
180 && item.level != Debug::NONE
181 && item.level & level) {
182 // avoid to_utf8(_(...)) re-entrance problem
183 docstring const s = _(item.desc);
184 os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
185 from_utf8(item.name), s))
193 void Debug::showTags(ostream & os)
195 for (DebugErrorItem const & item : errorTags)
196 os << setw(10) << static_cast<unsigned int>(item.level)
197 << setw(13) << item.name
198 << " " << to_utf8(_(item.desc)) << '\n';
203 void LyXErr::disable()
209 void LyXErr::enable()
215 bool LyXErr::debugging(Debug::Type t) const
224 stream() << std::endl;
226 secondStream() << std::endl;
231 char const * LyXErr::stripName(char const * n)
233 string const name = n;
234 // find the last occurrence of /src/ in name
235 size_t pos = name.rfind("/src/");
236 if (pos == string::npos)
237 pos = name.rfind("\\src\\");
238 if (pos == string::npos)
245 // It seems not possible to instantiate operator template out of class body
247 LyXErr & toStream(LyXErr & l, T t)
251 if (l.secondEnabled()) {
252 l.secondStream() << t;
253 ProgressInterface::instance()->lyxerrFlush();
260 LyXErr & operator<<(LyXErr & l, void const * t)
261 { return toStream(l, t); }
262 LyXErr & operator<<(LyXErr & l, char const * t)
263 { return toStream(l, t); }
264 LyXErr & operator<<(LyXErr & l, char t)
265 { return toStream(l, t); }
266 LyXErr & operator<<(LyXErr & l, int t)
267 { return toStream(l, t); }
268 LyXErr & operator<<(LyXErr & l, unsigned int t)
269 { return toStream(l, t); }
270 LyXErr & operator<<(LyXErr & l, long t)
271 { return toStream(l, t); }
272 LyXErr & operator<<(LyXErr & l, unsigned long t)
273 { return toStream(l, t); }
274 #ifdef HAVE_LONG_LONG_INT
275 LyXErr & operator<<(LyXErr & l, long long t)
276 { return toStream(l, t); }
277 LyXErr & operator<<(LyXErr & l, unsigned long long t)
278 { return toStream(l, t); }
280 LyXErr & operator<<(LyXErr & l, double t)
281 { return toStream(l, t); }
282 LyXErr & operator<<(LyXErr & l, string const & t)
283 { return toStream(l, t); }
284 LyXErr & operator<<(LyXErr & l, docstring const & t)
285 { return toStream(l, to_utf8(t)); }
286 LyXErr & operator<<(LyXErr & l, FileName const & t)
287 { return toStream(l, t); }
288 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
289 { return toStream(l, t); }
290 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
291 { return toStream(l, t); }
294 // The global instance