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:
sankalp committed 2026-02-22 02:35:26 +05:45
1 parent 21cada4969
commit 373e64fcad
2 files changed
+5 -4

No files matched your search

+5
View File
@@ -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_PERROR(msg) LOG_MACRO(LOG_LEVEL_ERROR, "ERROR", stderr, "%s: %s", msg, strerror(errno))
#endif // VBX_LOG_H
-4
View File
@@ -8,10 +8,6 @@
// Returns 1 on success, 0 on truncation/error
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
const char *get_runtime_dir(void);
void build_pidfile_path(char *buffer, size_t buflen);