]> git.lyx.org Git - features.git/blob - src/support/debug.cpp
4b3219142eceab696dba64660821a3e3a540f534
[features.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, 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")}
79 };
80
81
82 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
83
84 } // namespace
85
86
87 int Debug::levelCount()
88 {
89         return numErrorTags;
90 }
91
92
93 Debug::Type Debug::value(int idx)
94 {
95         if (idx > 0 && idx < numErrorTags)
96                 return errorTags[idx].level;
97         return Debug::NONE;
98 }
99
100
101 string const Debug::description(Debug::Type val)
102 {
103         for (const auto & errorTag : errorTags) {
104                 if (errorTag.level == val)
105                         return errorTag.desc;
106         }
107         return "unknown level";
108 }
109
110
111 string const Debug::name(Debug::Type val)
112 {
113         for (const auto & errorTag : errorTags) {
114                 if (errorTag.level == val)
115                         return errorTag.name;
116         }
117         return "unknown level";
118 }
119
120
121 string const Debug::realName(int idx)
122 {
123         if (idx < numErrorTags)
124                 return errorTags[idx].name;
125         return "unknown index";
126 }
127
128
129 Debug::Type Debug::value(string const & val)
130 {
131         Type l = Debug::NONE;
132         string v = val;
133         while (!v.empty()) {
134                 size_t const st = v.find(',');
135                 string const tmp = ascii_lowercase(v.substr(0, st));
136                 if (tmp.empty())
137                         break;
138                 // Is it a number?
139                 if (isStrInt(tmp))
140                         l |= static_cast<Type>(convert<unsigned long long>(tmp));
141                 else
142                         // Search for an explicit name
143                         for (DebugErrorItem const & item : errorTags)
144                                 if (tmp == item.name) {
145                                         l |= item.level;
146                                         break;
147                                 }
148                 if (st == string::npos)
149                         break;
150                 v.erase(0, st + 1);
151         }
152         return l;
153 }
154
155
156 string Debug::badValue(string const & val)
157 {
158         string v = val;
159         while (!v.empty()) {
160                 size_t const st = v.find(',');
161                 string const tmp = ascii_lowercase(v.substr(0, st));
162                 if (tmp.empty())
163                         break;
164                 // Is it a number?
165                 if (!tmp.empty() && !isStrInt(tmp)) {
166                         // Search for an explicit name
167                         bool found = false;
168                         for (DebugErrorItem const & item : errorTags)
169                                 if (tmp == item.name) {
170                                         found = true;
171                                         break;
172                                 }
173                         if (!found)
174                                 return tmp;
175                 }
176                 if (st == string::npos)
177                         break;
178                 v.erase(0, st + 1);
179         }
180         return empty_string();
181 }
182
183
184 void Debug::showLevel(ostream & os, Debug::Type level)
185 {
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))
195                            << '\n';
196                 }
197         }
198         os.flush();
199 }
200
201
202 void Debug::showTags(ostream & os)
203 {
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';
208         os.flush();
209 }
210
211
212 void LyXErr::disable()
213 {
214         enabled_ = false;
215 }
216
217
218 void LyXErr::enable()
219 {
220         enabled_ = true;
221 }
222
223
224 bool LyXErr::debugging(Debug::base_type t) const
225 {
226         return (dt_ & t);
227 }
228
229
230 void LyXErr::endl()
231 {
232         if (enabled_) {
233                 stream() << std::endl;
234                 if (second_enabled_)
235                         secondStream() << std::endl;
236         }
237 }
238
239
240 char const * LyXErr::stripName(char const * n)
241 {
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)
248                 return n;
249         else
250                 return n + pos + 5;
251 }
252
253
254 // It seems not possible to instantiate operator template out of class body
255 template<class T>
256 LyXErr & toStream(LyXErr & l, T t)
257 {
258         if (l.enabled()){
259                 l.stream() << t;
260                 if (l.secondEnabled()) {
261                         l.secondStream() << t;
262                         ProgressInterface::instance()->lyxerrFlush();
263                 }
264         }
265         return l;
266 }
267
268
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); }
288 #endif
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); }
301
302
303 // The global instance
304 LyXErr lyxerr;
305
306
307
308 } // namespace lyx