feat(include/common): add thread‑safe logging macros and error handling
- New Features - Wrap log output with flockfile/funlockfile to guarantee atomic writes. - Add LOG_PERROR macro to report messages with errno details. - Enhancements - Include <errno.h> and <string.h> for robust error handling. - Remove unused errorf declaration. - Bug Fixes - None.
This commit is contained in:
2 files changed
+9
-8
No files matched your search
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
// Assuming g_verbose is defined globally or we pass it
|
||||
extern int g_verbose;
|
||||
@@ -15,15 +17,18 @@ extern int is_daemon;
|
||||
|
||||
#define LOG_MACRO(level, prefix, out_stream, ...) do { \
|
||||
if (level == LOG_LEVEL_DEBUG && !g_verbose) break; \
|
||||
flockfile(out_stream); \
|
||||
fprintf(out_stream, "[VBX-%s] ", prefix); \
|
||||
fprintf(out_stream, __VA_ARGS__); \
|
||||
fprintf(out_stream, "\n"); \
|
||||
fflush(out_stream); \
|
||||
funlockfile(out_stream); \
|
||||
} while(0)
|
||||
|
||||
#define LOG_DEBUG(...) LOG_MACRO(LOG_LEVEL_DEBUG, "DEBUG", stdout, __VA_ARGS__)
|
||||
#define LOG_INFO(...) LOG_MACRO(LOG_LEVEL_INFO, "INFO", stdout, __VA_ARGS__)
|
||||
#define LOG_WARN(...) LOG_MACRO(LOG_LEVEL_WARN, "WARN", stderr, __VA_ARGS__)
|
||||
#define LOG_ERROR(...) LOG_MACRO(LOG_LEVEL_ERROR, "ERROR", stderr, __VA_ARGS__)
|
||||
#define LOG_DEBUG(...) LOG_MACRO(LOG_LEVEL_DEBUG, "DEBUG", stdout, __VA_ARGS__)
|
||||
#define LOG_INFO(...) LOG_MACRO(LOG_LEVEL_INFO, "INFO", stdout, __VA_ARGS__)
|
||||
#define LOG_WARN(...) LOG_MACRO(LOG_LEVEL_WARN, "WARN", stderr, __VA_ARGS__)
|
||||
#define LOG_ERROR(...) LOG_MACRO(LOG_LEVEL_ERROR, "ERROR", stderr, __VA_ARGS__)
|
||||
#define LOG_PERROR(msg) LOG_MACRO(LOG_LEVEL_ERROR, "ERROR", stderr, "%s: %s", msg, strerror(errno))
|
||||
|
||||
#endif // VBX_LOG_H
|
||||
Reference in new issue
Block a user