HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BaseLogger.h
Go to the documentation of this file.
1
19#pragma once
20
21#include "HardwareTypes.h"
22#include <cstdarg>
23#include <cstdint>
24#include <functional>
25#include <string>
26#include <string_view>
27
45#define HF_LOGGER_ERR_LIST(X) \
46 /* Success codes */ \
47 X(LOGGER_SUCCESS, 0, "Success") \
48 \
49 /* General errors */ \
50 X(LOGGER_ERR_FAILURE, 1, "General failure") \
51 X(LOGGER_ERR_NOT_INITIALIZED, 2, "Not initialized") \
52 X(LOGGER_ERR_ALREADY_INITIALIZED, 3, "Already initialized") \
53 X(LOGGER_ERR_INVALID_PARAMETER, 4, "Invalid parameter") \
54 X(LOGGER_ERR_NULL_POINTER, 5, "Null pointer") \
55 X(LOGGER_ERR_OUT_OF_MEMORY, 6, "Out of memory") \
56 \
57 /* Configuration errors */ \
58 X(LOGGER_ERR_INVALID_CONFIGURATION, 7, "Invalid configuration") \
59 X(LOGGER_ERR_UNSUPPORTED_OPERATION, 8, "Unsupported operation") \
60 X(LOGGER_ERR_RESOURCE_BUSY, 9, "Resource busy") \
61 X(LOGGER_ERR_RESOURCE_UNAVAILABLE, 10, "Resource unavailable") \
62 \
63 /* Output errors */ \
64 X(LOGGER_ERR_WRITE_FAILURE, 11, "Write failure") \
65 X(LOGGER_ERR_OUTPUT_BUFFER_FULL, 12, "Output buffer full") \
66 X(LOGGER_ERR_FORMAT_ERROR, 13, "Format error") \
67 X(LOGGER_ERR_ENCODING_ERROR, 14, "Encoding error") \
68 \
69 /* System errors */ \
70 X(LOGGER_ERR_SYSTEM_ERROR, 15, "System error") \
71 X(LOGGER_ERR_PERMISSION_DENIED, 16, "Permission denied") \
72 X(LOGGER_ERR_OPERATION_ABORTED, 17, "Operation aborted") \
73 \
74 /* Extended errors */ \
75 X(LOGGER_ERR_NOT_SUPPORTED, 18, "Operation not supported") \
76 X(LOGGER_ERR_DRIVER_ERROR, 19, "Driver error") \
77 X(LOGGER_ERR_INVALID_STATE, 20, "Invalid state") \
78 X(LOGGER_ERR_INVALID_ARG, 21, "Invalid argument") \
79 X(LOGGER_ERR_TIMEOUT, 22, "Timeout") \
80 X(LOGGER_ERR_BUFFER_OVERFLOW, 23, "Buffer overflow") \
81 X(LOGGER_ERR_UNKNOWN, 24, "Unknown error")
82
87#define HF_LOGGER_ERR_ENUM(name, value, description) name = value,
89#undef HF_LOGGER_ERR_ENUM
90
95enum class hf_log_level_t : hf_u8_t {
96 LOG_LEVEL_NONE = 0,
97 LOG_LEVEL_ERROR = 1,
98 LOG_LEVEL_WARN = 2,
99 LOG_LEVEL_INFO = 3,
100 LOG_LEVEL_DEBUG = 4,
102};
103
109 LOG_OUTPUT_NONE = 0,
110 LOG_OUTPUT_UART = 1,
111 LOG_OUTPUT_USB = 2,
112 LOG_OUTPUT_FILE = 3,
115};
116
122 LOG_FORMAT_NONE = 0,
123 LOG_FORMAT_TIMESTAMP = (1U << 0),
124 LOG_FORMAT_LEVEL = (1U << 1),
125 LOG_FORMAT_TAG = (1U << 2),
126 LOG_FORMAT_FILE_LINE = (1U << 3),
127 LOG_FORMAT_FUNCTION = (1U << 4),
128 LOG_FORMAT_THREAD_ID = (1U << 5),
129 LOG_FORMAT_COLORS = (1U << 6),
131};
132
133// Bitwise operators for hf_log_format_t enum class
135 return static_cast<hf_log_format_t>(static_cast<hf_u32_t>(a) | static_cast<hf_u32_t>(b));
136}
137
139 return static_cast<hf_log_format_t>(static_cast<hf_u32_t>(a) & static_cast<hf_u32_t>(b));
140}
141
143 return static_cast<hf_log_format_t>(static_cast<hf_u32_t>(a) ^ static_cast<hf_u32_t>(b));
144}
145
147 return static_cast<hf_log_format_t>(~static_cast<hf_u32_t>(a));
148}
149
165
182
198
214
215//==============================================================================
216// UTILITY FUNCTIONS
217//==============================================================================
218
224constexpr std::string_view HfLoggerErrToString(hf_logger_err_t err) noexcept {
225 switch (err) {
226#define X(NAME, VALUE, DESC) \
227 case hf_logger_err_t::NAME: \
228 return DESC;
230#undef X
231 default:
233 }
234}
235
241inline const char* HfLogLevelToString(hf_log_level_t level) noexcept {
242 switch (level) {
244 return "NONE";
246 return "ERROR";
248 return "WARN";
250 return "INFO";
252 return "DEBUG";
254 return "VERBOSE";
255 default:
256 return "UNKNOWN";
257 }
258}
259
265inline const char* HfLogLevelToShortString(hf_log_level_t level) noexcept {
266 switch (level) {
268 return "N";
270 return "E";
272 return "W";
274 return "I";
276 return "D";
278 return "V";
279 default:
280 return "?";
281 }
282}
283
289
295
296//==============================================================================
297// CONVENIENCE MACROS
298//==============================================================================
299
303#define HF_LOG_ERROR(tag, format, ...) \
304 LogWithLocation(hf_log_level_t::LOG_LEVEL_ERROR, tag, __FILE__, __LINE__, __FUNCTION__, format, \
305 ##__VA_ARGS__)
306
310#define HF_LOG_WARN(tag, format, ...) \
311 LogWithLocation(hf_log_level_t::LOG_LEVEL_WARN, tag, __FILE__, __LINE__, __FUNCTION__, format, \
312 ##__VA_ARGS__)
313
317#define HF_LOG_INFO(tag, format, ...) \
318 LogWithLocation(hf_log_level_t::LOG_LEVEL_INFO, tag, __FILE__, __LINE__, __FUNCTION__, format, \
319 ##__VA_ARGS__)
320
324#define HF_LOG_DEBUG(tag, format, ...) \
325 LogWithLocation(hf_log_level_t::LOG_LEVEL_DEBUG, tag, __FILE__, __LINE__, __FUNCTION__, format, \
326 ##__VA_ARGS__)
327
331#define HF_LOG_VERBOSE(tag, format, ...) \
332 LogWithLocation(hf_log_level_t::LOG_LEVEL_VERBOSE, tag, __FILE__, __LINE__, __FUNCTION__, \
333 format, ##__VA_ARGS__)
334
338#define HF_LOG_IF(condition, level, tag, format, ...) \
339 do { \
340 if (condition) { \
341 LogWithLocation(level, tag, __FILE__, __LINE__, __FUNCTION__, format, ##__VA_ARGS__); \
342 } \
343 } while (0)
344
359public:
363 virtual ~BaseLogger() noexcept = default;
364
365 //==============================================================================
366 // INITIALIZATION AND CONFIGURATION
367 //==============================================================================
368
374 virtual hf_logger_err_t Initialize(const hf_logger_config_t& config) noexcept = 0;
375
380 virtual hf_logger_err_t Deinitialize() noexcept = 0;
381
386 virtual bool IsInitialized() const noexcept = 0;
387
392 virtual bool EnsureInitialized() noexcept = 0;
393
400 virtual hf_logger_err_t SetLogLevel(const char* tag, hf_log_level_t level) noexcept = 0;
401
408 virtual hf_logger_err_t GetLogLevel(const char* tag, hf_log_level_t& level) const noexcept = 0;
409
410 //==============================================================================
411 // LOGGING METHODS
412 //==============================================================================
413
421 virtual hf_logger_err_t Error(const char* tag, const char* format, ...) noexcept = 0;
422
430 virtual hf_logger_err_t Warn(const char* tag, const char* format, ...) noexcept = 0;
431
439 virtual hf_logger_err_t Info(const char* tag, const char* format, ...) noexcept = 0;
440
448 virtual hf_logger_err_t Debug(const char* tag, const char* format, ...) noexcept = 0;
449
457 virtual hf_logger_err_t Verbose(const char* tag, const char* format, ...) noexcept = 0;
458
467 virtual hf_logger_err_t Log(hf_log_level_t level, const char* tag, const char* format,
468 ...) noexcept = 0;
469
478 virtual hf_logger_err_t LogV(hf_log_level_t level, const char* tag, const char* format,
479 va_list args) noexcept = 0;
480
492 virtual hf_logger_err_t LogWithLocation(hf_log_level_t level, const char* tag, const char* file,
493 hf_u32_t line, const char* function, const char* format,
494 ...) noexcept = 0;
495
496 //==============================================================================
497 // UTILITY METHODS
498 //==============================================================================
499
504 virtual hf_logger_err_t Flush() noexcept = 0;
505
512 virtual bool IsLevelEnabled(hf_log_level_t level, const char* tag = nullptr) const noexcept = 0;
513
519 virtual hf_logger_err_t GetStatistics(hf_logger_statistics_t& statistics) const noexcept = 0;
520
526 virtual hf_logger_err_t GetDiagnostics(hf_logger_diagnostics_t& diagnostics) const noexcept = 0;
527
532 virtual hf_logger_err_t ResetStatistics() noexcept = 0;
533
538 virtual hf_logger_err_t ResetDiagnostics() noexcept = 0;
539
544 virtual bool IsHealthy() const noexcept = 0;
545
550 virtual hf_logger_err_t GetLastError() const noexcept = 0;
551
559 hf_u32_t max_length) const noexcept = 0;
560
561 //==============================================================================
562 // DIAGNOSTIC PRINTING METHODS
563 //==============================================================================
564
571 virtual hf_logger_err_t PrintStatistics(const char* tag = nullptr,
572 bool detailed = true) const noexcept = 0;
573
580 virtual hf_logger_err_t PrintDiagnostics(const char* tag = nullptr,
581 bool detailed = true) const noexcept = 0;
582
589 virtual hf_logger_err_t PrintStatus(const char* tag = nullptr,
590 bool detailed = true) const noexcept = 0;
591
592protected:
596 BaseLogger() = default;
597
601 BaseLogger(const BaseLogger&) = delete;
602
606 BaseLogger& operator=(const BaseLogger&) = delete;
607
612
616 BaseLogger& operator=(BaseLogger&&) = delete;
617};
const char * HfLogLevelToShortString(hf_log_level_t level) noexcept
Convert log level to short string.
Definition BaseLogger.h:265
hf_u32_t HfLoggerGetThreadId() noexcept
Get current thread ID.
hf_log_format_t operator|(hf_log_format_t a, hf_log_format_t b)
Definition BaseLogger.h:134
hf_u64_t HfLoggerGetTimestamp() noexcept
Get current timestamp in microseconds.
hf_log_format_t operator&(hf_log_format_t a, hf_log_format_t b)
Definition BaseLogger.h:138
#define X(NAME, VALUE, DESC)
hf_log_format_t operator~(hf_log_format_t a)
Definition BaseLogger.h:146
hf_logger_err_t
Definition BaseLogger.h:88
const char * HfLogLevelToString(hf_log_level_t level) noexcept
Convert log level to string.
Definition BaseLogger.h:241
hf_log_format_t operator^(hf_log_format_t a, hf_log_format_t b)
Definition BaseLogger.h:142
constexpr std::string_view HfLoggerErrToString(hf_logger_err_t err) noexcept
Convert logger error code to string view.
Definition BaseLogger.h:224
Platform-agnostic hardware type definitions for the HardFOC system.
uint32_t hf_u32_t
Platform-agnostic 32-bit unsigned integer type.
Definition HardwareTypes.h:52
uint8_t hf_u8_t
Platform-agnostic 8-bit unsigned integer type.
Definition HardwareTypes.h:40
uint64_t hf_u64_t
Platform-agnostic 64-bit unsigned integer type.
Definition HardwareTypes.h:58
Base logger abstract class.
Definition BaseLogger.h:358
virtual hf_logger_err_t PrintStatus(const char *tag=nullptr, bool detailed=true) const noexcept=0
Print both statistics and diagnostics.
virtual hf_logger_err_t GetLastError() const noexcept=0
Get last error code.
virtual bool EnsureInitialized() noexcept=0
Ensure logger is initialized (lazy initialization)
virtual hf_logger_err_t Debug(const char *tag, const char *format,...) noexcept=0
Log a message at DEBUG level.
virtual hf_logger_err_t ResetDiagnostics() noexcept=0
Reset diagnostics.
virtual hf_logger_err_t Flush() noexcept=0
Flush any buffered output.
virtual hf_logger_err_t GetStatistics(hf_logger_statistics_t &statistics) const noexcept=0
Get logger statistics.
virtual hf_logger_err_t ResetStatistics() noexcept=0
Reset statistics.
virtual hf_logger_err_t Error(const char *tag, const char *format,...) noexcept=0
Log a message at ERROR level.
virtual hf_logger_err_t LogWithLocation(hf_log_level_t level, const char *tag, const char *file, hf_u32_t line, const char *function, const char *format,...) noexcept=0
Log a message with file and line information.
virtual bool IsHealthy() const noexcept=0
Check if logger is healthy.
virtual hf_logger_err_t Deinitialize() noexcept=0
Deinitialize the logger.
virtual ~BaseLogger() noexcept=default
Virtual destructor.
virtual hf_logger_err_t Info(const char *tag, const char *format,...) noexcept=0
Log a message at INFO level.
virtual hf_logger_err_t GetLogLevel(const char *tag, hf_log_level_t &level) const noexcept=0
Get log level for a specific tag.
virtual hf_logger_err_t Verbose(const char *tag, const char *format,...) noexcept=0
Log a message at VERBOSE level.
virtual hf_logger_err_t PrintStatistics(const char *tag=nullptr, bool detailed=true) const noexcept=0
Print statistics to log output.
virtual hf_logger_err_t SetLogLevel(const char *tag, hf_log_level_t level) noexcept=0
Set log level for a specific tag.
virtual hf_logger_err_t LogV(hf_log_level_t level, const char *tag, const char *format, va_list args) noexcept=0
Log a message with va_list (for internal use)
virtual bool IsInitialized() const noexcept=0
Check if logger is initialized.
virtual hf_logger_err_t PrintDiagnostics(const char *tag=nullptr, bool detailed=true) const noexcept=0
Print diagnostics to log output.
virtual hf_logger_err_t Warn(const char *tag, const char *format,...) noexcept=0
Log a message at WARN level.
virtual hf_logger_err_t Initialize(const hf_logger_config_t &config) noexcept=0
Initialize the logger.
virtual hf_logger_err_t Log(hf_log_level_t level, const char *tag, const char *format,...) noexcept=0
Log a message at specified level.
virtual hf_logger_err_t GetLastErrorMessage(char *message, hf_u32_t max_length) const noexcept=0
Get last error message.
virtual hf_logger_err_t GetDiagnostics(hf_logger_diagnostics_t &diagnostics) const noexcept=0
Get logger diagnostics.
virtual bool IsLevelEnabled(hf_log_level_t level, const char *tag=nullptr) const noexcept=0
Check if a log level is enabled for a tag.
hf_log_output_t
Log output destination enumeration.
Definition BaseLogger.h:108
hf_log_format_t
Log format options.
Definition BaseLogger.h:121
#define HF_LOGGER_ERR_LIST(X)
HardFOC Logger error codes macro list.
Definition BaseLogger.h:45
#define HF_LOGGER_ERR_ENUM(name, value, description)
Generate logger error enum from macro list.
Definition BaseLogger.h:87
hf_log_level_t
Log levels enumeration.
Definition BaseLogger.h:95
@ LOG_OUTPUT_NETWORK
Network output.
@ LOG_OUTPUT_USB
USB CDC output.
@ LOG_OUTPUT_UART
UART serial output.
@ LOG_OUTPUT_FILE
File system output.
@ LOG_OUTPUT_CUSTOM
Custom output callback.
@ LOG_OUTPUT_NONE
No output.
@ LOG_FORMAT_FUNCTION
Include function name.
@ LOG_FORMAT_COLORS
Include ANSI colors.
@ LOG_FORMAT_LEVEL
Include log level.
@ LOG_FORMAT_FILE_LINE
Include file and line.
@ LOG_FORMAT_NONE
No formatting.
@ LOG_FORMAT_THREAD_ID
Include thread ID.
@ LOG_FORMAT_TAG
Include tag.
@ LOG_FORMAT_TIMESTAMP
Include timestamp.
@ LOG_LEVEL_WARN
Warning and error messages.
@ LOG_LEVEL_VERBOSE
All messages including verbose.
@ LOG_LEVEL_ERROR
Error messages only.
@ LOG_LEVEL_INFO
Info, warning, and error messages.
@ LOG_LEVEL_DEBUG
Debug, info, warning, and error messages.
@ LOG_LEVEL_NONE
No logging.
Log message structure.
Definition BaseLogger.h:203
const char * file
Source file.
Definition BaseLogger.h:207
hf_u32_t thread_id
Thread ID.
Definition BaseLogger.h:211
hf_log_level_t level
Log level.
Definition BaseLogger.h:204
const char * tag
Message tag.
Definition BaseLogger.h:205
const char * function
Function name.
Definition BaseLogger.h:209
const char * message
Message content.
Definition BaseLogger.h:206
hf_u32_t message_length
Message length.
Definition BaseLogger.h:212
hf_u64_t timestamp
Timestamp.
Definition BaseLogger.h:210
hf_u32_t line
Source line.
Definition BaseLogger.h:208
Logger configuration structure.
Definition BaseLogger.h:154
hf_log_level_t default_level
Default log level.
Definition BaseLogger.h:155
bool enable_performance_monitoring
Enable performance monitoring.
Definition BaseLogger.h:162
bool enable_thread_safety
Enable thread safety.
Definition BaseLogger.h:161
std::function< void(const char *, hf_u32_t) custom_output_callback)
Custom output callback.
Definition BaseLogger.h:163
hf_u32_t flush_interval_ms
Flush interval in milliseconds.
Definition BaseLogger.h:160
hf_log_format_t format_options
Format options.
Definition BaseLogger.h:157
hf_u32_t buffer_size
Internal buffer size.
Definition BaseLogger.h:159
hf_u32_t max_message_length
Maximum message length.
Definition BaseLogger.h:158
hf_log_output_t output_destination
Output destination.
Definition BaseLogger.h:156
Logger diagnostics structure.
Definition BaseLogger.h:187
char last_error_message[256]
Last error message.
Definition BaseLogger.h:196
hf_logger_err_t last_error
Last error code.
Definition BaseLogger.h:190
hf_u32_t error_recovery_count
Error recovery count.
Definition BaseLogger.h:193
hf_u64_t last_health_check
Last health check timestamp.
Definition BaseLogger.h:195
bool is_initialized
Initialization status.
Definition BaseLogger.h:188
bool is_healthy
Health status.
Definition BaseLogger.h:189
hf_u64_t uptime_seconds
Uptime in seconds.
Definition BaseLogger.h:194
hf_u64_t last_error_timestamp
Last error timestamp.
Definition BaseLogger.h:191
hf_u32_t consecutive_errors
Consecutive error count.
Definition BaseLogger.h:192
Logger statistics structure.
Definition BaseLogger.h:170
hf_u64_t last_message_timestamp
Timestamp of last message.
Definition BaseLogger.h:178
hf_u64_t total_bytes_written
Total bytes written.
Definition BaseLogger.h:173
hf_u64_t buffer_overflows
Number of buffer overflows.
Definition BaseLogger.h:176
hf_u64_t max_message_length_seen
Maximum message length seen.
Definition BaseLogger.h:180
hf_u64_t messages_by_level[6]
Messages by level (indexed by hf_log_level_t)
Definition BaseLogger.h:172
hf_u64_t average_message_length
Average message length.
Definition BaseLogger.h:179
hf_u64_t performance_monitor_calls
Number of performance monitor calls.
Definition BaseLogger.h:177
hf_u64_t format_errors
Number of format errors.
Definition BaseLogger.h:175
hf_u64_t write_errors
Number of write errors.
Definition BaseLogger.h:174
hf_u64_t total_messages
Total messages logged.
Definition BaseLogger.h:171