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