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

Documentation of CSS Preprocessor.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.