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