]> git.lyx.org Git - features.git/blob - src/support/debug.cpp
FindAdv: Amend d09f5ce1: Added new debug level :findverbose
[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 Debug::Type Debug::value(string const & val)
122 {
123         Type l = Debug::NONE;
124         string v = val;
125         while (!v.empty()) {
126                 size_t const st = v.find(',');
127                 string const tmp = ascii_lowercase(v.substr(0, st));
128                 if (tmp.empty())
129                         break;
130                 // Is it a number?
131                 if (isStrInt(tmp))
132                         l |= static_cast<Type>(convert<unsigned long long>(tmp));
133                 else
134                         // Search for an explicit name
135                         for (DebugErrorItem const & item : errorTags)
136                                 if (tmp == item.name) {
137                                         l |= item.level;
138                                         break;
139                                 }
140                 if (st == string::npos)
141                         break;
142                 v.erase(0, st + 1);
143         }
144         return l;
145 }
146
147
148 string Debug::badValue(string const & val)
149 {
150         string v = val;
151         while (!v.empty()) {
152                 size_t const st = v.find(',');
153                 string const tmp = ascii_lowercase(v.substr(0, st));
154                 if (tmp.empty())
155                         break;
156                 // Is it a number?
157                 if (!tmp.empty() && !isStrInt(tmp)) {
158                         // Search for an explicit name
159                         bool found = false;
160                         for (DebugErrorItem const & item : errorTags)
161                                 if (tmp == item.name) {
162                                         found = true;
163                                         break;
164                                 }
165                         if (!found)
166                                 return tmp;
167                 }
168                 if (st == string::npos)
169                         break;
170                 v.erase(0, st + 1);
171         }
172         return empty_string();
173 }
174
175
176 void Debug::showLevel(ostream & os, Debug::Type level)
177 {
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))
187                            << '\n';
188                 }
189         }
190         os.flush();
191 }
192
193
194 void Debug::showTags(ostream & os)
195 {
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';
200         os.flush();
201 }
202
203
204 void LyXErr::disable()
205 {
206         enabled_ = false;
207 }
208
209
210 void LyXErr::enable()
211 {
212         enabled_ = true;
213 }
214
215
216 bool LyXErr::debugging(Debug::base_type t) const
217 {
218         return (dt_ & t);
219 }
220
221
222 void LyXErr::endl()
223 {
224         if (enabled_) {
225                 stream() << std::endl;
226                 if (second_enabled_)
227                         secondStream() << std::endl;
228         }
229 }
230
231
232 char const * LyXErr::stripName(char const * n)
233 {
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)
240                 return n;
241         else
242                 return n + pos + 5;
243 }
244
245
246 // It seems not possible to instantiate operator template out of class body
247 template<class T>
248 LyXErr & toStream(LyXErr & l, T t)
249 {
250         if (l.enabled()){
251                 l.stream() << t;
252                 if (l.secondEnabled()) {
253                         l.secondStream() << t;
254                         ProgressInterface::instance()->lyxerrFlush();
255                 }
256         }
257         return l;
258 }
259
260
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); }
280 #endif
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); }
293
294
295 // The global instance
296 LyXErr lyxerr;
297
298
299
300 } // namespace lyx