Concord - C Discord API library
A Discord API wrapper library written in C
logconf.h
Go to the documentation of this file.
1#ifndef LOGCONF_H
2#define LOGCONF_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif /* __cplusplus */
7
8#include <stdint.h> /* uint64_t */
9
10#include "log.h"
11
12#define __ERR(fmt, ...) log_fatal(fmt "%s", __VA_ARGS__)
13
19#define ERR(...) \
20 do { \
21 __ERR(__VA_ARGS__, ""); \
22 abort(); \
23 } while (0)
24
31#define ASSERT_S(expr, msg) \
32 do { \
33 if (!(expr)) { \
34 ERR(ANSICOLOR( \
35 "\n\tAssert Failed", \
36 ANSI_FG_RED) ":\t%s\n\t" ANSICOLOR("Expected", \
37 ANSI_FG_RED) ":" \
38 "\t" msg, \
39 #expr); \
40 } \
41 } while (0)
42
52#define VASSERT_S(expr, fmt, ...) \
53 do { \
54 if (!(expr)) { \
55 ERR(ANSICOLOR("\n\tAssert Failed", \
56 ANSI_FG_RED) ":\t" fmt \
57 "\n\t" ANSICOLOR( \
58 "Expected", ANSI_FG_RED) ":\t %s", \
59 __VA_ARGS__, #expr); \
60 } \
61 } while (0)
62
63/* Encode a string with ANSI color */
64#ifdef LOG_USE_COLOR
65#define ANSICOLOR(str, color) "\x1b[" color "m" str "\x1b[0m"
66#else
67#define ANSICOLOR(str, color) str
68#endif
69
70#define ANSI_FG_BLACK "30"
71#define ANSI_FG_RED "31"
72#define ANSI_FG_GREEN "32"
73#define ANSI_FG_YELLOW "33"
74#define ANSI_FG_BLUE "34"
75#define ANSI_FG_MAGENTA "35"
76#define ANSI_FG_CYAN "36"
77#define ANSI_FG_WHITE "37"
78#define ANSI_FG_GRAY "90"
79#define ANSI_FG_BRIGHT_RED "91"
80#define ANSI_FG_BRIGHT_GREEN "92"
81#define ANSI_FG_BRIGHT_YELLOW "93"
82#define ANSI_FG_BRIGHT_BLUE "94"
83#define ANSI_FG_BRIGHT_MAGENTA "95"
84#define ANSI_FG_BRIGHT_CYAN "96"
85#define ANSI_FG_BRIGHT_WHITE "97"
86
87#define ANSI_BG_BLACK "40"
88#define ANSI_BG_RED "41"
89#define ANSI_BG_GREEN "42"
90#define ANSI_BG_YELLOW "43"
91#define ANSI_BG_BLUE "44"
92#define ANSI_BG_MAGENTA "45"
93#define ANSI_BG_CYAN "46"
94#define ANSI_BG_WHITE "47"
95#define ANSI_BG_GRAY "100"
96#define ANSI_BG_BRIGHT_RED "101"
97#define ANSI_BG_BRIGHT_GREEN "102"
98#define ANSI_BG_BRIGHT_YELLOW "103"
99#define ANSI_BG_BRIGHT_BLUE "104"
100#define ANSI_BG_BRIGHT_MAGENTA "105"
101#define ANSI_BG_BRIGHT_CYAN "106"
102#define ANSI_BG_BRIGHT_WHITE "107"
103
126#define logconf_trace(conf, ...) \
127 logconf_log(conf, LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
134#define logconf_debug(conf, ...) \
135 logconf_log(conf, LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
142#define logconf_info(conf, ...) \
143 logconf_log(conf, LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
150#define logconf_warn(conf, ...) \
151 logconf_log(conf, LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
158#define logconf_error(conf, ...) \
159 logconf_log(conf, LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
166#define logconf_fatal(conf, ...) \
167 logconf_log(conf, LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
168
169/* helper function for logconf_log() */
170#define __logconf_log(conf, level, file, line, fmt, ...) \
171 _log_log((conf)->L, level, file, line, "[%s] " fmt "%s", (conf)->id, \
172 __VA_ARGS__)
182#define logconf_log(conf, level, file, line, ...) \
183 if (!(conf)->is_disabled) \
184 __logconf_log(conf, level, file, line, __VA_ARGS__, "")
185
187#define LOGCONF_ID_LEN 64 + 1
188
195 const char *start;
197 size_t size;
198};
199
203 char *start;
205 size_t size;
206};
207
216struct logconf {
220 unsigned pid;
227
232
233 struct {
235 char *fname;
237 FILE *f;
239
241 struct {
242 size_t size;
243 char **ids;
245};
246
248struct loginfo {
250 size_t counter;
252 uint64_t tstamp_ms;
253};
254
264void logconf_setup(struct logconf *conf, const char id[], FILE *fp);
265
278void logconf_branch(struct logconf *branch,
279 struct logconf *orig,
280 const char id[]);
281
287void logconf_cleanup(struct logconf *conf);
288
299 char *const path[],
300 unsigned depth);
301
315void logconf_http(struct logconf *conf,
316 struct loginfo *info,
317 char url[],
318 struct logconf_szbuf header,
319 struct logconf_szbuf body,
320 char label_fmt[],
321 ...);
322
333void logconf_set_lock(struct logconf *conf, log_LockFn fn, void *udata);
334
343void logconf_set_level(struct logconf *conf, int level);
344
354void logconf_set_quiet(struct logconf *conf, bool enable);
355
370 log_LogFn fn,
371 void *udata,
372 int level);
373
384int logconf_add_fp(struct logconf *conf, FILE *fp, int level);
385
386#ifdef __cplusplus
387}
388#endif /* __cplusplus */
389
390#endif /* LOGCONF_H */
void(* log_LogFn)(log_Event *ev)
Definition: log.h:29
void(* log_LockFn)(bool lock, void *udata)
Definition: log.h:30
void logconf_cleanup(struct logconf *conf)
Cleanup a struct logconf module.
void logconf_setup(struct logconf *conf, const char id[], FILE *fp)
Initialize a struct logconf module from a config file.
int logconf_add_fp(struct logconf *conf, FILE *fp, int level)
File where the log will be written.
void logconf_branch(struct logconf *branch, struct logconf *orig, const char id[])
Branch and link a struct logconf module to a existing one.
void logconf_set_quiet(struct logconf *conf, bool enable)
Toggle quiet mode.
void logconf_set_level(struct logconf *conf, int level)
Set the current logging level.
#define LOGCONF_ID_LEN
Definition: logconf.h:187
void logconf_add_callback(struct logconf *conf, log_LogFn fn, void *udata, int level)
Callback functions called when logging data.
void logconf_http(struct logconf *conf, struct loginfo *info, char url[], struct logconf_szbuf header, struct logconf_szbuf body, char label_fmt[],...)
Log HTTP transfers.
void logconf_set_lock(struct logconf *conf, log_LockFn fn, void *udata)
If the log will be written to from multiple threads a lock function can be set.
struct logconf_field logconf_get_field(struct logconf *conf, char *const path[], unsigned depth)
Get the value from a given JSON field of the config file.
Definition: log.h:40
The read-only config.json field.
Definition: logconf.h:193
const char * start
Definition: logconf.h:195
size_t size
Definition: logconf.h:197
Generic sized-buffer.
Definition: logconf.h:201
char * start
Definition: logconf.h:203
size_t size
Definition: logconf.h:205
A stackful and modularized wrapper over the popular 'log.c' facilities.
Definition: logconf.h:216
char * fname
Definition: logconf.h:235
FILE * f
Definition: logconf.h:237
int * counter
Definition: logconf.h:229
log_Logger * L
Definition: logconf.h:231
char ** ids
Definition: logconf.h:243
size_t size
Definition: logconf.h:242
_Bool is_branch
Definition: logconf.h:222
_Bool is_disabled
Definition: logconf.h:224
struct logconf_szbuf file
Definition: logconf.h:226
struct logconf::@13 * logger
struct logconf::@14 disable_modules
unsigned pid
Definition: logconf.h:220
struct logconf::@13 * http
Store logging information from log_http()
Definition: logconf.h:248
size_t counter
Definition: logconf.h:250
uint64_t tstamp_ms
Definition: logconf.h:252