]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
2e1c7ab1d91822f88829fb7d99cba0d6916faf47
[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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/debug.h"
15
16 #include "support/convert.h"
17 #include "support/gettext.h"
18 #include "support/lstrings.h"
19 #include "support/FileName.h"
20
21 #include <iostream>
22 #include <iomanip>
23
24 using std::setw;
25 using std::string;
26 using std::ostream;
27
28
29 namespace lyx {
30
31 using support::ascii_lowercase;
32 using support::bformat;
33 using support::isStrInt;
34
35 namespace {
36
37 struct ErrorItem {
38         Debug::Type level;
39         char const * name;
40         char const * desc;
41 };
42
43
44 ErrorItem errorTags[] = {
45         { Debug::NONE,      "none",      N_("No debugging message")},
46         { Debug::INFO,      "info",      N_("General information")},
47         { Debug::INIT,      "init",      N_("Program initialisation")},
48         { Debug::KEY,       "key",       N_("Keyboard events handling")},
49         { Debug::GUI,       "gui",       N_("GUI handling")},
50         { Debug::PARSER,    "parser",    N_("Lyxlex grammar parser")},
51         { Debug::LYXRC,     "lyxrc",     N_("Configuration files reading")},
52         { Debug::KBMAP,     "kbmap",     N_("Custom keyboard definition")},
53         { Debug::LATEX,     "latex",     N_("LaTeX generation/execution")},
54         { Debug::MATHED,    "mathed",    N_("Math editor")},
55         { Debug::FONT,      "font",      N_("Font handling")},
56         { Debug::TCLASS,    "tclass",    N_("Textclass files reading")},
57         { Debug::LYXVC,     "lyxvc",     N_("Version control")},
58         { Debug::LYXSERVER, "lyxserver", N_("External control interface")},
59         { Debug::ROFF,      "roff",      N_("Keep *roff temporary files")},
60         { Debug::ACTION,    "action",    N_("User commands")},
61         { Debug::LYXLEX,    "lyxlex",    N_("The LyX Lexxer")},
62         { Debug::DEPEND,    "depend",    N_("Dependency information")},
63         { Debug::INSETS,    "insets",    N_("LyX Insets")},
64         { Debug::FILES,     "files",     N_("Files used by LyX")},
65         { Debug::WORKAREA,  "workarea",  N_("Workarea events")},
66         { Debug::INSETTEXT, "insettext", N_("Insettext/tabular messages")},
67         { Debug::GRAPHICS,  "graphics",  N_("Graphics conversion and loading")},
68         { Debug::CHANGES,   "changes",   N_("Change tracking")},
69         { Debug::EXTERNAL,  "external",  N_("External template/inset messages")},
70         { Debug::PAINTING,  "painting",  N_("RowPainter profiling")},
71         { Debug::DEBUG,     "debug",     N_("Developers' general debug messages")},
72         { Debug::ANY,       "any",       N_("All debugging messages")}
73 };
74
75
76 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
77
78 } // namespace anon
79
80
81 Debug::Type Debug::value(string const & val)
82 {
83         Type l = Debug::NONE;
84         string v = val;
85         while (!v.empty()) {
86                 size_t const st = v.find(',');
87                 string const tmp = ascii_lowercase(v.substr(0, st));
88                 if (tmp.empty())
89                         break;
90                 // Is it a number?
91                 if (isStrInt(tmp))
92                         l |= static_cast<Type>(convert<int>(tmp));
93                 else
94                 // Search for an explicit name
95                 for (int i = 0 ; i < numErrorTags ; ++i)
96                         if (tmp == errorTags[i].name) {
97                                 l |= errorTags[i].level;
98                                 break;
99                         }
100                 if (st == string::npos)
101                 break;
102                 v.erase(0, st + 1);
103         }
104         return l;
105 }
106
107
108 void Debug::showLevel(ostream & os, Debug::Type level)
109 {
110         // Show what features are traced
111         for (int i = 0; i != numErrorTags; ++i) {
112                 if (errorTags[i].level != Debug::ANY
113                     && errorTags[i].level != Debug::NONE
114                     && errorTags[i].level & level) {
115                         // avoid to_utf8(_(...)) re-entrance problem
116                         docstring const s = _(errorTags[i].desc);
117                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
118                                         from_utf8(errorTags[i].name), s))
119                            << '\n';
120                 }
121         }
122         os.flush();
123 }
124
125
126 void Debug::showTags(ostream & os)
127 {
128         for (int i = 0; i != numErrorTags ; ++i)
129                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
130                    << setw(13) << errorTags[i].name
131                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
132         os.flush();
133 }
134
135
136 void LyXErr::disable()
137 {
138         enabled_ = false;
139 }
140
141
142 void LyXErr::enable()
143 {
144         enabled_ = true;
145 }
146
147
148 bool LyXErr::debugging(Debug::Type t) const
149 {
150         return (dt & t);
151 }
152
153
154 void LyXErr::endl()
155 {
156         if (enabled_)
157                 stream() << std::endl;
158 }
159
160
161 LyXErr & operator<<(LyXErr & l, void const * t)
162 { if (l.enabled()) l.stream() << t; return l; }
163 LyXErr & operator<<(LyXErr & l, char const * t)
164 { if (l.enabled()) l.stream() << t; return l; }
165 LyXErr & operator<<(LyXErr & l, char t)
166 { if (l.enabled()) l.stream() << t; return l; }
167 LyXErr & operator<<(LyXErr & l, int t)
168 { if (l.enabled()) l.stream() << t; return l; }
169 LyXErr & operator<<(LyXErr & l, unsigned int t)
170 { if (l.enabled()) l.stream() << t; return l; }
171 LyXErr & operator<<(LyXErr & l, long t)
172 { if (l.enabled()) l.stream() << t; return l; }
173 LyXErr & operator<<(LyXErr & l, unsigned long t)
174 { if (l.enabled()) l.stream() << t; return l; }
175 LyXErr & operator<<(LyXErr & l, double t)
176 { if (l.enabled()) l.stream() << t; return l; }
177 LyXErr & operator<<(LyXErr & l, std::string const & t)
178 { if (l.enabled()) l.stream() << t; return l; }
179 LyXErr & operator<<(LyXErr & l, docstring const & t)
180 { if (l.enabled()) l.stream() << to_utf8(t); return l; }
181 LyXErr & operator<<(LyXErr & l, support::FileName const & t)
182 { if (l.enabled()) l.stream() << t; return l; }
183 LyXErr & operator<<(LyXErr & l, std::ostream &(*t)(std::ostream &))
184 { if (l.enabled()) l.stream() << t; return l; }
185 LyXErr & operator<<(LyXErr & l, std::ios_base &(*t)(std::ios_base &))
186 { if (l.enabled()) l.stream() << t; return l; }
187
188
189 // The global instance
190 LyXErr lyxerr;
191
192
193 } // namespace lyx