diff --git a/include/common/log.h b/include/common/log.h index e358e8d..2a28cb2 100644 --- a/include/common/log.h +++ b/include/common/log.h @@ -3,6 +3,8 @@ #include #include +#include +#include // 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 diff --git a/include/common/utils.h b/include/common/utils.h index b0fa16a..675e85d 100644 --- a/include/common/utils.h +++ b/include/common/utils.h @@ -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);