123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #pragma once
- #include <errno.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <syslog.h>
- #include <unistd.h>
- #define PLOG_MSG(...) printf(__VA_ARGS__)
- #define SYSLOG_MSG(...) syslog(LOG_INFO, __VA_ARGS__)
- #define LOG_MSG(...) \
- do { \
- if (get_use_syslog()) { \
- SYSLOG_MSG(__VA_ARGS__); \
- } else { \
- PLOG_MSG(__VA_ARGS__); \
- } \
- } while (0)
- #define PLOG_ERROR(...) fprintf(stderr, "ERROR: " __VA_ARGS__)
- #define SYSLOG_ERROR(...) syslog(LOG_ERR, __VA_ARGS__)
- #define LOG_ERROR(...) \
- do { \
- if (get_use_syslog()) { \
- SYSLOG_ERROR(__VA_ARGS__); \
- } else { \
- PLOG_ERROR(__VA_ARGS__); \
- } \
- } while (0)
- #define LOG_ONCE(type, ...) \
- do { \
- static int __once = 0; \
- if (!__once++) \
- LOG_##type(__VA_ARGS__); \
- } while (0)
- #define FATAL_ERRORNO(msg) \
- do { \
- LOG_ERROR(msg " (%s)\n", strerror(errno)); \
- exit(EXIT_FAILURE); \
- } while (0)
- #define FATAL_ERROR(...) \
- do { \
- LOG_ERROR(__VA_ARGS__); \
- exit(EXIT_FAILURE); \
- } while (0)
- #define HINT_ONCE(name, hint) \
- do { \
- static int __once = 0; \
- name = (!__once++ ? hint : ""); \
- } while (0)
- #define HINT_ONCE_ON(cond, ...) \
- do { \
- if (cond) \
- HINT_ONCE(__VA_ARGS__); \
- } while (0);
- #define LOG_HINTED(type, msg, hint, ...) \
- do { \
- const char *__arg; \
- HINT_ONCE(__arg, hint); \
- LOG_##type(msg "%s", __VA_ARGS__, __arg); \
- } while (0)
- void set_use_syslog(const char *name);
- bool get_use_syslog(void);
|