]> git.lyx.org Git - lyx.git/blob - src/support/debug.cpp
infrastructure for 'graceful asserts'
[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/convert.h"
15 #include "support/debug.h"
16 #include "support/gettext.h"
17 #include "support/lstrings.h"
18 #include "support/FileName.h"
19
20 #include <boost/assert.hpp>
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::ROFF,      "roff",      N_("Keep *roff temporary files")},
55         { Debug::ACTION,    "action",    N_("User commands")},
56         { Debug::LYXLEX,    "lyxlex",    N_("The LyX Lexxer")},
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::DEBUG,     "debug",     N_("Developers' general debug messages")},
70         { Debug::ANY,       "any",       N_("All debugging messages")}
71 };
72
73
74 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
75
76 } // namespace anon
77
78
79 Debug::Type Debug::value(string const & val)
80 {
81         Type l = Debug::NONE;
82         string v = val;
83         while (!v.empty()) {
84                 size_t const st = v.find(',');
85                 string const tmp = ascii_lowercase(v.substr(0, st));
86                 if (tmp.empty())
87                         break;
88                 // Is it a number?
89                 if (isStrInt(tmp))
90                         l |= static_cast<Type>(convert<int>(tmp));
91                 else
92                 // Search for an explicit name
93                 for (int i = 0 ; i < numErrorTags ; ++i)
94                         if (tmp == errorTags[i].name) {
95                                 l |= errorTags[i].level;
96                                 break;
97                         }
98                 if (st == string::npos)
99                 break;
100                 v.erase(0, st + 1);
101         }
102         return l;
103 }
104
105
106 void Debug::showLevel(ostream & os, Debug::Type level)
107 {
108         // Show what features are traced
109         for (int i = 0; i != numErrorTags; ++i) {
110                 if (errorTags[i].level != Debug::ANY
111                     && errorTags[i].level != Debug::NONE
112                     && errorTags[i].level & level) {
113                         // avoid to_utf8(_(...)) re-entrance problem
114                         docstring const s = _(errorTags[i].desc);
115                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
116                                         from_utf8(errorTags[i].name), s))
117                            << '\n';
118                 }
119         }
120         os.flush();
121 }
122
123
124 void Debug::showTags(ostream & os)
125 {
126         for (int i = 0; i != numErrorTags ; ++i)
127                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
128                    << setw(13) << errorTags[i].name
129                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
130         os.flush();
131 }
132
133
134 void LyXErr::disable()
135 {
136         enabled_ = false;
137 }
138
139
140 void LyXErr::enable()
141 {
142         enabled_ = true;
143 }
144
145
146 bool LyXErr::debugging(Debug::Type t) const
147 {
148         return (dt & t);
149 }
150
151
152 void LyXErr::endl()
153 {
154         if (enabled_)
155                 stream() << std::endl;
156 }
157
158
159 LyXErr & operator<<(LyXErr & l, void const * t)
160 { if (l.enabled()) l.stream() << t; return l; }
161 LyXErr & operator<<(LyXErr & l, char const * t)
162 { if (l.enabled()) l.stream() << t; return l; }
163 LyXErr & operator<<(LyXErr & l, char t)
164 { if (l.enabled()) l.stream() << t; return l; }
165 LyXErr & operator<<(LyXErr & l, int t)
166 { if (l.enabled()) l.stream() << t; return l; }
167 LyXErr & operator<<(LyXErr & l, unsigned int t)
168 { if (l.enabled()) l.stream() << t; return l; }
169 LyXErr & operator<<(LyXErr & l, long t)
170 { if (l.enabled()) l.stream() << t; return l; }
171 LyXErr & operator<<(LyXErr & l, unsigned long t)
172 { if (l.enabled()) l.stream() << t; return l; }
173 LyXErr & operator<<(LyXErr & l, double t)
174 { if (l.enabled()) l.stream() << t; return l; }
175 LyXErr & operator<<(LyXErr & l, string const & t)
176 { if (l.enabled()) l.stream() << t; return l; }
177 LyXErr & operator<<(LyXErr & l, docstring const & t)
178 { if (l.enabled()) l.stream() << to_utf8(t); return l; }
179 LyXErr & operator<<(LyXErr & l, FileName const & t)
180 { if (l.enabled()) l.stream() << t; return l; }
181 LyXErr & operator<<(LyXErr & l, ostream &(*t)(ostream &))
182 { if (l.enabled()) l.stream() << t; return l; }
183 LyXErr & operator<<(LyXErr & l, ios_base &(*t)(ios_base &))
184 { if (l.enabled()) l.stream() << t; return l; }
185
186
187 // The global instance
188 LyXErr lyxerr;
189
190 void doAssert(char const * expr,  char const * file, long line)
191 {
192         LYXERR0("ASSERTION " << expr << " VIOLATED in " << file << ":" << line);
193         // comment this out if not needed
194         BOOST_ASSERT(false);
195 }
196
197 } // namespace lyx