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, terse version")},
75 { Debug::FINDVERBOSE,"findverbose", N_("Find and replace mechanism, verbose version")},
76 { Debug::DEBUG, "debug", N_("Developers' general debug messages")},
77 { Debug::ANY, "any", N_("All debugging messages")},
78 { Debug::ANY, "all", N_("All debugging messages")}
82 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
87 int Debug::levelCount()
93 Debug::Type Debug::value(int idx)
95 if (idx > 0 && idx < numErrorTags)
96 return errorTags[idx].level;
101 string const Debug::description(Debug::Type val)
103 for (const auto & errorTag : errorTags) {
104 if (errorTag.level == val)
105 return errorTag.desc;
107 return "unknown level";
111 string const Debug::name(Debug::Type val)
113 for (const auto & errorTag : errorTags) {
114 if (errorTag.level == val)
115 return errorTag.name;
117 return "unknown level";
121 Debug::Type Debug::value(string const & val)
123 Type l = Debug::NONE;
126 size_t const st = v.find(',');
127 string const tmp = ascii_lowercase(v.substr(0, st));
132 l |= static_cast<Type>(convert<unsigned long long>(tmp));
134 // Search for an explicit name
135 for (DebugErrorItem const & item : errorTags)
136 if (tmp == item.name) {
140 if (st == string::npos)
148 string Debug::badValue(string const & val)
152 size_t const st = v.find(',');
153 string const tmp = ascii_lowercase(v.substr(0, st));
157 if (!tmp.empty() && !isStrInt(tmp)) {
158 // Search for an explicit name
160 for (DebugErrorItem const & item : errorTags)
161 if (tmp == item.name) {
168 if (st == string::npos)
172 return empty_string();
176 void Debug::showLevel(ostream & os, Debug::Type level)
178 // Show what features are traced
179 for (DebugErrorItem const & item : errorTags) {
180 if (item.level != Debug::ANY
181 && item.level != Debug::NONE
182 && item.level & level) {
183 // avoid to_utf8(_(...)) re-entrance problem
184 docstring const s = _(item.desc);
185 os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
186 from_utf8(item.name), s))
194 void Debug::showTags(ostream & os)
196 for (DebugErrorItem const & item : errorTags)
197 os << setw(12) << static_cast<Debug::base_type>(item.level)
198 << setw(13) << item.name
199 << " " << to_utf8(_(item.desc)) << '\n';
204 void LyXErr::disable()
210 void LyXErr::enable()
216 bool LyXErr::debugging(Debug::base_type t) const
225 stream() << std::endl;
227 secondStream() << std::endl;
232 char const * LyXErr::stripName(char const * n)
234 string const name = n;
235 // find the last occurrence of /src/ in name
236 size_t pos = name.rfind("/src/");
237 if (pos == string::npos)
238 pos = name.rfind("\\src\\");
239 if (pos == string::npos)
246 // It seems not possible to instantiate operator template out of class body
248 LyXErr & toStream(LyXErr & l, T t)
252 if (l.secondEnabled()) {
253 l.secondStream() << t;
254 ProgressInterface::instance()->lyxerrFlush();
261 LyXErr & operator<<(LyXErr & l, void const * t)
262 { return toStream(l, t); }
263 LyXErr & operator<<(LyXErr & l, char const * t)
264 { return toStream(l, t); }
265 LyXErr & operator<<(LyXErr & l, char t)
266 { return toStream(l, t); }
267 LyXErr & operator<<(LyXErr & l, int t)
268 { return toStream(l, t); }
269 LyXErr & operator<<(LyXErr & l, unsigned int t)
270 { return toStream(l, t); }
271 LyXErr & operator<<(LyXErr & l, long t)
272 { return toStream(l, t); }
273 LyXErr & operator<<(LyXErr & l, unsigned long t)
274 { return toStream(l, t); }
275 #ifdef HAVE_LONG_LONG_INT
276 LyXErr & operator<<(LyXErr & l, long long t)
277 { return toStream(l, t); }
278 LyXErr & operator<<(LyXErr & l, unsigned long long t)
279 { return toStream(l, t); }
281 LyXErr & operator<<(LyXErr & l, double t)
282 { return toStream(l, t); }
283 LyXErr & operator<<(LyXErr & l, string const & t)
284 { return toStream(l, t); }
285 LyXErr & operator<<(LyXErr & l, docstring const & t)
286 { return toStream(l, to_utf8(t)); }
287 LyXErr & operator<<(LyXErr & l, FileName const & t)
288 { return toStream(l, t); }
289 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
290 { return toStream(l, t); }
291 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
292 { return toStream(l, t); }
295 // The global instance