]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
6f3ce34e09064e94a567db8fc392635d95660626
[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 message")},
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 const std::vector<Debug::Type> Debug::levels()
83 {
84         std::vector<Debug::Type> vec;
85         for (int i = 0 ; i < numErrorTags ; ++i) {
86                 vec.push_back(errorTags[i].level);
87         }
88         return vec;
89 }
90
91 string const Debug::description(Debug::Type val)
92 {
93         for (int i = 0 ; i < numErrorTags ; ++i) {
94                 if (errorTags[i].level == val)
95                         return errorTags[i].desc;
96         }
97         return "unknown level";
98 }
99
100
101 string const Debug::name(Debug::Type val)
102 {
103         for (int i = 0 ; i < numErrorTags ; ++i) {
104                 if (errorTags[i].level == val)
105                         return errorTags[i].name;
106         }
107         return "unknown level";
108 }
109
110
111 Debug::Type Debug::value(string const & val)
112 {
113         Type l = Debug::NONE;
114         string v = val;
115         while (!v.empty()) {
116                 size_t const st = v.find(',');
117                 string const tmp = ascii_lowercase(v.substr(0, st));
118                 if (tmp.empty())
119                         break;
120                 // Is it a number?
121                 if (isStrInt(tmp))
122                         l |= static_cast<Type>(convert<int>(tmp));
123                 else
124                 // Search for an explicit name
125                 for (int i = 0 ; i < numErrorTags ; ++i)
126                         if (tmp == errorTags[i].name) {
127                                 l |= errorTags[i].level;
128                                 break;
129                         }
130                 if (st == string::npos)
131                 break;
132                 v.erase(0, st + 1);
133         }
134         return l;
135 }
136
137
138 void Debug::showLevel(ostream & os, Debug::Type level)
139 {
140         // Show what features are traced
141         for (int i = 0; i != numErrorTags; ++i) {
142                 if (errorTags[i].level != Debug::ANY
143                     && errorTags[i].level != Debug::NONE
144                     && errorTags[i].level & level) {
145                         // avoid to_utf8(_(...)) re-entrance problem
146                         docstring const s = _(errorTags[i].desc);
147                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
148                                         from_utf8(errorTags[i].name), s))
149                            << '\n';
150                 }
151         }
152         os.flush();
153 }
154
155
156 void Debug::showTags(ostream & os)
157 {
158         for (int i = 0; i != numErrorTags ; ++i)
159                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
160                    << setw(13) << errorTags[i].name
161                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
162         os.flush();
163 }
164
165
166 void LyXErr::disable()
167 {
168         enabled_ = false;
169 }
170
171
172 void LyXErr::enable()
173 {
174         enabled_ = true;
175 }
176
177
178 bool LyXErr::debugging(Debug::Type t) const
179 {
180         return (dt & t);
181 }
182
183
184 void LyXErr::endl()
185 {
186         if (enabled_) {
187                 stream() << std::endl;
188                 if (second_used_)
189                         second() << "\n";
190         }
191 }
192
193
194 // It seems not possible to instantiate operator template out of class body
195 #define STREAM_OPERATOR(t)      \
196 {\
197         if (l.enabled()){\
198                 l.stream() << t;\
199                 if (l.second_used()){\
200                         l.second() << t;\
201                         ProgressInterface::instance()->lyxerrFlush();\
202                 }\
203         }\
204         return l;\
205 }
206
207
208 LyXErr & operator<<(LyXErr & l, void const * t)
209 STREAM_OPERATOR(t)
210 LyXErr & operator<<(LyXErr & l, char const * t)
211 STREAM_OPERATOR(t)
212 LyXErr & operator<<(LyXErr & l, char t)
213 STREAM_OPERATOR(t)
214 LyXErr & operator<<(LyXErr & l, int t)
215 STREAM_OPERATOR(t)
216 LyXErr & operator<<(LyXErr & l, unsigned int t)
217 STREAM_OPERATOR(t)
218 LyXErr & operator<<(LyXErr & l, long t)
219 STREAM_OPERATOR(t)
220 LyXErr & operator<<(LyXErr & l, unsigned long t)
221 STREAM_OPERATOR(t)
222 LyXErr & operator<<(LyXErr & l, double t)
223 STREAM_OPERATOR(t)
224 LyXErr & operator<<(LyXErr & l, string const & t)
225 STREAM_OPERATOR(t)
226 LyXErr & operator<<(LyXErr & l, docstring const & t)
227 STREAM_OPERATOR(to_utf8(t));
228 LyXErr & operator<<(LyXErr & l, FileName const & t)
229 STREAM_OPERATOR(t)
230 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
231 STREAM_OPERATOR(t)
232 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
233 STREAM_OPERATOR(t)
234
235
236 // The global instance
237 LyXErr lyxerr;
238
239 } // namespace lyx