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 <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Assuming g_verbose is defined globally or we pass it
|
// Assuming g_verbose is defined globally or we pass it
|
||||||
extern int g_verbose;
|
extern int g_verbose;
|
||||||
@@ -15,15 +17,18 @@ extern int is_daemon;
|
|||||||
|
|
||||||
#define LOG_MACRO(level, prefix, out_stream, ...) do { \
|
#define LOG_MACRO(level, prefix, out_stream, ...) do { \
|
||||||
if (level == LOG_LEVEL_DEBUG && !g_verbose) break; \
|
if (level == LOG_LEVEL_DEBUG && !g_verbose) break; \
|
||||||
|
flockfile(out_stream); \
|
||||||
fprintf(out_stream, "[VBX-%s] ", prefix); \
|
fprintf(out_stream, "[VBX-%s] ", prefix); \
|
||||||
fprintf(out_stream, __VA_ARGS__); \
|
fprintf(out_stream, __VA_ARGS__); \
|
||||||
fprintf(out_stream, "\n"); \
|
fprintf(out_stream, "\n"); \
|
||||||
fflush(out_stream); \
|
fflush(out_stream); \
|
||||||
|
funlockfile(out_stream); \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
#define LOG_DEBUG(...) LOG_MACRO(LOG_LEVEL_DEBUG, "DEBUG", stdout, __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_INFO(...) LOG_MACRO(LOG_LEVEL_INFO, "INFO", stdout, __VA_ARGS__)
|
||||||
#define LOG_WARN(...) LOG_MACRO(LOG_LEVEL_WARN, "WARN", stderr, __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_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
|
#endif // VBX_LOG_H
|
||||||
@@ -8,10 +8,6 @@
|
|||||||
// Returns 1 on success, 0 on truncation/error
|
// Returns 1 on success, 0 on truncation/error
|
||||||
int safe_snprintf(char *dst, size_t dst_sz, const char *fmt, ...);
|
int safe_snprintf(char *dst, size_t dst_sz, const char *fmt, ...);
|
||||||
|
|
||||||
// fprintf to stderr with printf-style formatting. Returns 1 for convenient
|
|
||||||
// `return errorf(...)` usage.
|
|
||||||
int errorf(const char *fmt, ...);
|
|
||||||
|
|
||||||
// Common runtime helpers
|
// Common runtime helpers
|
||||||
const char *get_runtime_dir(void);
|
const char *get_runtime_dir(void);
|
||||||
void build_pidfile_path(char *buffer, size_t buflen);
|
void build_pidfile_path(char *buffer, size_t buflen);
|
||||||
|
|||||||
Reference in new issue
Block a user