]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
Merge branch 'master' into features/indexmacros
[lyx.git] / src / support / debug.cpp
1 /**
2  * \file debug.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Pavel Sanda
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/debug.h"
16
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"
22
23 #include <iostream>
24 #include <iomanip>
25
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32 namespace {
33
34 struct DebugErrorItem {
35         Debug::Type level;
36         char const * name;
37         char const * desc;
38 };
39
40
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")}
78 };
79
80
81 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
82
83 } // namespace
84
85
86 int Debug::levelCount()
87 {
88         return numErrorTags;
89 }
90
91
92 Debug::Type Debug::value(int idx)
93 {
94         if (idx > 0 && idx < numErrorTags)
95                 return errorTags[idx].level;
96         return Debug::NONE;
97 }
98
99
100 string const Debug::description(Debug::Type val)
101 {
102         for (const auto & errorTag : errorTags) {
103                 if (errorTag.level == val)
104                         return errorTag.desc;
105         }
106         return "unknown level";
107 }
108
109
110 string const Debug::name(Debug::Type val)
111 {
112         for (const auto & errorTag : errorTags) {
113                 if (errorTag.level == val)
114                         return errorTag.name;
115         }
116         return "unknown level";
117 }
118
119
120 Debug::Type Debug::value(string const & val)
121 {
122         Type l = Debug::NONE;
123         string v = val;
124         while (!v.empty()) {
125                 size_t const st = v.find(',');
126                 string const tmp = ascii_lowercase(v.substr(0, st));
127                 if (tmp.empty())
128                         break;
129                 // Is it a number?
130                 if (isStrInt(tmp))
131                         l |= static_cast<Type>(convert<int>(tmp));
132                 else
133                         // Search for an explicit name
134                         for (DebugErrorItem const & item : errorTags)
135                                 if (tmp == item.name) {
136                                         l |= item.level;
137                                         break;
138                                 }
139                 if (st == string::npos)
140                         break;
141                 v.erase(0, st + 1);
142         }
143         return l;
144 }
145
146
147 string Debug::badValue(string const & val)
148 {
149         string v = val;
150         while (!v.empty()) {
151                 size_t const st = v.find(',');
152                 string const tmp = ascii_lowercase(v.substr(0, st));
153                 if (tmp.empty())
154                         break;
155                 // Is it a number?
156                 if (!tmp.empty() && !isStrInt(tmp)) {
157                         // Search for an explicit name
158                         bool found = false;
159                         for (DebugErrorItem const & item : errorTags)
160                                 if (tmp == item.name) {
161                                         found = true;
162                                         break;
163                                 }
164                         if (!found)
165                                 return tmp;
166                 }
167                 if (st == string::npos)
168                         break;
169                 v.erase(0, st + 1);
170         }
171         return empty_string();
172 }
173
174
175 void Debug::showLevel(ostream & os, Debug::Type level)
176 {
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))
186                            << '\n';
187                 }
188         }
189         os.flush();
190 }
191
192
193 void Debug::showTags(ostream & os)
194 {
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';
199         os.flush();
200 }
201
202
203 void LyXErr::disable()
204 {
205         enabled_ = false;
206 }
207
208
209 void LyXErr::enable()
210 {
211         enabled_ = true;
212 }
213
214
215 bool LyXErr::debugging(Debug::Type t) const
216 {
217         return (dt_ & t);
218 }
219
220
221 void LyXErr::endl()
222 {
223         if (enabled_) {
224                 stream() << std::endl;
225                 if (second_enabled_)
226                         secondStream() << std::endl;
227         }
228 }
229
230
231 char const * LyXErr::stripName(char const * n)
232 {
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)
239                 return n;
240         else
241                 return n + pos + 5;
242 }
243
244
245 // It seems not possible to instantiate operator template out of class body
246 template<class T>
247 LyXErr & toStream(LyXErr & l, T t)
248 {
249         if (l.enabled()){
250                 l.stream() << t;
251                 if (l.secondEnabled()) {
252                         l.secondStream() << t;
253                         ProgressInterface::instance()->lyxerrFlush();
254                 }
255         }
256         return l;
257 }
258
259
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); }
279 #endif
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); }
292
293
294 // The global instance
295 LyXErr lyxerr;
296
297
298
299 } // namespace lyx