diff options
author | Dimitri Staessens <[email protected]> | 2023-08-17 16:12:43 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2023-08-23 13:09:11 +0200 |
commit | 868c7be8f599404a23f1d5178b1ba18379df1132 (patch) | |
tree | 3a6492d047a5a1b0d63b5c6ae719e74e63d347bf | |
parent | ab8ee0b24335790e49127b1330f9d7bcca7f6bfa (diff) | |
download | ouroboros-868c7be8f599404a23f1d5178b1ba18379df1132.tar.gz ouroboros-868c7be8f599404a23f1d5178b1ba18379df1132.zip |
include: Add option to prefix logs with an ID
This adds the log_xxx_id() macros to print a logline prefixed with a
64-bit identifier in hex format, for instance:
[2a92d78c2f52b76a] Hello ouroboros!
The ID is assumed to be 8 bytes in big endian format (it uses the
HASH_FMT functions and hashes are stored in big endian byte arrays).
The implementation uses the compiler-specific '##' operator to allow
empty variadics. E.g.
func(arg , ## __VA_ARGS__);
will eat the comma if __VA_ARGS__ is empty and thus allow
func(arg);
Signed-off-by: Dimitri Staessens <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
-rw-r--r-- | include/ouroboros/logs.h | 74 |
1 files changed, 53 insertions, 21 deletions
diff --git a/include/ouroboros/logs.h b/include/ouroboros/logs.h index 08ea29c7..479cae4a 100644 --- a/include/ouroboros/logs.h +++ b/include/ouroboros/logs.h @@ -23,18 +23,16 @@ #ifndef OUROBOROS_LOGS_H #define OUROBOROS_LOGS_H -#include <unistd.h> -#include <stdio.h> -#include <stdbool.h> -#include <syslog.h> - #ifndef OUROBOROS_PREFIX #error You must define OUROBOROS_PREFIX before including this file #endif -void log_init(bool sysout); +#include <ouroboros/hash.h> -void log_fini(void); +#include <unistd.h> +#include <stdio.h> +#include <stdbool.h> +#include <syslog.h> #define CLR_RED "\x1b[31m" #define CLR_GREEN "\x1b[32m" @@ -48,27 +46,61 @@ void log_fini(void); extern bool log_syslog; -#define __olog(CLR, LVL, SYSLVL, ...) \ - do { \ - if (log_syslog) { \ - syslog(SYSLVL, __VA_ARGS__); \ - } else { \ - printf(CLR "==%05d== " OUROBOROS_PREFIX \ - "(" LVL "): ", getpid()); \ - printf(__VA_ARGS__); \ - printf(CLR_RESET "\n"); \ - fflush(stdout); \ - } \ +void log_init(bool sysout); + +void log_fini(void); + + +#define __olog(CLR, LVL, SYSLVL, ...) \ + do { \ + if (log_syslog) { \ + syslog(SYSLVL, __VA_ARGS__); \ + } else { \ + printf(CLR "==%05d== " OUROBOROS_PREFIX \ + "(" LVL "): ", getpid()); \ + printf(__VA_ARGS__); \ + printf(CLR_RESET "\n"); \ + fflush(stdout); \ + } \ + } while (0) + +#define __olog_id(CLR, LVL, SYSLVL, id, fmt, ...) \ + do { \ + if (log_syslog) { \ + syslog(SYSLVL, "[" HASH_FMT64 "] " fmt, \ + HASH_VAL64(id), ## __VA_ARGS__); \ + } else { \ + printf(CLR "==%05d== " OUROBOROS_PREFIX \ + "(" LVL "): ", getpid()); \ + printf("[" HASH_FMT64 "] " fmt, \ + HASH_VAL64(id), ## __VA_ARGS__); \ + printf(CLR_RESET "\n"); \ + fflush(stdout); \ + } \ } while (0) -#define log_err(...) __olog(CLR_RED, ERROR_CODE, LOG_ERR, __VA_ARGS__) -#define log_warn(...) __olog(CLR_YELLOW, WARN_CODE, LOG_WARNING, __VA_ARGS__) -#define log_info(...) __olog(CLR_GREEN, INFO_CODE, LOG_INFO, __VA_ARGS__) +#define log_err(...) \ + __olog(CLR_RED, ERROR_CODE, LOG_ERR, __VA_ARGS__) +#define log_warn(...) \ + __olog(CLR_YELLOW, WARN_CODE, LOG_WARNING, __VA_ARGS__) +#define log_info(...) \ + __olog(CLR_GREEN, INFO_CODE, LOG_INFO, __VA_ARGS__) + + +#define log_err_id(id, fmt, ...) \ + __olog_id(CLR_RED, ERROR_CODE, LOG_ERR, id, fmt, ## __VA_ARGS__) +#define log_warn_id(id, fmt, ...) \ + __olog_id(CLR_YELLOW, WARN_CODE, LOG_WARNING, id, fmt, ## __VA_ARGS__) +#define log_info_id(id, fmt, ...) \ + __olog_id(CLR_GREEN, INFO_CODE, LOG_INFO, id, fmt, ## __VA_ARGS__) #ifdef CONFIG_OUROBOROS_DEBUG #define log_dbg(...) __olog("", DEBUG_CODE, LOG_DEBUG, __VA_ARGS__) +#define log_dbg_id(id, fmt, ...) \ + __olog_id("", DEBUG_CODE, LOG_DEBUG, id, fmt, ## __VA_ARGS__) #else #define log_dbg(...) do { } while (0) +#define log_dbg_id(...) do { } while (0) #endif #endif /* OUROBOROS_LOGS_H */ |