Current Version: 1.0.33
Project Name: csspp
error.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2025 Made to Order Software Corp. All Rights Reserved
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License along
14// with this program; if not, write to the Free Software Foundation, Inc.,
15// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
56#include "csspp/error.h"
57
58#include <cmath>
59#include <iostream>
60
61namespace csspp
62{
63
64namespace
65{
66
67// the error instance
68error *g_error = nullptr;
69
70} // no name namespace
71
73 : f_position("error.css")
74{
75}
76
78{
79 // first time, allocate the instance
80 if(g_error == nullptr)
81 {
82 g_error = new error;
83 }
84
85 g_error->reset();
86
87 return *g_error;
88}
89
90std::ostream & error::get_error_stream() const
91{
92 return *f_error;
93}
94
95void error::set_error_stream(std::ostream & err_stream)
96{
97 f_error = &err_stream;
98}
99
100void error::set_count_warnings_as_errors(bool warnings_as_errors)
101{
102 f_warnings_as_errors = warnings_as_errors;
103}
104
109
111{
112 f_error_count = count;
113}
114
119
121{
122 f_warning_count = count;
123}
124
125void error::set_hide_all(bool hide_all)
126{
127 f_hide_all = hide_all;
128}
129
130void error::set_show_debug(bool show_debug)
131{
132 f_show_debug = show_debug;
133}
134
135void error::set_verbose(bool status)
136{
137 f_verbose = status;
138}
139
141{
142 f_position = pos;
143 return *this;
144}
145
147{
148 switch(mode)
149 {
151 f_message << std::dec;
152 break;
153
157 goto print_error;
158
161 {
162 // count warnings just like if they were errors
164 }
165 else
166 {
167 // should we count warnings even if we do not show them?
168 if(f_hide_all)
169 {
170 break;
171 }
173 }
174 goto print_error;
175
177 if(!f_show_debug)
178 {
179 break;
180 }
181#if __cplusplus >= 201700
182 [[fallthrough]];
183#endif
185 if(f_hide_all)
186 {
187 break;
188 }
189
190print_error:
191 // print the error now
192 // (show the page number?)
193 {
194 std::ostream * out = f_error ? f_error : &std::cerr;
195 *out << f_position.get_filename()
196 << "(" << f_position.get_line() << "): " << mode << ": "
197 << f_message.str()
198 << std::endl;
199
200 // for debug purposes, otherwise errors are always hidden!
201 if(f_verbose)
202 {
203 std::cerr << "ERROR CONSOLE OUTPUT -- " << f_position.get_filename()
204 << "(" << f_position.get_line() << "): " << mode << ": "
205 << f_message.str()
206 << std::endl;
207 }
208 }
209 break;
210
212 f_message << std::hex;
213 break;
214
215 }
216
217 return *this;
218}
219
220error & error::operator << (std::string const & msg)
221{
222 f_message << msg;
223 return *this;
224}
225
226error & error::operator << (char const * msg)
227{
228 f_message << msg;
229 return *this;
230}
231
233{
234 f_message << value;
235 return *this;
236}
237
239{
240 f_message << value;
241 return *this;
242}
243
245{
246 f_message << value;
247 return *this;
248}
249
251{
252 // reset the output message
253 f_message.str("");
254 f_message << std::dec;
255}
256
258 : f_error_count(error::instance().get_error_count())
259 , f_warning_count(error::instance().get_warning_count())
260{
261}
262
268
270 : f_error(&error::instance().get_error_stream())
271{
272 error::instance().set_error_stream(err_stream);
273}
274
279
281 : f_error_count(error::instance().get_error_count())
282 , f_warning_count(error::instance().get_warning_count())
283{
284}
285
290
295
296} // namespace csspp
297
298std::ostream & operator << (std::ostream & out, csspp::error_mode_t const type)
299{
300 switch(type)
301 {
303 out << "debug";
304 break;
305
307 out << "dec";
308 break;
309
311 out << "error";
312 break;
313
315 out << "fatal";
316 break;
317
319 out << "info";
320 break;
321
323 out << "hex";
324 break;
325
327 out << "warning";
328 break;
329
330 }
331
332 return out;
333}
334
335// Local Variables:
336// mode: cpp
337// indent-tabs-mode: nil
338// c-basic-offset: 4
339// tab-width: 4
340// End:
341
342// vim: ts=4 sw=4 et
bool error_happened() const
Definition error.cpp:286
bool warning_happened() const
Definition error.cpp:291
error_count_t f_error_count
Definition error.h:127
error_count_t f_warning_count
Definition error.h:128
bool f_warnings_as_errors
Definition error.h:85
std::ostream * f_error
Definition error.h:82
void set_warning_count(error_count_t count)
Definition error.cpp:120
void set_verbose(bool status)
Definition error.cpp:135
static error & instance()
Definition error.cpp:77
void reset()
Definition error.cpp:250
error_count_t f_warning_count
Definition error.h:84
bool f_verbose
Definition error.h:88
void set_show_debug(bool show_debug)
Definition error.cpp:130
void set_count_warnings_as_errors(bool warnings_as_errors)
Definition error.cpp:100
std::stringstream f_message
Definition error.h:81
void set_hide_all(bool show_debug)
Definition error.cpp:125
std::ostream & get_error_stream() const
Definition error.cpp:90
position f_position
Definition error.h:80
bool f_hide_all
Definition error.h:86
error_count_t get_warning_count() const
Definition error.cpp:115
void set_error_count(error_count_t count)
Definition error.cpp:110
bool f_show_debug
Definition error.h:87
error & operator<<(position const &pos)
Definition error.cpp:140
error_count_t get_error_count() const
Definition error.cpp:105
error_count_t f_error_count
Definition error.h:83
void set_error_stream(std::ostream &err_stream)
Definition error.cpp:95
std::string const & get_filename() const
Definition position.cpp:42
line_t get_line() const
Definition position.cpp:52
safe_error_stream_t(std::ostream &err_stream)
Definition error.cpp:269
std::ostream * f_error
Definition error.h:114
error_count_t f_warning_count
Definition error.h:100
error_count_t f_error_count
Definition error.h:99
std::ostream & operator<<(std::ostream &out, csspp::error_mode_t const type)
Definition error.cpp:298
The namespace of all the classes in the CSS Preprocessor.
Definition csspp.h:48
uint32_t error_count_t
Definition error.h:43
error_mode_t
Definition error.h:33

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.