]> git.lyx.org Git - lyx.git/blob - src/client/debug.cpp
* docstream: factorize out some code and introduce odocfstream::reset()
[lyx.git] / src / client / 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 #include "support/gettext.h"
16
17 #include "support/convert.h"
18 #include "support/lstrings.h"
19 #include "support/FileName.h"
20
21 #include <iostream>
22 #include <iomanip>
23
24
25 namespace lyx {
26
27 using support::ascii_lowercase;
28 using support::bformat;
29 using support::isStrInt;
30
31 using std::setw;
32 using std::string;
33 using std::ostream;
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::DEBUG,     "debug",     N_("Developers' general debug messages")},
48         { Debug::ANY,       "any",       N_("All debugging messages")}
49 };
50
51
52 int const numErrorTags = sizeof(errorTags)/sizeof(errorTags[0]);
53
54 } // namespace anon
55
56
57 Debug::Type Debug::value(string const & val)
58 {
59         Type l = Debug::NONE;
60         string v(val);
61         while (!v.empty()) {
62                 string::size_type const st = v.find(',');
63                 string const tmp(ascii_lowercase(v.substr(0, st)));
64                 if (tmp.empty())
65                         break;
66                 // Is it a number?
67                 if (isStrInt(tmp))
68                         l |= static_cast<Type>(convert<int>(tmp));
69                 else
70                 // Search for an explicit name
71                 for (int i = 0 ; i < numErrorTags ; ++i)
72                         if (tmp == errorTags[i].name) {
73                                 l |= errorTags[i].level;
74                                 break;
75                         }
76                 if (st == string::npos) break;
77                 v.erase(0, st + 1);
78         }
79         return l;
80 }
81
82
83 void Debug::showLevel(ostream & os, Debug::Type level)
84 {
85         // Show what features are traced
86         for (int i = 0; i < numErrorTags ; ++i) {
87                 if (errorTags[i].level != Debug::ANY
88                     && errorTags[i].level != Debug::NONE
89                     && errorTags[i].level & level) {
90                         // avoid to_utf8(_(...)) re-entrance problem
91                         docstring const s = _(errorTags[i].desc);
92                         os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
93                                         from_utf8(errorTags[i].name), s))
94                            << '\n';
95                 }
96         }
97         os.flush();
98 }
99
100
101 void Debug::showTags(ostream & os)
102 {
103         for (int i = 0; i < numErrorTags ; ++i)
104                 os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
105                    << setw(13) << errorTags[i].name
106                    << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
107         os.flush();
108 }
109
110
111 void LyXErr::disable()
112 {
113         enabled_ = false;
114 }
115
116
117 void LyXErr::enable()
118 {
119         enabled_ = true;
120 }
121
122
123 bool LyXErr::debugging(Debug::Type t) const
124 {
125         return (dt & t);
126 }
127
128
129 void LyXErr::endl()
130 {
131         stream() << std::endl;
132 }
133
134
135 LyXErr & operator<<(LyXErr & l, void const * t)
136 { l.stream() << t; return l; }
137 LyXErr & operator<<(LyXErr & l, char const * t)
138 { l.stream() << t; return l; }
139 LyXErr & operator<<(LyXErr & l, char t)
140 { l.stream() << t; return l; }
141 LyXErr & operator<<(LyXErr & l, int t)
142 { l.stream() << t; return l; }
143 LyXErr & operator<<(LyXErr & l, unsigned int t)
144 { l.stream() << t; return l; }
145 LyXErr & operator<<(LyXErr & l, long t)
146 { l.stream() << t; return l; }
147 LyXErr & operator<<(LyXErr & l, unsigned long t)
148 { l.stream() << t; return l; }
149 LyXErr & operator<<(LyXErr & l, double t)
150 { l.stream() << t; return l; }
151 LyXErr & operator<<(LyXErr & l, std::string const & t)
152 { l.stream() << t; return l; }
153 LyXErr & operator<<(LyXErr & l, docstring const & t)
154 { l.stream() << to_utf8(t); return l; }
155 LyXErr & operator<<(LyXErr & l, support::FileName const & t)
156 { l.stream() << t; return l; }
157 LyXErr & operator<<(LyXErr & l, std::ostream &(*t)(std::ostream &))
158 { l.stream() << t; return l; }
159 LyXErr & operator<<(LyXErr & l, std::ios_base &(*t)(std::ios_base &))
160 { l.stream() << t; return l; }
161
162 LyXErr lyxerr;
163
164
165 } // namespace lyx