]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
Merge branch 'master' of git.lyx.org:lyx
[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 #include "support/regex.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 };
77
78
79 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
80
81 } // namespace anon
82
83
84 int Debug::levelCount()
85 {
86         return numErrorTags;
87 }
88
89
90 Debug::Type Debug::value(int idx)
91 {
92         if (idx > 0 && idx < numErrorTags)
93                 return errorTags[idx].level;
94         return Debug::NONE;
95 }
96
97
98 string const Debug::description(Debug::Type val)
99 {
100         for (int i = 0 ; i < numErrorTags ; ++i) {
101                 if (errorTags[i].level == val)
102                         return errorTags[i].desc;
103         }
104         return "unknown level";
105 }
106
107
108 string const Debug::name(Debug::Type val)
109 {
110         for (int i = 0 ; i < numErrorTags ; ++i) {
111                 if (errorTags[i].level == val)
112                         return errorTags[i].name;
113         }
114         return "unknown level";
115 }
116
117
118 Debug::Type Debug::value(string const & val)
119 {
120         Type l = Debug::NONE;
121         string v = val;
122         while (!v.empty()) {
123                 size_t const st = v.find(',');
124                 string const tmp = ascii_lowercase(v.substr(0, st));
125                 if (tmp.empty())
126                         break;
127                 // Is it a number?
128                 if (isStrInt(tmp))
129                         l |= static_cast<Type>(convert<int>(tmp));
130                 else
131                 // Search for an explicit name
132                 for (int i = 0 ; i < numErrorTags ; ++i)
133                         if (tmp == errorTags[i].name) {
134                                 l |= errorTags[i].level;
135                                 break;
136                         }
137                 if (st == string::npos)
138                 break;
139                 v.erase(0, st + 1);
140         }
141         return l;
142 }
143
144
145 void Debug::showLevel(ostream & os, Debug::Type level)
146 {
147         // Show what features are traced
148         for (int i = 0; i < numErrorTags; ++i) {
149                 if (errorTags[i].level != Debug::ANY
150                       && errorTags[i].level != Debug::NONE
151                       && errorTags[i].level & level) {
152                         // avoid to_utf8(_(...)) re-entrance problem
153                         docstring const s = _(errorTags[i].desc);
154                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
155                                         from_utf8(errorTags[i].name), s))
156                            << '\n';
157                 }
158         }
159         os.flush();
160 }
161
162
163 void Debug::showTags(ostream & os)
164 {
165         for (int i = 0; i != numErrorTags ; ++i)
166                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
167                    << setw(13) << errorTags[i].name
168                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
169         os.flush();
170 }
171
172
173 void LyXErr::disable()
174 {
175         enabled_ = false;
176 }
177
178
179 void LyXErr::enable()
180 {
181         enabled_ = true;
182 }
183
184
185 bool LyXErr::debugging(Debug::Type t) const
186 {
187         return (dt_ & t);
188 }
189
190
191 void LyXErr::endl()
192 {
193         if (enabled_) {
194                 stream() << std::endl;
195                 if (second_enabled_)
196                         secondStream() << std::endl;
197         }
198 }
199
200
201 char const * LyXErr::stripName(char const * n)
202 {
203         string const name = n;
204         // find the last occurence of /src/ in name
205         static const regex re("[\\/]src[\\/]");
206         string::const_iterator const begin = name.begin();
207         string::const_iterator it = begin;
208         string::const_iterator const end = name.end();
209         smatch results;
210         while (regex_search(it, end, results, re)) {
211                 it = results[0].second;
212         }
213         return n + std::distance(begin, it);
214 }
215
216
217 // It seems not possible to instantiate operator template out of class body
218 template<class T>
219 LyXErr & toStream(LyXErr & l, T t)      
220 {
221         if (l.enabled()){
222                 l.stream() << t;
223                 if (l.secondEnabled()) {
224                         l.secondStream() << t;
225                         ProgressInterface::instance()->lyxerrFlush();
226                 }
227         }
228         return l;
229 }
230
231
232 LyXErr & operator<<(LyXErr & l, void const * t)
233 { return toStream(l, t); }
234 LyXErr & operator<<(LyXErr & l, char const * t)
235 { return toStream(l, t); }
236 LyXErr & operator<<(LyXErr & l, char t)
237 { return toStream(l, t); }
238 LyXErr & operator<<(LyXErr & l, int t)
239 { return toStream(l, t); }
240 LyXErr & operator<<(LyXErr & l, unsigned int t)
241 { return toStream(l, t); }
242 LyXErr & operator<<(LyXErr & l, long t)
243 { return toStream(l, t); }
244 LyXErr & operator<<(LyXErr & l, unsigned long t)
245 { return toStream(l, t); }
246 #ifdef LYX_USE_LONG_LONG
247 LyXErr & operator<<(LyXErr & l, long long t)
248 { return toStream(l, t); }
249 LyXErr & operator<<(LyXErr & l, unsigned long long t)
250 { return toStream(l, t); }
251 #endif
252 LyXErr & operator<<(LyXErr & l, double t)
253 { return toStream(l, t); }
254 LyXErr & operator<<(LyXErr & l, string const & t)
255 { return toStream(l, t); }
256 LyXErr & operator<<(LyXErr & l, docstring const & t)
257 { return toStream(l, to_utf8(t)); }
258 LyXErr & operator<<(LyXErr & l, FileName const & t)
259 { return toStream(l, t); }
260 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
261 { return toStream(l, t); }
262 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
263 { return toStream(l, t); }
264
265
266 // The global instance
267 LyXErr lyxerr;
268
269
270
271 } // namespace lyx