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 string const Debug::realName(int idx)
123 if (idx < numErrorTags)
124 return errorTags[idx].name;
125 return "unknown index";
129 Debug::Type Debug::value(string const & val)
131 Type l = Debug::NONE;
134 size_t const st = v.find(',');
135 string const tmp = ascii_lowercase(v.substr(0, st));
140 l |= static_cast<Type>(convert<unsigned long long>(tmp));
142 // Search for an explicit name
143 for (DebugErrorItem const & item : errorTags)
144 if (tmp == item.name) {
148 if (st == string::npos)
156 string Debug::badValue(string const & val)
160 size_t const st = v.find(',');
161 string const tmp = ascii_lowercase(v.substr(0, st));
165 if (!tmp.empty() && !isStrInt(tmp)) {
166 // Search for an explicit name
168 for (DebugErrorItem const & item : errorTags)
169 if (tmp == item.name) {
176 if (st == string::npos)
180 return empty_string();
184 void Debug::showLevel(ostream & os, Debug::Type level)
186 // Show what features are traced
187 for (DebugErrorItem const & item : errorTags) {
188 if (item.level != Debug::ANY
189 && item.level != Debug::NONE
190 && item.level & level) {
191 // avoid to_utf8(_(...)) re-entrance problem
192 docstring const s = _(item.desc);
193 os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
194 from_utf8(item.name), s))
202 void Debug::showTags(ostream & os)
204 for (DebugErrorItem const & item : errorTags)
205 os << setw(12) << static_cast<Debug::base_type>(item.level)
206 << setw(13) << item.name
207 << " " << to_utf8(_(item.desc)) << '\n';
212 void LyXErr::disable()
218 void LyXErr::enable()
224 bool LyXErr::debugging(Debug::base_type t) const
233 stream() << std::endl;
235 secondStream() << std::endl;
240 char const * LyXErr::stripName(char const * n)
242 string const name = n;
243 // find the last occurrence of /src/ in name
244 size_t pos = name.rfind("/src/");
245 if (pos == string::npos)
246 pos = name.rfind("\\src\\");
247 if (pos == string::npos)
254 // It seems not possible to instantiate operator template out of class body
256 LyXErr & toStream(LyXErr & l, T t)
260 if (l.secondEnabled()) {
261 l.secondStream() << t;
262 ProgressInterface::instance()->lyxerrFlush();
269 LyXErr & operator<<(LyXErr & l, void const * t)
270 { return toStream(l, t); }
271 LyXErr & operator<<(LyXErr & l, char const * t)
272 { return toStream(l, t); }
273 LyXErr & operator<<(LyXErr & l, char t)
274 { return toStream(l, t); }
275 LyXErr & operator<<(LyXErr & l, int t)
276 { return toStream(l, t); }
277 LyXErr & operator<<(LyXErr & l, unsigned int t)
278 { return toStream(l, t); }
279 LyXErr & operator<<(LyXErr & l, long t)
280 { return toStream(l, t); }
281 LyXErr & operator<<(LyXErr & l, unsigned long t)
282 { return toStream(l, t); }
283 #ifdef HAVE_LONG_LONG_INT
284 LyXErr & operator<<(LyXErr & l, long long t)
285 { return toStream(l, t); }
286 LyXErr & operator<<(LyXErr & l, unsigned long long t)
287 { return toStream(l, t); }
289 LyXErr & operator<<(LyXErr & l, double t)
290 { return toStream(l, t); }
291 LyXErr & operator<<(LyXErr & l, string const & t)
292 { return toStream(l, t); }
293 LyXErr & operator<<(LyXErr & l, docstring const & t)
294 { return toStream(l, to_utf8(t)); }
295 LyXErr & operator<<(LyXErr & l, FileName const & t)
296 { return toStream(l, t); }
297 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
298 { return toStream(l, t); }
299 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
300 { return toStream(l, t); }
303 // The global instance