OS  v1.7.5
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 "config/config.h"
12
13namespace qOS {
14
20 #ifdef DOXYGEN
25 class logger final {
26 public:
46 static logger& out( const logSeverity s );
56 static logger& var( const void &v );
57 };
58 #endif
59
63 namespace logger {
64
70 struct source_location {
71 public:
72 #if not defined(__apple_build_version__) and defined(__clang__) and (__clang_major__ >= 9)
73 static constexpr source_location current(const char* fileName = __builtin_FILE(),
74 const char* functionName = __builtin_FUNCTION(),
75 const unsigned long lineNumber = __builtin_LINE(),
76 const unsigned long columnOffset = __builtin_COLUMN() ) noexcept
77 #elif defined(__GNUC__) and (__GNUC__ > 4 or (__GNUC__ == 4 and __GNUC_MINOR__ >= 8))
78 static constexpr source_location current(const char* fileName = __builtin_FILE(),
79 const char* functionName = __builtin_FUNCTION(),
80 const unsigned long lineNumber = __builtin_LINE(),
81 const unsigned long columnOffset = 0 ) noexcept
82 #else
83 static constexpr source_location current(const char* fileName = "unsupported",
84 const char* functionName = "unsupported",
85 const unsigned long lineNumber = __LINE__,
86 const unsigned long columnOffset = 0) noexcept
87 #endif
88 {
89 return source_location( fileName, functionName, lineNumber, columnOffset ); // skipcq: CXX-W2033
90 }
91
92 source_location( const source_location & ) = default;
93 source_location( source_location && ) = default;
94
95 constexpr const char* file_name( void ) const noexcept
96 {
97 return fileName;
98 }
99
100 constexpr const char* function_name( void ) const noexcept
101 {
102 return functionName;
103 }
104
105 constexpr unsigned long line( void ) const noexcept
106 {
107 return lineNumber;
108 }
109
110 constexpr unsigned long column( void ) const noexcept
111 {
112 return columnOffset;
113 }
114
115 private:
116 constexpr source_location( const char* FileName, const char* FunctionName, const unsigned long LineNumber, const unsigned long ColumnOffset ) noexcept
117 : fileName(FileName), functionName(FunctionName), lineNumber(LineNumber), columnOffset(ColumnOffset) {}
118
119 const char* fileName;
120 const char* functionName;
121 const unsigned long lineNumber; // skipcq: CXX-W2010
122 const unsigned long columnOffset; // skipcq: CXX-W2010
123 };
124
125 class lout_base final {
126 public:
127 uint8_t base;
128 explicit lout_base(uint8_t b) : base(b) {}
129 };
133 none = 0,
134 fatal = 1,
135 error = 2,
137 info = 4,
138 debug = 5,
140 };
141
151 class mem final {
152 public:
154 size_t n;
160 explicit mem( size_t nb ) : n( nb ) {}
161 };
162
172 class pre final {
173 public:
175 uint8_t precision;
181 explicit pre( uint8_t p ) : precision( p ) {}
182 };
183
193 extern const lout_base dec;
203 extern const lout_base hex;
213 extern const lout_base oct;
223 extern const lout_base bin;
231 extern const char * const endl;
240 extern const char * const end;
248 extern const char * const nrm;
256 extern const char * const red;
264 extern const char * const grn;
272 extern const char * const yel;
280 extern const char * const blu;
288 extern const char * const mag;
296 extern const char * const cyn;
304 extern const char * const wht;
305
307 class _logger final {
308 private:
309 _logger() = default;
310 _logger( _logger &other ) = delete;
311 void operator=( const _logger & ) = delete;
312 const char *s_str[ 7 ] = { "", "[fatal]: ", "[error]: ", "[warning]: ", "[info]: ", "[debug] ", "" }; // skipcq: CXX-W2066
313 uint8_t base = { 10U };
314 size_t n{ 0U };
315 uint8_t precision { 6U };
316 #if ( Q_TRACE_BUFSIZE < 36 )
317 #define Q_TRACE_BUFSIZE ( 36 )
318 #endif
319 char buffer[ Q_TRACE_BUFSIZE ] = { 0 }; // skipcq: CXX-W2066
320 char preFix[ 5 ] = { 0 }; // skipcq: CXX-W2066
321 util::putChar_t writeChar{ nullptr };
322 void writeNumStr( void ) noexcept;
323 public:
324 static _logger& getInstance( void ) noexcept;
325 template <typename T>
326 inline _logger& _log_integer( const T& v, bool is_int )
327 {
328 /*cstat -MISRAC++2008-5-0-9 -MISRAC++2008-5-0-8*/
329 if ( is_int ) {
330 (void)util::integerToString( static_cast<signed_t>( v ), buffer, base ); // skipcq: CXX-C1000
331 }
332 else {
333 (void)util::unsignedToString( static_cast<unsigned_t>( v ), buffer, base ); // skipcq: CXX-C1000
334 }
335 /*cstat +MISRAC++2008-5-0-9 +MISRAC++2008-5-0-8*/
336 writeNumStr();
337 return *this;
338 }
339 _logger& operator<<( const char& v );
340 _logger& operator<<( const char * s );
341 _logger& operator<<( const short& v );
342 _logger& operator<<( const int& v );
343 _logger& operator<<( const long int& v );
344 _logger& operator<<( const unsigned char& v );
345 _logger& operator<<( const unsigned short& v );
346 _logger& operator<<( const unsigned int& v );
347 _logger& operator<<( const unsigned long& v );
348 _logger& operator<<( const void * const p );
349 _logger& operator<<( const float64_t& v );
350 _logger& operator<<( const lout_base& f );
351 _logger& operator<<( const mem& m );
352 _logger& operator<<( const pre& m );
353 _logger& operator<<( const task& t );
354 _logger& operator<<( const qOS::timer& t );
355 _logger& operator<<( const qOS::stateMachine& sm );
356 _logger& operator<<( const qOS::sm::state& s );
357 _logger& operator<<( const qOS::trigger& t );
358 _logger& operator<<( const qOS::input::channel& in );
359 _logger& operator<<( const qOS::string & s );
360
361 friend _logger& out( const logSeverity s, const source_location &loc ) noexcept;
362 friend void setOutputFcn( util::putChar_t fcn );
363 };
364 extern _logger& _logger_out; // skipcq: CXX-W2011, CXX-W2009
372
374 _logger& out( const logSeverity s = logSeverity::none, const source_location &loc = source_location::current() ) noexcept;
375 /* cppcheck-suppress functionStatic */
376 inline const char * var( const char * vname ){ return vname; }
380 }
382}
383
385#define var(v) var( #v ) << '=' << (v)
388#endif /*QOS_CPP_LOGGER*/
Definition input.hpp:97
Class that sets the number of bytes to be logged when a pointer is being used after....
Definition logger.hpp:151
mem(size_t nb)
Instantiates a memory specifier to logger nb bytes.
Definition logger.hpp:160
Class that sets the decimal precision to be used to format floating-point values on logger operations...
Definition logger.hpp:172
pre(uint8_t p)
Instantiates a precision specifier of p decimal points.
Definition logger.hpp:181
The global class to output logging streams. Its usage requires the static method: logger::out()
Definition logger.hpp:25
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>
A state object.
Definition fsm.hpp:542
A FSM(Finite State Machine) object.
Definition fsm.hpp:822
A task node object.
Definition task.hpp:348
A non-blocking Timer object.
Definition timer.hpp:26
#define Q_TRACE_BUFSIZE
Size for the trace internal buffer.
Definition config.h:95
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:256
const char *const cyn
Set colored output to "cyan" after the usage of this statement Example:
Definition logger.hpp:296
const char *const yel
Set colored output to "yellow" after the usage of this statement Example:
Definition logger.hpp:272
const char *const blu
Set colored output to "blue" after the usage of this statement Example:
Definition logger.hpp:280
const char *const wht
Set colored output to "white" after the usage of this statement Example:
Definition logger.hpp:304
logSeverity
Definition logger.hpp:132
const lout_base bin
Modifies the default numeric base to binary for integer logger output Example:
Definition logger.hpp:223
const char *const grn
Set colored output to "green" after the usage of this statement Example:
Definition logger.hpp:264
const lout_base oct
Modifies the default numeric base to octal for integer logger output Example:
Definition logger.hpp:213
const char *const mag
Set colored output to "magenta" after the usage of this statement Example:
Definition logger.hpp:288
const lout_base hex
Modifies the default numeric base to hexadecimal for integer logger output Example:
Definition logger.hpp:203
const lout_base dec
Modifies the default numeric base to decimal for integer logger output Example:
Definition logger.hpp:193
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:248
const char *const end
Inserts a new-line character to the logger output and restore the default color Example:
Definition logger.hpp:240
const char *const endl
Inserts a new-line character to the logger output. Example:
Definition logger.hpp:231
@ none
Definition logger.hpp:133
@ info
Definition logger.hpp:137
@ warning
Definition logger.hpp:136
@ error
Definition logger.hpp:135
@ verbose
Definition logger.hpp:139
@ fatal
Definition logger.hpp:134
@ debug
Definition logger.hpp:138
trigger
An enum with all the possible values for the event_t::getTrigger() method.
Definition task.hpp:23
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
double float64_t
A type to instantiate a single-precision variable of 64-bits IEEE 754.
Definition types.hpp:87
OS/Kernel interfaces.
Definition bytebuffer.hpp:7