EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
log.h
Go to the documentation of this file.
1 
14 #ifndef LOG_H
15 #define LOG_H
16 
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <inttypes.h>
21 #include <string.h>
22 
23 #define LOG_VERSION "0.1.0"
24 #define __FILENAME__ strrchr("/" __FILE__, '/') + 1
25 #define MAX_LOG_LEVELS 5
26 
27 typedef void (*log_lock_fn)(bool lock);
28 
30 
31 #define LEVEL_NAMES \
32  { "TRACE", "DEBUG", "INFO", "WARN", "ERROR" }
33 #define LEVEL_COLORS \
34  { "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m" }
35 
36 #if defined __has_attribute
37 #if __has_attribute(format)
45 #define PRINTF_FORMAT(a, b) __attribute__((format(printf, (a), (b))))
46 #else
47 #define PRINTF_FORMAT(a, b)
48 #endif /* __has_attribute(format) */
49 #else
50 #define PRINTF_FORMAT(a, b)
51 #endif /* defined __has_attribute */
52 
53 static inline int snprintf_error(size_t size, int res) {
54  return res < 0 || (unsigned int)res >= size;
55 }
56 
57 #define log_trace(...) \
58  log_levels(LOGC_TRACE, __FILENAME__, __LINE__, __VA_ARGS__)
59 #define log_debug(...) \
60  log_levels(LOGC_DEBUG, __FILENAME__, __LINE__, __VA_ARGS__)
61 #define log_info(...) log_levels(LOGC_INFO, __FILENAME__, __LINE__, __VA_ARGS__)
62 #define log_warn(...) log_levels(LOGC_WARN, __FILENAME__, __LINE__, __VA_ARGS__)
68 #define log_error(...) \
69  log_levels(LOGC_ERROR, __FILENAME__, __LINE__, __VA_ARGS__)
75 #define log_errno(...) \
76  log_errno_error(LOGC_ERROR, __FILENAME__, __LINE__, __VA_ARGS__)
77 
78 #define log_err_ex(...) \
79  log_error_exit(LOGC_ERROR, __FILENAME__, __LINE__, __VA_ARGS__)
80 #define log_err_exp(...) \
81  log_error_exit_proc(LOGC_ERROR, __FILENAME__, __LINE__, __VA_ARGS__)
82 
83 void log_set_udata(void *udata);
84 void log_set_lock(log_lock_fn fn);
85 void log_set_level(uint8_t level);
86 void log_set_quiet(bool enable);
87 void log_set_color(bool enable);
88 void log_set_meta(bool enable);
89 int log_open_file(char *path);
90 void log_close_file(void);
91 
92 PRINTF_FORMAT(4, 5)
93 void log_levels(uint8_t level, const char *file, uint32_t line,
94  const char *format, ...);
95 PRINTF_FORMAT(4, 5)
96 void log_errno_error(uint8_t level, const char *file, uint32_t line,
97  const char *format, ...);
98 PRINTF_FORMAT(4, 5)
99 void log_error_exit(uint8_t level, const char *file, uint32_t line,
100  const char *format, ...);
101 PRINTF_FORMAT(4, 5)
102 void log_error_exit_proc(uint8_t level, const char *file, uint32_t line,
103  const char *format, ...);
104 
128 size_t printf_hex(char *buf, size_t buf_size, const uint8_t *data, size_t len,
129  bool uppercase);
130 #endif
log_lock_fn lock
Definition: log.c:56
uint8_t level
Definition: log.c:57
@ LOGC_WARN
Definition: log.h:29
@ LOGC_DEBUG
Definition: log.h:29
@ LOGC_ERROR
Definition: log.h:29
@ LOGC_TRACE
Definition: log.h:29
@ LOGC_INFO
Definition: log.h:29
void(* log_lock_fn)(bool lock)
Definition: log.h:27
void log_set_level(uint8_t level)
Definition: log.c:139
void log_set_color(bool enable)
Definition: log.c:145
size_t printf_hex(char *buf, size_t buf_size, const uint8_t *data, size_t len, bool uppercase)
Prints the data in data to buf as hex.
Definition: log.c:324
#define PRINTF_FORMAT(a, b)
Definition: log.h:50
void log_set_meta(bool enable)
Definition: log.c:143
void log_close_file(void)
Definition: log.c:167
void log_errno_error(uint8_t level, const char *file, uint32_t line, const char *format,...)
Definition: log.c:268
void log_error_exit(uint8_t level, const char *file, uint32_t line, const char *format,...)
Definition: log.c:282
void log_set_udata(void *udata)
void log_set_lock(log_lock_fn fn)
Definition: log.c:137
void log_set_quiet(bool enable)
Definition: log.c:141
int log_open_file(char *path)
Definition: log.c:147
void log_error_exit_proc(uint8_t level, const char *file, uint32_t line, const char *format,...)
Definition: log.c:310
void log_levels(uint8_t level, const char *file, uint32_t line, const char *format,...)
Definition: log.c:259