]> git.lyx.org Git - features.git/blob - src/support/debug.cpp
Make all a synonym of any in cmd debug mode
[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::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 (int i = 0 ; i < numErrorTags ; ++i)
134                         if (tmp == errorTags[i].name) {
135                                 l |= errorTags[i].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 void Debug::showLevel(ostream & os, Debug::Type level)
147 {
148         // Show what features are traced
149         for (int i = 0; i < numErrorTags; ++i) {
150                 if (errorTags[i].level != Debug::ANY
151                       && errorTags[i].level != Debug::NONE
152                       && errorTags[i].level & level) {
153                         // avoid to_utf8(_(...)) re-entrance problem
154                         docstring const s = _(errorTags[i].desc);
155                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
156                                         from_utf8(errorTags[i].name), s))
157                            << '\n';
158                 }
159         }
160         os.flush();
161 }
162
163
164 void Debug::showTags(ostream & os)
165 {
166         for (int i = 0; i != numErrorTags ; ++i)
167                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
168                    << setw(13) << errorTags[i].name
169                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
170         os.flush();
171 }
172
173
174 void LyXErr::disable()
175 {
176         enabled_ = false;
177 }
178
179
180 void LyXErr::enable()
181 {
182         enabled_ = true;
183 }
184
185
186 bool LyXErr::debugging(Debug::Type t) const
187 {
188         return (dt_ & t);
189 }
190
191
192 void LyXErr::endl()
193 {
194         if (enabled_) {
195                 stream() << std::endl;
196                 if (second_enabled_)
197                         secondStream() << std::endl;
198         }
199 }
200
201
202 char const * LyXErr::stripName(char const * n)
203 {
204         string const name = n;
205         // find the last occurrence of /src/ in name
206         size_t pos = name.rfind("/src/");
207         if (pos == string::npos)
208                 pos = name.rfind("\\src\\");
209         if (pos == string::npos)
210                 return n;
211         else
212                 return n + pos + 5;
213 }
214
215
216 // It seems not possible to instantiate operator template out of class body
217 template<class T>
218 LyXErr & toStream(LyXErr & l, T t)
219 {
220         if (l.enabled()){
221                 l.stream() << t;
222                 if (l.secondEnabled()) {
223                         l.secondStream() << t;
224                         ProgressInterface::instance()->lyxerrFlush();
225                 }
226         }
227         return l;
228 }
229
230
231 LyXErr & operator<<(LyXErr & l, void const * t)
232 { return toStream(l, t); }
233 LyXErr & operator<<(LyXErr & l, char const * t)
234 { return toStream(l, t); }
235 LyXErr & operator<<(LyXErr & l, char t)
236 { return toStream(l, t); }
237 LyXErr & operator<<(LyXErr & l, int t)
238 { return toStream(l, t); }
239 LyXErr & operator<<(LyXErr & l, unsigned int t)
240 { return toStream(l, t); }
241 LyXErr & operator<<(LyXErr & l, long t)
242 { return toStream(l, t); }
243 LyXErr & operator<<(LyXErr & l, unsigned long t)
244 { return toStream(l, t); }
245 #ifdef HAVE_LONG_LONG_INT
246 LyXErr & operator<<(LyXErr & l, long long t)
247 { return toStream(l, t); }
248 LyXErr & operator<<(LyXErr & l, unsigned long long t)
249 { return toStream(l, t); }
250 #endif
251 LyXErr & operator<<(LyXErr & l, double t)
252 { return toStream(l, t); }
253 LyXErr & operator<<(LyXErr & l, string const & t)
254 { return toStream(l, t); }
255 LyXErr & operator<<(LyXErr & l, docstring const & t)
256 { return toStream(l, to_utf8(t)); }
257 LyXErr & operator<<(LyXErr & l, FileName const & t)
258 { return toStream(l, t); }
259 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
260 { return toStream(l, t); }
261 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
262 { return toStream(l, t); }
263
264
265 // The global instance
266 LyXErr lyxerr;
267
268
269
270 } // namespace lyx