OS  v1.8.0
Documentation
Loading...
Searching...
No Matches
logger.hpp
1#ifndef QOS_CPP_LOGGER
2#define QOS_CPP_LOGGER
3
4#include "include/types.hpp"
5#include "include/util.hpp"
6#include "include/clock.hpp"
7#include "include/task.hpp"
8#include "include/fsm.hpp"
9#include "include/timer.hpp"
10#include "include/input.hpp"
11#include "include/queue.hpp"
12#include "include/memory.hpp"
13#include "config/config.h"
14
15namespace qOS {
16
21
22 #ifdef DOXYGEN
27 class logger final {
28 public:
48 static logger& out( const logSeverity s );
58 static logger& var( const void &v );
59 };
60 #endif
61
65 namespace logger {
66
70
72 struct source_location {
73 public:
74 #if not defined(__apple_build_version__) and defined(__clang__) and (__clang_major__ >= 9)
75 static constexpr source_location current(const char* fileName = __builtin_FILE(),
76 const char* functionName = __builtin_FUNCTION(),
77 const unsigned long lineNumber = __builtin_LINE(),
78 const unsigned long columnOffset = __builtin_COLUMN() ) noexcept
79 #elif defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8))
80 static constexpr source_location current(const char* fileName = __builtin_FILE(),
81 const char* functionName = __builtin_FUNCTION(),
82 const unsigned long lineNumber = __builtin_LINE(),
83 const unsigned long columnOffset = 0 ) noexcept
84 #else
85 static constexpr source_location current(const char* fileName = "unsupported",
86 const char* functionName = "unsupported",
87 const unsigned long lineNumber = __LINE__,
88 const unsigned long columnOffset = 0) noexcept
89 #endif
90 {
91 return source_location( fileName, functionName, lineNumber, columnOffset ); // skipcq: CXX-W2033
92 }
93
94 source_location( const source_location & ) = default;
95 source_location( source_location && ) = default;
96
97 constexpr const char* file_name( void ) const noexcept
98 {
99 return fileName;
100 }
101
102 constexpr const char* function_name( void ) const noexcept
103 {
104 return functionName;
105 }
106
107 constexpr unsigned long line( void ) const noexcept
108 {
109 return lineNumber;
110 }
111
112 constexpr unsigned long column( void ) const noexcept
113 {
114 return columnOffset;
115 }
116
117 private:
118 constexpr source_location( const char* FileName, const char* FunctionName, const unsigned long LineNumber, const unsigned long ColumnOffset ) noexcept
119 : fileName(FileName), functionName(FunctionName), lineNumber(LineNumber), columnOffset(ColumnOffset) {}
120
121 const char* fileName;
122 const char* functionName;
123 const unsigned long lineNumber; // skipcq: CXX-W2010
124 const unsigned long columnOffset; // skipcq: CXX-W2010
125 };
126
127 class lout_base final {
128 public:
129 uint8_t base;
130 explicit lout_base(uint8_t b) : base(b) {}
131 };
133
135 none = 0,
136 fatal = 1,
137 error = 2,
139 info = 4,
140 debug = 5,
142 };
143
153 class mem final {
154 public:
156 size_t n;
162 explicit mem( size_t nb ) : n( nb ) {}
163 };
164
174 class pre final {
175 public:
177 uint8_t precision;
183 explicit pre( uint8_t p ) : precision( p ) {}
184 };
185
195 extern const lout_base dec;
205 extern const lout_base hex;
215 extern const lout_base oct;
225 extern const lout_base bin;
233 extern const char * const endl;
242 extern const char * const end;
250 extern const char * const nrm;
258 extern const char * const red;
266 extern const char * const grn;
274 extern const char * const yel;
282 extern const char * const blu;
290 extern const char * const mag;
298 extern const char * const cyn;
306 extern const char * const wht;
307
308 struct ChainLoggerProxy;
309
311 class _logger final : private nonCopyable {
312 private:
313 _logger() = default;
314 _logger( _logger &other ) = delete;
315 void operator=( const _logger & ) = delete;
316 const char *s_str[ 7 ] = { "", "[fatal]: ", "[error]: ", "[warning]: ", "[info]: ", "[debug] ", "" }; // skipcq: CXX-W2066
317 uint8_t base = { 10U };
318 size_t n{ 0U };
319 uint8_t precision { 6U };
320 #if ( Q_TRACE_BUFSIZE < 36 )
321 #define Q_TRACE_BUFSIZE ( 36 )
322 #endif
323 char buffer[ Q_TRACE_BUFSIZE ] = { 0 }; // skipcq: CXX-W2066
324 char preFix[ 5 ] = { 0 }; // skipcq: CXX-W2066
325 util::putChar_t writeChar{ nullptr };
326 void writeNumStr( void ) noexcept;
327 public:
328 static _logger& getInstance( void ) noexcept;
329
330 template <typename T>
331 auto toLog(const T& v) -> decltype((void)(T(1) % 1), (void)(-T(1)), void()) // only valid for integrals
332 {
333 /*cstat -CERT-INT31-C_a -MISRAC++2008-5-0-8 -MISRAC++2008-5-0-9 -MISRAC++2008-0-1-2_b -MISRAC++2008-0-1-2_a*/
334 if (T(-1) < T(0)) {
335 (void)util::integerToString(static_cast<signed_t>(v), buffer, base); // skipcq: CXX-C1000
336 } else {
337 (void)util::unsignedToString(static_cast<unsigned_t>(v), buffer, base); // skipcq: CXX-C1000
338 }
339 /*cstat +CERT-INT31-C_a +MISRAC++2008-5-0-8 +MISRAC++2008-5-0-9 +MISRAC++2008-0-1-2_b +MISRAC++2008-0-1-2_a*/
340 writeNumStr();
341 }
342
343 void toLog( const char& v );
344 void toLog( const char * s );
345 void toLog( const void * const p );
346
347 void toLog( const float32_t& v );
348 void toLog( const float64_t& v );
349 void toLog( const lout_base& f );
350 void toLog( const mem& m );
351 void toLog( const pre& m );
352 void toLog( const qOS::task& t );
353 void toLog( const qOS::timer& t );
354 void toLog( const qOS::stateMachine& sm );
355 void toLog( const qOS::sm::state& s );
356 void toLog( const qOS::trigger& t );
357 void toLog( const qOS::globalState& s );
358 void toLog( const qOS::taskEvent& e );
359 void toLog( const qOS::taskState& v );
360 void toLog( const qOS::queue& v );
361 void toLog( const qOS::mem::pool& v );
362 void toLog( const qOS::input::channel& in );
363 void toLog( const qOS::input::watcher& v );
364 void toLog( const qOS::sm::signalID& v );
365 void toLog( const qOS::sm::status& v );
366 void toLog( const qOS::sm::stateHandler& v );
367 void toLog( const qOS::string & s );
368
369 friend ChainLoggerProxy out( const logSeverity s, const source_location &loc ) noexcept;
370 friend void setOutputFcn( util::putChar_t fcn );
371 friend class ChainLoggerProxy;
372 };
373 extern _logger& _logger_out; // skipcq: CXX-W2011, CXX-W2009
374
375 #define IMPL_CHAIN_LOGGER_OPERATOR( Type ) \
376 ChainLoggerProxy& operator<<( Type v ) \
377 { \
378 parent.toLog( v ); \
379 return *this; \
380 } \
381
382 struct ChainLoggerProxy {
383 _logger& parent; // skipcq: CXX-W2010
384 explicit ChainLoggerProxy( _logger& p) : parent(p) {}
385 ChainLoggerProxy( const ChainLoggerProxy& ) = delete;
386 ChainLoggerProxy( ChainLoggerProxy&& other ) noexcept : parent( other.parent ) {}
387 ~ChainLoggerProxy();
388
389 template<typename T>
390 auto operator<<(const T& v) -> decltype((void)(v + 0), *this)
391 {
392 parent.toLog( v );
393 return *this;
394 }
395 IMPL_CHAIN_LOGGER_OPERATOR( const char& )
396 IMPL_CHAIN_LOGGER_OPERATOR( const char * )
397 IMPL_CHAIN_LOGGER_OPERATOR( const void * const )
398 IMPL_CHAIN_LOGGER_OPERATOR( const lout_base& )
399 IMPL_CHAIN_LOGGER_OPERATOR( const mem& )
400 IMPL_CHAIN_LOGGER_OPERATOR( const pre& )
401 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::task& )
402 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::timer& )
403 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::stateMachine& )
404 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::sm::state& )
405 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::trigger& )
406 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::globalState& )
407 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::taskEvent& )
408 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::taskState& )
409 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::queue& )
410 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::mem::pool& )
411 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::input::channel& )
412 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::input::watcher& )
413 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::sm::signalID& )
414 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::sm::status& )
415 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::sm::stateHandler& )
416 IMPL_CHAIN_LOGGER_OPERATOR( const qOS::string& )
417 };
419
425
427 ChainLoggerProxy out( const logSeverity s = logSeverity::none, const source_location &loc = source_location::current() ) noexcept;
428 /* cppcheck-suppress functionStatic */
429 inline const char * var( const char * vname ){ return vname; }
431
433 }
434
435}
436
438#define var(v) var( #v ) << '=' << (v)
440
441#endif /*QOS_CPP_LOGGER*/
mem(size_t nb)
Instantiates a memory specifier to logger nb bytes.
Definition logger.hpp:162
pre(uint8_t p)
Instantiates a precision specifier of p decimal points.
Definition logger.hpp:183
static logger & out(const logSeverity s)
Specify a new logger output with severity level of information (if defined).
static logger & var(const void &v)
Specify that the variable given by v should be printed with its own name : <var::name> = <var::value>
#define Q_TRACE_BUFSIZE
Size for the trace internal buffer.
Definition config.h:95
status
This enumeration defines the built-in state-execution status values that can be used as return value ...
Definition fsm.hpp:193
signalID
The type for signal ID.
Definition fsm.hpp:34
void(*)(void *, const char) putChar_t
Pointer to function that write-out a single character.
Definition util.hpp:33
const char *const red
Set colored output to "red" after the usage of this statement Example:
Definition logger.hpp:258
const char *const cyn
Set colored output to "cyan" after the usage of this statement Example:
Definition logger.hpp:298
const char *const yel
Set colored output to "yellow" after the usage of this statement Example:
Definition logger.hpp:274
const char *const blu
Set colored output to "blue" after the usage of this statement Example:
Definition logger.hpp:282
const char *const wht
Set colored output to "white" after the usage of this statement Example:
Definition logger.hpp:306
logSeverity
Definition logger.hpp:134
const lout_base bin
Modifies the default numeric base to binary for integer logger output Example:
Definition logger.hpp:225
const char *const grn
Set colored output to "green" after the usage of this statement Example:
Definition logger.hpp:266
const lout_base oct
Modifies the default numeric base to octal for integer logger output Example:
Definition logger.hpp:215
const char *const mag
Set colored output to "magenta" after the usage of this statement Example:
Definition logger.hpp:290
const lout_base hex
Modifies the default numeric base to hexadecimal for integer logger output Example:
Definition logger.hpp:205
const lout_base dec
Modifies the default numeric base to decimal for integer logger output Example:
Definition logger.hpp:195
void setOutputFcn(util::putChar_t fcn)
Set the output method for the logger stream.
const char *const nrm
Set colored output to "normal" after the usage of this statement Example:
Definition logger.hpp:250
const char *const end
Inserts a new-line character to the logger output and restore the default color Example:
Definition logger.hpp:242
const char *const endl
Inserts a new-line character to the logger output. Example:
Definition logger.hpp:233
@ none
Definition logger.hpp:135
@ info
Definition logger.hpp:139
@ warning
Definition logger.hpp:138
@ error
Definition logger.hpp:137
@ verbose
Definition logger.hpp:141
@ fatal
Definition logger.hpp:136
@ debug
Definition logger.hpp:140
globalState
An enum to describe the task global states.
Definition task.hpp:86
trigger
An enum with all the possible values for the event_t::getTrigger() method.
Definition task.hpp:23
taskState
An enum that defines the possible task operational states.
Definition task.hpp:254
unsigned long unsigned_t
A type to instantiate an unsigned variable.
Definition types.hpp:72
long int signed_t
A type to instantiate an signed variable.
Definition types.hpp:77
Logger interfaces.
Definition logger.hpp:65
OS/Kernel interfaces.
Definition bytebuffer.hpp:7