]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
Yet another deprecation fix (this is the last one I am aware of)
[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/convert.h"
16 #include "support/debug.h"
17 #include "support/FileName.h"
18 #include "support/gettext.h"
19 #include "support/lstrings.h"
20 #include "support/ProgressInterface.h"
21
22 #include <iostream>
23 #include <iomanip>
24
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 namespace {
32
33 struct DebugErrorItem {
34         Debug::Type level;
35         char const * name;
36         char const * desc;
37 };
38
39
40 DebugErrorItem errorTags[] = {
41         { Debug::NONE,      "none",      N_("No debugging messages")},
42         { Debug::INFO,      "info",      N_("General information")},
43         { Debug::INIT,      "init",      N_("Program initialisation")},
44         { Debug::KEY,       "key",       N_("Keyboard events handling")},
45         { Debug::GUI,       "gui",       N_("GUI handling")},
46         { Debug::PARSER,    "parser",    N_("Lyxlex grammar parser")},
47         { Debug::LYXRC,     "lyxrc",     N_("Configuration files reading")},
48         { Debug::KBMAP,     "kbmap",     N_("Custom keyboard definition")},
49         { Debug::LATEX,     "latex",     N_("LaTeX generation/execution")},
50         { Debug::MATHED,    "mathed",    N_("Math editor")},
51         { Debug::FONT,      "font",      N_("Font handling")},
52         { Debug::TCLASS,    "tclass",    N_("Textclass files reading")},
53         { Debug::LYXVC,     "lyxvc",     N_("Version control")},
54         { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
55         { Debug::UNDO,      "undo",      N_("Undo/Redo mechanism")},
56         { Debug::ACTION,    "action",    N_("User commands")},
57         { Debug::LYXLEX,    "lyxlex",    N_("The LyX Lexer")},
58         { Debug::DEPEND,    "depend",    N_("Dependency information")},
59         { Debug::INSETS,    "insets",    N_("LyX Insets")},
60         { Debug::FILES,     "files",     N_("Files used by LyX")},
61         { Debug::WORKAREA,  "workarea",  N_("Workarea events")},
62         { Debug::CLIPBOARD, "clipboard", N_("Clipboard handling")},
63         { Debug::GRAPHICS,  "graphics",  N_("Graphics conversion and loading")},
64         { Debug::CHANGES,   "changes",   N_("Change tracking")},
65         { Debug::EXTERNAL,  "external",  N_("External template/inset messages")},
66         { Debug::PAINTING,  "painting",  N_("RowPainter profiling")},
67         { Debug::SCROLLING, "scrolling", N_("Scrolling debugging")},
68         { Debug::MACROS,    "macros",    N_("Math macros")},
69         { Debug::RTL,       "rtl",       N_("RTL/Bidi")},
70         { Debug::LOCALE,    "locale",    N_("Locale/Internationalisation")},
71         { Debug::SELECTION, "selection", N_("Selection copy/paste mechanism")},
72         { Debug::FIND,      "find",      N_("Find and replace mechanism")},
73         { Debug::DEBUG,     "debug",     N_("Developers' general debug messages")},
74         { Debug::ANY,       "any",       N_("All debugging messages")}
75 };
76
77
78 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
79
80 } // namespace
81
82
83 int Debug::levelCount()
84 {
85         return numErrorTags;
86 }
87
88
89 Debug::Type Debug::value(int idx)
90 {
91         if (idx > 0 && idx < numErrorTags)
92                 return errorTags[idx].level;
93         return Debug::NONE;
94 }
95
96
97 string const Debug::description(Debug::Type val)
98 {
99         for (int i = 0 ; i < numErrorTags ; ++i) {
100                 if (errorTags[i].level == val)
101                         return errorTags[i].desc;
102         }
103         return "unknown level";
104 }
105
106
107 string const Debug::name(Debug::Type val)
108 {
109         for (int i = 0 ; i < numErrorTags ; ++i) {
110                 if (errorTags[i].level == val)
111                         return errorTags[i].name;
112         }
113         return "unknown level";
114 }
115
116
117 Debug::Type Debug::value(string const & val)
118 {
119         Type l = Debug::NONE;
120         string v = val;
121         while (!v.empty()) {
122                 size_t const st = v.find(',');
123                 string const tmp = ascii_lowercase(v.substr(0, st));
124                 if (tmp.empty())
125                         break;
126                 // Is it a number?
127                 if (isStrInt(tmp))
128                         l |= static_cast<Type>(convert<int>(tmp));
129                 else
130                 // Search for an explicit name
131                 for (int i = 0 ; i < numErrorTags ; ++i)
132                         if (tmp == errorTags[i].name) {
133                                 l |= errorTags[i].level;
134                                 break;
135                         }
136                 if (st == string::npos)
137                 break;
138                 v.erase(0, st + 1);
139         }
140         return l;
141 }
142
143
144 void Debug::showLevel(ostream & os, Debug::Type level)
145 {
146         // Show what features are traced
147         for (int i = 0; i < numErrorTags; ++i) {
148                 if (errorTags[i].level != Debug::ANY
149                       && errorTags[i].level != Debug::NONE
150                       && errorTags[i].level & level) {
151                         // avoid to_utf8(_(...)) re-entrance problem
152                         docstring const s = _(errorTags[i].desc);
153                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
154                                         from_utf8(errorTags[i].name), s))
155                            << '\n';
156                 }
157         }
158         os.flush();
159 }
160
161
162 void Debug::showTags(ostream & os)
163 {
164         for (int i = 0; i != numErrorTags ; ++i)
165                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
166                    << setw(13) << errorTags[i].name
167                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
168         os.flush();
169 }
170
171
172 void LyXErr::disable()
173 {
174         enabled_ = false;
175 }
176
177
178 void LyXErr::enable()
179 {
180         enabled_ = true;
181 }
182
183
184 bool LyXErr::debugging(Debug::Type t) const
185 {
186         return (dt_ & t);
187 }
188
189
190 void LyXErr::endl()
191 {
192         if (enabled_) {
193                 stream() << std::endl;
194                 if (second_enabled_)
195                         secondStream() << std::endl;
196         }
197 }
198
199
200 char const * LyXErr::stripName(char const * n)
201 {
202         string const name = n;
203         // find the last occurence of /src/ in name
204         size_t pos = name.rfind("/src/");
205         if (pos == string::npos)
206                 pos = name.rfind("\\src\\");
207         if (pos == string::npos)
208                 return n;
209         else
210                 return n + pos + 5;
211 }
212
213
214 // It seems not possible to instantiate operator template out of class body
215 template<class T>
216 LyXErr & toStream(LyXErr & l, T t)
217 {
218         if (l.enabled()){
219                 l.stream() << t;
220                 if (l.secondEnabled()) {
221                         l.secondStream() << t;
222                         ProgressInterface::instance()->lyxerrFlush();
223                 }
224         }
225         return l;
226 }
227
228
229 LyXErr & operator<<(LyXErr & l, void const * t)
230 { return toStream(l, t); }
231 LyXErr & operator<<(LyXErr & l, char const * t)
232 { return toStream(l, t); }
233 LyXErr & operator<<(LyXErr & l, char t)
234 { return toStream(l, t); }
235 LyXErr & operator<<(LyXErr & l, int t)
236 { return toStream(l, t); }
237 LyXErr & operator<<(LyXErr & l, unsigned int t)
238 { return toStream(l, t); }
239 LyXErr & operator<<(LyXErr & l, long t)
240 { return toStream(l, t); }
241 LyXErr & operator<<(LyXErr & l, unsigned long t)
242 { return toStream(l, t); }
243 #ifdef LYX_USE_LONG_LONG
244 LyXErr & operator<<(LyXErr & l, long long t)
245 { return toStream(l, t); }
246 LyXErr & operator<<(LyXErr & l, unsigned long long t)
247 { return toStream(l, t); }
248 #endif
249 LyXErr & operator<<(LyXErr & l, double t)
250 { return toStream(l, t); }
251 LyXErr & operator<<(LyXErr & l, string const & t)
252 { return toStream(l, t); }
253 LyXErr & operator<<(LyXErr & l, docstring const & t)
254 { return toStream(l, to_utf8(t)); }
255 LyXErr & operator<<(LyXErr & l, FileName const & t)
256 { return toStream(l, t); }
257 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
258 { return toStream(l, t); }
259 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
260 { return toStream(l, t); }
261
262
263 // The global instance
264 LyXErr lyxerr;
265
266
267
268 } // namespace lyx