]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
Let paragraph::requestSpellcheck() consider contained insets
[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::LATEX,     "latex",     N_("LaTeX generation/execution")},
51         { Debug::MATHED,    "mathed",    N_("Math editor")},
52         { Debug::FONT,      "font",      N_("Font handling")},
53         { Debug::TCLASS,    "tclass",    N_("Textclass files reading")},
54         { Debug::LYXVC,     "lyxvc",     N_("Version control")},
55         { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
56         { Debug::UNDO,      "undo",      N_("Undo/Redo mechanism")},
57         { Debug::ACTION,    "action",    N_("User commands")},
58         { Debug::LYXLEX,    "lyxlex",    N_("The LyX Lexer")},
59         { Debug::DEPEND,    "depend",    N_("Dependency information")},
60         { Debug::INSETS,    "insets",    N_("LyX Insets")},
61         { Debug::FILES,     "files",     N_("Files used by LyX")},
62         { Debug::WORKAREA,  "workarea",  N_("Workarea events")},
63         { Debug::CLIPBOARD, "clipboard", N_("Clipboard handling")},
64         { Debug::GRAPHICS,  "graphics",  N_("Graphics conversion and loading")},
65         { Debug::CHANGES,   "changes",   N_("Change tracking")},
66         { Debug::EXTERNAL,  "external",  N_("External template/inset messages")},
67         { Debug::PAINTING,  "painting",  N_("RowPainter profiling")},
68         { Debug::SCROLLING, "scrolling", N_("Scrolling debugging")},
69         { Debug::MACROS,    "macros",    N_("Math macros")},
70         { Debug::RTL,       "rtl",       N_("RTL/Bidi")},
71         { Debug::LOCALE,    "locale",    N_("Locale/Internationalisation")},
72         { Debug::SELECTION, "selection", N_("Selection copy/paste mechanism")},
73         { Debug::FIND,      "find",      N_("Find and replace mechanism")},
74         { Debug::DEBUG,     "debug",     N_("Developers' general debug messages")},
75         { Debug::ANY,       "any",       N_("All debugging messages")},
76         { Debug::ANY,       "all",       N_("All debugging messages")}
77 };
78
79
80 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
81
82 } // namespace
83
84
85 int Debug::levelCount()
86 {
87         return numErrorTags;
88 }
89
90
91 Debug::Type Debug::value(int idx)
92 {
93         if (idx > 0 && idx < numErrorTags)
94                 return errorTags[idx].level;
95         return Debug::NONE;
96 }
97
98
99 string const Debug::description(Debug::Type val)
100 {
101         for (int i = 0 ; i < numErrorTags ; ++i) {
102                 if (errorTags[i].level == val)
103                         return errorTags[i].desc;
104         }
105         return "unknown level";
106 }
107
108
109 string const Debug::name(Debug::Type val)
110 {
111         for (int i = 0 ; i < numErrorTags ; ++i) {
112                 if (errorTags[i].level == val)
113                         return errorTags[i].name;
114         }
115         return "unknown level";
116 }
117
118
119 Debug::Type Debug::value(string const & val)
120 {
121         Type l = Debug::NONE;
122         string v = val;
123         while (!v.empty()) {
124                 size_t const st = v.find(',');
125                 string const tmp = ascii_lowercase(v.substr(0, st));
126                 if (tmp.empty())
127                         break;
128                 // Is it a number?
129                 if (isStrInt(tmp))
130                         l |= static_cast<Type>(convert<int>(tmp));
131                 else
132                         // Search for an explicit name
133                         for (DebugErrorItem const & item : errorTags)
134                                 if (tmp == item.name) {
135                                         l |= item.level;
136                                         break;
137                                 }
138                 if (st == string::npos)
139                         break;
140                 v.erase(0, st + 1);
141         }
142         return l;
143 }
144
145
146 string Debug::badValue(string const & val)
147 {
148         string v = val;
149         while (!v.empty()) {
150                 size_t const st = v.find(',');
151                 string const tmp = ascii_lowercase(v.substr(0, st));
152                 if (tmp.empty())
153                         break;
154                 // Is it a number?
155                 if (!tmp.empty() && !isStrInt(tmp)) {
156                         // Search for an explicit name
157                         bool found = false;
158                         for (DebugErrorItem const & item : errorTags)
159                                 if (tmp == item.name) {
160                                         found = true;
161                                         break;
162                                 }
163                         if (!found)
164                                 return tmp;
165                 }
166                 if (st == string::npos)
167                         break;
168                 v.erase(0, st + 1);
169         }
170         return empty_string();
171 }
172
173
174 void Debug::showLevel(ostream & os, Debug::Type level)
175 {
176         // Show what features are traced
177         for (DebugErrorItem const & item : errorTags) {
178                 if (item.level != Debug::ANY
179                       && item.level != Debug::NONE
180                       && item.level & level) {
181                         // avoid to_utf8(_(...)) re-entrance problem
182                         docstring const s = _(item.desc);
183                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
184                                         from_utf8(item.name), s))
185                            << '\n';
186                 }
187         }
188         os.flush();
189 }
190
191
192 void Debug::showTags(ostream & os)
193 {
194         for (DebugErrorItem const & item : errorTags)
195                 os << setw(10) << static_cast<unsigned int>(item.level)
196                    << setw(13) << item.name
197                    << "  " << to_utf8(_(item.desc)) << '\n';
198         os.flush();
199 }
200
201
202 void LyXErr::disable()
203 {
204         enabled_ = false;
205 }
206
207
208 void LyXErr::enable()
209 {
210         enabled_ = true;
211 }
212
213
214 bool LyXErr::debugging(Debug::Type t) const
215 {
216         return (dt_ & t);
217 }
218
219
220 void LyXErr::endl()
221 {
222         if (enabled_) {
223                 stream() << std::endl;
224                 if (second_enabled_)
225                         secondStream() << std::endl;
226         }
227 }
228
229
230 char const * LyXErr::stripName(char const * n)
231 {
232         string const name = n;
233         // find the last occurrence of /src/ in name
234         size_t pos = name.rfind("/src/");
235         if (pos == string::npos)
236                 pos = name.rfind("\\src\\");
237         if (pos == string::npos)
238                 return n;
239         else
240                 return n + pos + 5;
241 }
242
243
244 // It seems not possible to instantiate operator template out of class body
245 template<class T>
246 LyXErr & toStream(LyXErr & l, T t)
247 {
248         if (l.enabled()){
249                 l.stream() << t;
250                 if (l.secondEnabled()) {
251                         l.secondStream() << t;
252                         ProgressInterface::instance()->lyxerrFlush();
253                 }
254         }
255         return l;
256 }
257
258
259 LyXErr & operator<<(LyXErr & l, void const * t)
260 { return toStream(l, t); }
261 LyXErr & operator<<(LyXErr & l, char const * t)
262 { return toStream(l, t); }
263 LyXErr & operator<<(LyXErr & l, char t)
264 { return toStream(l, t); }
265 LyXErr & operator<<(LyXErr & l, int t)
266 { return toStream(l, t); }
267 LyXErr & operator<<(LyXErr & l, unsigned int t)
268 { return toStream(l, t); }
269 LyXErr & operator<<(LyXErr & l, long t)
270 { return toStream(l, t); }
271 LyXErr & operator<<(LyXErr & l, unsigned long t)
272 { return toStream(l, t); }
273 #ifdef HAVE_LONG_LONG_INT
274 LyXErr & operator<<(LyXErr & l, long long t)
275 { return toStream(l, t); }
276 LyXErr & operator<<(LyXErr & l, unsigned long long t)
277 { return toStream(l, t); }
278 #endif
279 LyXErr & operator<<(LyXErr & l, double t)
280 { return toStream(l, t); }
281 LyXErr & operator<<(LyXErr & l, string const & t)
282 { return toStream(l, t); }
283 LyXErr & operator<<(LyXErr & l, docstring const & t)
284 { return toStream(l, to_utf8(t)); }
285 LyXErr & operator<<(LyXErr & l, FileName const & t)
286 { return toStream(l, t); }
287 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
288 { return toStream(l, t); }
289 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
290 { return toStream(l, t); }
291
292
293 // The global instance
294 LyXErr lyxerr;
295
296
297
298 } // namespace lyx