UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Console.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2020 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
24
25namespace Utils {
26
27 using istream_type = std::basic_istream<char>;
28 using ostream_type = std::basic_ostream<char>;
29 using string = std::string;
30
32
36 string basename( string_view filename );
37
52 class Console {
53 mutable std::mutex m_message_mutex;
54
55 #ifndef DOXYGEN_SHOULD_SKIP_THIS
56 public:
58 class Console_style {
59 public:
60 rang::style s;
61 rang::fg f;
62 rang::bg b;
63 };
64 #endif
65
66 private:
67 ostream_type * m_stream{nullptr};
68 int m_level{4};
69
70 Console_style m_message_style = { rang::style::reset, rang::fg::reset, rang::bg::reset };
71 Console_style m_warning_style = { rang::style::reset, rang::fg::yellow, rang::bg::reset };
72 Console_style m_error_style = { rang::style::italic, rang::fg::red, rang::bg::reset };
73 Console_style m_fatal_style = { rang::style::underline, rang::fg::red, rang::bg::reset };
74
75 public:
77 Console() = delete;
78
80 Console( Console const & ) = delete;
81
83
87 explicit Console( ostream_type * stream = &std::cout, int level = 4 );
88
91
93
100 void change_level( int new_level );
102
106 void changeLevel( int new_level ) { this->change_level(new_level); }
107
109
112 void change_stream( ostream_type * new_stream ) { m_stream = new_stream; }
114
118 void changeStream( ostream_type * new_stream ) { m_stream = new_stream; }
119
121
124 int get_level() const { return m_level; }
126
130 int getLevel() const { return m_level; }
131
133
136 ostream_type * get_stream() const { return m_stream; }
138
142 ostream_type * getStream() const { return m_stream; }
143
145 void flush() const { m_stream->flush(); }
146
148
152 void message( string_view msg, int msg_level = 4 ) const;
153
155
160 void semaphore( unsigned ryg, string_view msg, int msg_level = 0 ) const;
161
163
168 void colors( unsigned c, string_view msg, int msg_level = 0 ) const;
169
171
174 void warning( string_view msg ) const; // level >= 2
175
177
180 void error( string_view msg ) const; // level >= 1
181
183
186 void fatal( string_view msg ) const; // level >= 0
187
189
193 void black( string_view msg, int msg_level = 0 ) const;
194
196
200 void red( string_view msg, int msg_level = 0 ) const;
201
203
207 void green( string_view msg, int msg_level = 0 ) const;
208
210
214 void yellow( string_view msg, int msg_level = 0 ) const;
215
217
221 void blue( string_view msg, int msg_level = 0 ) const;
222
224
228 void magenta( string_view msg, int msg_level = 0 ) const;
229
231
235 void cyan( string_view msg, int msg_level = 0 ) const;
236
238
242 void gray( string_view msg, int msg_level = 0 ) const;
243
245
249 void black_reversed( string_view msg, int msg_level = 0 ) const;
250
252
256 void red_reversed( string_view msg, int msg_level = 0 ) const;
257
259
263 void green_reversed( string_view msg, int msg_level = 0 ) const;
264
266
270 void yellow_reversed( string_view msg, int msg_level = 0 ) const;
271
273
277 void blue_reversed( string_view msg, int msg_level = 0 ) const;
278
280
284 void magenta_reversed( string_view msg, int msg_level = 0 ) const;
285
287
291 void cyan_reversed( string_view msg, int msg_level = 0 ) const;
292
294
298 void gray_reversed( string_view msg, int msg_level = 0 ) const;
299
304 void
306 rang::style const & s,
307 rang::fg const & f,
308 rang::bg const & b
309 ) {
310 m_message_style.s = s; m_message_style.f = f; m_message_style.b = b;
311 }
312
318 void
320 rang::style const & s,
321 rang::fg const & f,
322 rang::bg const & b
323 ) {
324 this->set_message_style( s, f, b );
325 }
326
331 void
333 rang::style const & s,
334 rang::fg const & f,
335 rang::bg const & b
336 ) {
337 m_warning_style.s = s;
338 m_warning_style.f = f;
339 m_warning_style.b = b;
340 }
341
347 void
349 rang::style const & s,
350 rang::fg const & f,
351 rang::bg const & b
352 ) {
353 this->set_warning_style( s, f, b );
354 }
355
360 void
362 rang::style const & s,
363 rang::fg const & f,
364 rang::bg const & b
365 ) {
366 m_error_style.s = s;
367 m_error_style.f = f;
368 m_error_style.b = b;
369 }
370
376 void
378 rang::style const & s,
379 rang::fg const & f,
380 rang::bg const & b
381 ) {
382 this->set_error_style( s, f, b );
383 }
384
389 void
391 rang::style const & s,
392 rang::fg const & f,
393 rang::bg const & b
394 ) {
395 m_fatal_style.s = s;
396 m_fatal_style.f = f;
397 m_fatal_style.b = b;
398 }
399
405 void
407 rang::style const & s,
408 rang::fg const & f,
409 rang::bg const & b
410 ) {
411 this->set_fatal_style( s, f, b );
412 }
413
416 void
417 set_off() const {
418 #ifndef UTILS_OS_WINDOWS
419 rang::setControlMode( rang::control::Off );
420 #endif
421 }
422
426 void setOff() const { this->set_off(); }
427
430 void
431 set_auto() const {
432 #ifdef UTILS_OS_WINDOWS
433 rang::setWinTermMode( rang::winTerm::Auto );
434 #else
435 rang::setControlMode( rang::control::Auto );
436 #endif
437 }
438
442 void setAuto() const { this->set_auto(); }
443 };
444
445} // namespace Utils
~Console()
Destructor.
Definition Console.hxx:90
void set_warning_style(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:332
void setAuto() const
Definition Console.hxx:442
void set_error_style(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:361
void colors(unsigned c, string_view msg, int msg_level=0) const
Output a message with specified colors.
void set_off() const
Definition Console.hxx:417
void setOff() const
Definition Console.hxx:426
void red(string_view msg, int msg_level=0) const
Output a message in red color.
ostream_type * getStream() const
Get the current output stream.
Definition Console.hxx:142
void black_reversed(string_view msg, int msg_level=0) const
Output a message in black reversed color.
void setFatalStyle(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:406
Console(ostream_type *stream=&std::cout, int level=4)
Constructor with stream and level parameters.
void blue(string_view msg, int msg_level=0) const
Output a message in blue color.
void green_reversed(string_view msg, int msg_level=0) const
Output a message in green reversed color.
void blue_reversed(string_view msg, int msg_level=0) const
Output a message in blue reversed color.
void message(string_view msg, int msg_level=4) const
Output a message at a specified level.
void magenta(string_view msg, int msg_level=0) const
Output a message in magenta color.
int getLevel() const
Get the current message level.
Definition Console.hxx:130
Console(Console const &)=delete
Deleted copy constructor.
void setWarningStyle(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:348
void changeStream(ostream_type *new_stream)
Change the output stream.
Definition Console.hxx:118
void error(string_view msg) const
Output an error message.
void yellow_reversed(string_view msg, int msg_level=0) const
Output a message in yellow reversed color.
void set_auto() const
Definition Console.hxx:431
ostream_type * get_stream() const
Get the current output stream.
Definition Console.hxx:136
void gray_reversed(string_view msg, int msg_level=0) const
Output a message in gray reversed color.
Console()=delete
Deleted default constructor.
void setErrorStyle(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:377
void fatal(string_view msg) const
Output a fatal message.
void green(string_view msg, int msg_level=0) const
Output a message in green color.
void changeLevel(int new_level)
Change the message level.
Definition Console.hxx:106
void change_stream(ostream_type *new_stream)
Change the output stream.
Definition Console.hxx:112
void flush() const
Flush the output stream.
Definition Console.hxx:145
void yellow(string_view msg, int msg_level=0) const
Output a message in yellow color.
void set_fatal_style(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:390
void semaphore(unsigned ryg, string_view msg, int msg_level=0) const
Output a semaphore message.
void gray(string_view msg, int msg_level=0) const
Output a message in gray color.
void black(string_view msg, int msg_level=0) const
Output a message in black color.
void cyan_reversed(string_view msg, int msg_level=0) const
Output a message in cyan reversed color.
void red_reversed(string_view msg, int msg_level=0) const
Output a message in red reversed color.
void cyan(string_view msg, int msg_level=0) const
Output a message in cyan color.
void magenta_reversed(string_view msg, int msg_level=0) const
Output a message in magenta reversed color.
void setMessageStyle(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:319
void set_message_style(rang::style const &s, rang::fg const &f, rang::bg const &b)
Definition Console.hxx:305
void warning(string_view msg) const
Output a warning message.
void change_level(int new_level)
Change the message level.
int get_level() const
Get the current message level.
Definition Console.hxx:124
Definition SystemUtils.cc:39
std::basic_istream< char > istream_type
Type for input stream.
Definition Console.hxx:27
std::basic_ostream< char > ostream_type
Type for output stream.
Definition Console.hxx:28
std::string string
Type for string.
Definition Console.hxx:29
string basename(string_view filename)
Get the base name of a file.
fg
Definition rang.hxx:38
@ reset
Definition rang.hxx:47
@ red
Definition rang.hxx:40
@ yellow
Definition rang.hxx:42
@ Auto
Definition rang.hxx:86
@ Off
Definition rang.hxx:85
bg
Definition rang.hxx:50
@ reset
Definition rang.hxx:59
style
Definition rang.hxx:25
@ italic
Definition rang.hxx:29
@ underline
Definition rang.hxx:30
@ reset
Definition rang.hxx:26
@ Auto
Definition rang.hxx:92