ViennaSHE 1.3.0
Free open-source semiconductor device simulator using spherical harmonics expansions techniques.
log.hpp
Go to the documentation of this file.
1#ifndef VIENNASHE_LOG_LOG_HPP
2#define VIENNASHE_LOG_LOG_HPP
3
4/* ============================================================================
5 Copyright (c) 2011-2022, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8
9 -----------------
10 ViennaSHE - The Vienna Spherical Harmonics Expansion Boltzmann Solver
11 -----------------
12
13 http://viennashe.sourceforge.net/
14
15 License: MIT (X11), see file LICENSE in the base directory
16=============================================================================== */
17
18#include <iosfwd>
19
20#include <sstream>
21#include <iostream>
22#include <iomanip>
23#include <string>
24#include <stdio.h>
25
27
28// Use this to enable fancy output for BASH
29//#define VIENNASHE_LOG_ENABLE_FANCY_BASH
30
37namespace viennashe
38{
40 namespace log
41 {
42
45 {
50
52 };
53
55 namespace detail
56 {
57 static log_levels modify_log_level(log_levels new_level, bool setter = false)
58 {
59 static log_levels current_level = logDEBUG; // DEFAULT LOG LEVEL IS DEBUG
60 if (setter) current_level = new_level;
61 return current_level;
62 }
63 }
64
66 inline void set_log_level(log_levels new_level) { viennashe::log::detail::modify_log_level(new_level, true); }
68 inline log_levels log_level() { return viennashe::log::detail::modify_log_level(viennashe::log::logDEBUG, false); }
69
74 template < bool enabled >
75 class logger
76 {
77 private:
78 typedef std::ostringstream CollectorStreamType;
79 CollectorStreamType local_out;
80
81 logger & operator=(const logger &) { return *this; }
82
83 log_levels _messageLevel;
84
85 public:
86
87 explicit logger() : _messageLevel(log_level()) { }
88
89 logger(log_levels level) : _messageLevel(level) { }
90
92 logger(const std::string & component_name) : _messageLevel(log_level())
93 {
94 this->get() << "[" << component_name << "] ";
95 }
96
98 logger(log_levels level, const std::string & component_name) : _messageLevel(level)
99 {
100 this->get() << "[" << component_name << "] ";
101 }
102
103 logger(const logger & r) : _messageLevel(r._messageLevel){
104 if ( do_log() ){local_out.str(r.local_out.str());} // _messageLevel must have been copied already! C++7 change of policy
105// if ( do_log() ){ get() << r.get()}; // _messageLevel must have been copied already!
106 }
107
110 {
111#ifndef VIENNASHE_LOG_DISABLE
112 if (this->do_log())
113 {
114 #ifdef VIENNASHE_LOG_ENABLE_FANCY_BASH
115 switch(_messageLevel)
116 {
117 case log::logERROR:
118 #ifdef VIENNASHE_LOG_ATOMIC
119 fprintf(stderr, "\033[1;31m %s \033[0m", get().str().c_str());
120 fflush(stderr);
121 #else
122 std::cout << get().str();
123 #endif
124 break;
125 case log::logWARNING:
126 #ifdef VIENNASHE_LOG_ATOMIC
127 fprintf(stdout, "\033[1;33m %s \033[0m", get().str().c_str());
128 fflush(stdout);
129 #else
130 std::cout << get().str();
131 #endif
132 break;
133 case log::logDEBUG:
134 #ifdef VIENNASHE_LOG_ATOMIC
135 fprintf(stdout, "\033[1;32m %s \033[0m", get().str().c_str());
136 fflush(stdout);
137 #else
138 std::cout << get().str();
139 #endif
140 break;
141 default:
142 #ifdef VIENNASHE_LOG_ATOMIC
143 fprintf(stdout, "%s", get().str().c_str());
144 fflush(stdout);
145 #else
146 std::cout << get().str();
147 #endif
148 break;
149 }
150 #else
151 #ifdef VIENNASHE_LOG_ATOMIC
152 fprintf(stdout, "%s", get().str().c_str());
153 fflush(stdout);
154 #else
155 std::cout << get().str();
156 #endif
157 #endif
158 }
159#endif
160 }
161
163 bool do_log() const { return (this->_messageLevel <= log_level()); }
164
166 CollectorStreamType & get() { return local_out; }
168 CollectorStreamType const & get() const { return local_out; }
169
171 template <typename T>
172 CollectorStreamType & operator<<(const T & x )
173 {
174 if( do_log() ) { get() << x; }
175 return get();
176 }
177
178 typedef std::ostream& (*ostream_manipulator)(std::ostream&);
179 CollectorStreamType & operator<<(ostream_manipulator pf)
180 {
181 if( do_log() ) { get() << pf ; }
182 return get();
183 }
184
185 CollectorStreamType & operator<<(std::string & x )
186 {
187 if( do_log() ) { get() << x; }
188 return get();
189 }
190
191 CollectorStreamType & operator<<(const std::string & x )
192 {
193 if( do_log() ) { get() << x; }
194 return get();
195 }
196
197 CollectorStreamType & operator<<(const char * x )
198 {
199 if( do_log() && x != 0) { get() << x; }
200 return get();
201 }
202
203 CollectorStreamType & operator<<(logger & r )
204 {
205 if( do_log() ) { get() << r.get(); }
206 return get();
207 }
208 };
209
212 template < >
213 class logger<false>
214 {
215 private:
216
217 logger & operator=(const logger &) { return *this; }
218
219 log_levels _messageLevel;
220
221 public:
222
223 explicit logger() : _messageLevel(log_level()) { }
224
225 logger(log_levels level) : _messageLevel(level) { }
226
227 logger(const std::string &) : _messageLevel(log_level()) { }
228
229 logger(log_levels level, const std::string &) : _messageLevel(level) { }
230
231 logger(const logger & r) : _messageLevel(r._messageLevel) { }
232
234
235 bool do_log() const { return false; }
236
237 nullstream & get() { return get_nullstream(); }
238 nullstream const& get() const { return get_nullstream(); }
239
240 template <typename T>
241 nullstream & operator<<(const T &) { return get_nullstream(); }
242
243 typedef std::ostream& (*ostream_manipulator)(std::ostream&);
245
246 nullstream & operator<<(std::string &) { return get_nullstream(); }
247 nullstream & operator<<(const std::string &) { return get_nullstream(); }
248 nullstream & operator<<(const char *) { return get_nullstream(); }
250 };
251
253 namespace detail
254 {
255
256#ifndef VIENNASHE_LOG_DISABLE
257
258 template < typename KeyTypeT >
260 {
262 }
263
264 template < log_levels level, typename KeyTypeT >
266 {
267 return logger<KeyTypeT::enabled>(level);
268 }
269
270 template < log_levels level >
272 {
273 return logger<true>(level); // using the given level
274 }
275
276#else
277 template < typename KeyTypeT >
278 nullstream & vlogT()
279 {
280 return get_nullstream();
281 }
282
283 template < log_levels level, typename KeyTypeT >
284 nullstream & vlogTL()
285 {
286 return get_nullstream();
287 }
288
289 template < log_levels level >
290 nullstream & vlogL( )
291 {
292 return get_nullstream();
293 }
294#endif
295
296 } // namespace detail
297
298#ifndef VIENNASHE_LOG_DISABLE
299
301 inline logger<true> error() { return detail::vlogL<logERROR>(); }
303 inline logger<true> warn() { return detail::vlogL<logWARNING>(); }
305 inline logger<true> warning() { return detail::vlogL<logWARNING>(); }
307 inline logger<true> info() { return detail::vlogL<logINFO>(); }
309 inline logger<true> debug() { return detail::vlogL<logDEBUG>(); }
310
311
312
315 template < typename KeyTypeT >
316 logger<KeyTypeT::enabled> error() { return detail::vlogTL<logERROR, KeyTypeT>(); }
317
320 template < typename KeyTypeT >
321 logger<KeyTypeT::enabled> warn() { return detail::vlogTL<logWARNING, KeyTypeT>(); }
322
325 template < typename KeyTypeT >
326 logger<KeyTypeT::enabled> warning() { return detail::vlogTL<logWARNING, KeyTypeT>(); }
327
330 template < typename KeyTypeT >
331 logger<KeyTypeT::enabled> info() { return detail::vlogTL<logINFO, KeyTypeT>(); }
332
335 template < typename KeyTypeT >
336 logger<KeyTypeT::enabled> debug() { return detail::vlogTL<logDEBUG, KeyTypeT>(); }
337
338#else
339
340 inline nullstream & error() { return detail::vlogL<logERROR>(); }
341 inline nullstream & warn() { return detail::vlogL<logWARNING>(); }
342 inline nullstream & warning() { return detail::vlogL<logWARNING>(); }
343 inline nullstream & info() { return detail::vlogL<logINFO>(); }
344 inline nullstream & debug() { return detail::vlogL<logDEBUG>(); }
345
346 template < typename KeyTypeT >
347 nullstream & error() { return detail::vlogTL<logERROR, KeyTypeT>(); }
348
349 template < typename KeyTypeT >
350 nullstream & warn() { return detail::vlogTL<logWARNING, KeyTypeT>(); }
351
352 template < typename KeyTypeT >
353 nullstream & warning() { return detail::vlogTL<logWARNING, KeyTypeT>(); }
354
355 template < typename KeyTypeT >
356 nullstream & info() { return detail::vlogTL<logINFO, KeyTypeT>(); }
357
358 template < typename KeyTypeT >
359 nullstream & debug() { return detail::vlogTL<logDEBUG, KeyTypeT>(); }
360
361#endif
362
363 } // log
364} // viennashe
365
366
367#endif
368
nullstream & operator<<(std::string &)
Definition: log.hpp:246
nullstream & operator<<(const T &)
Definition: log.hpp:241
logger(log_levels level, const std::string &)
Definition: log.hpp:229
logger(const std::string &)
Definition: log.hpp:227
logger(const logger &r)
Definition: log.hpp:231
nullstream & operator<<(logger &)
Definition: log.hpp:249
nullstream & operator<<(ostream_manipulator)
Definition: log.hpp:244
nullstream & operator<<(const std::string &)
Definition: log.hpp:247
nullstream const & get() const
Definition: log.hpp:238
logger(log_levels level)
Definition: log.hpp:225
nullstream & operator<<(const char *)
Definition: log.hpp:248
The Main logger class. Assembles output lines and writes them to std::cout upon destruction.
Definition: log.hpp:76
CollectorStreamType & operator<<(logger &r)
Definition: log.hpp:203
~logger()
Destructor. Does actually write the log-message to the output-stream (normally std::cout)
Definition: log.hpp:109
logger(const logger &r)
Definition: log.hpp:103
CollectorStreamType & get()
Returns the collector stream to collect the output.
Definition: log.hpp:166
CollectorStreamType & operator<<(const T &x)
Generic shift left operator to print stuff via the logger.
Definition: log.hpp:172
CollectorStreamType & operator<<(std::string &x)
Definition: log.hpp:185
logger(log_levels level)
Definition: log.hpp:89
CollectorStreamType & operator<<(const char *x)
Definition: log.hpp:197
CollectorStreamType const & get() const
Returns the collector stream to collect the output.
Definition: log.hpp:168
std::ostream &(* ostream_manipulator)(std::ostream &)
Definition: log.hpp:178
CollectorStreamType & operator<<(const std::string &x)
Definition: log.hpp:191
logger(log_levels level, const std::string &component_name)
CTOR to log componentwise on a certain log-level. Adds [component_name] to the start of every log-lin...
Definition: log.hpp:98
logger(const std::string &component_name)
CTOR to log componentwise. Adds [component_name] to the start of every log-line.
Definition: log.hpp:92
bool do_log() const
Returns true if the log-level is smaller than the globally set one.
Definition: log.hpp:163
CollectorStreamType & operator<<(ostream_manipulator pf)
Definition: log.hpp:179
logger< true > vlogL()
Definition: log.hpp:271
logger< KeyTypeT::enabled > vlogT()
Definition: log.hpp:259
logger< KeyTypeT::enabled > vlogTL()
Definition: log.hpp:265
log_levels
Defines various log-levels. Note that log levels are inclusive, i.e. log levels with larger number in...
Definition: log.hpp:45
@ logWARNING
logger outputs errors only
Definition: log.hpp:47
@ logDEBUG
logger outputs errors, warnings and status information
Definition: log.hpp:49
@ logINFO
logger outputs errors and warnings
Definition: log.hpp:48
@ logNEVER
logger outputs lots of debug infos as well
Definition: log.hpp:51
logger< true > error()
Used to log errors. The logging level is logERROR.
Definition: log.hpp:301
nullstream & get_nullstream()
Singleton factory for nullstream.
Definition: nullstream.hpp:37
logger< true > debug()
Used to log debug output. The logging level is logDEBUG.
Definition: log.hpp:309
log_levels log_level()
Getter for the global log level.
Definition: log.hpp:68
void set_log_level(log_levels new_level)
Sets the global log level.
Definition: log.hpp:66
logger< true > warning()
Used to log warnings. The logging level is logWARNING.
Definition: log.hpp:305
logger< true > warn()
Used to log warnings. The logging level is logWARNING.
Definition: log.hpp:303
logger< true > info()
Used to log infos. The logging level is logINFO.
Definition: log.hpp:307
The main ViennaSHE namespace. All functionality resides inside this namespace.
Definition: accessors.hpp:40
Provides a streamer which discards all inputs. Similar in effect to a redirection to /dev/null....
Streaming class which only provides operator<< discarding the right hand side.
Definition: nullstream.hpp:31