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 "attributes.h"
11#include "log.h"
12
13#define __ERR(fmt, ...) log_fatal(fmt "%s", __VA_ARGS__)
14
20#define ERR(...) \
21 do { \
22 __ERR(__VA_ARGS__, ""); \
23 abort(); \
24 } while (0)
25
32#define ASSERT_S(expr, msg) \
33 do { \
34 if (!(expr)) { \
35 ERR(ANSICOLOR( \
36 "\n\tAssert Failed", \
37 ANSI_FG_RED) ":\t%s\n\t" ANSICOLOR("Expected", \
38 ANSI_FG_RED) ":" \
39 "\t" msg, \
40 #expr); \
41 } \
42 } while (0)
43
53#define VASSERT_S(expr, fmt, ...) \
54 do { \
55 if (!(expr)) { \
56 ERR(ANSICOLOR("\n\tAssert Failed", \
57 ANSI_FG_RED) ":\t" fmt \
58 "\n\t" ANSICOLOR( \
59 "Expected", ANSI_FG_RED) ":\t %s", \
60 __VA_ARGS__, #expr); \
61 } \
62 } while (0)
63
64/* Encode a string with ANSI color */
65#ifdef LOG_USE_COLOR
66#define ANSICOLOR(str, color) "\x1b[" color "m" str "\x1b[0m"
67#else
68#define ANSICOLOR(str, color) str
69#endif
70
71#define ANSI_FG_BLACK "30"
72#define ANSI_FG_RED "31"
73#define ANSI_FG_GREEN "32"
74#define ANSI_FG_YELLOW "33"
75#define ANSI_FG_BLUE "34"
76#define ANSI_FG_MAGENTA "35"
77#define ANSI_FG_CYAN "36"
78#define ANSI_FG_WHITE "37"
79#define ANSI_FG_GRAY "90"
80#define ANSI_FG_BRIGHT_RED "91"
81#define ANSI_FG_BRIGHT_GREEN "92"
82#define ANSI_FG_BRIGHT_YELLOW "93"
83#define ANSI_FG_BRIGHT_BLUE "94"
84#define ANSI_FG_BRIGHT_MAGENTA "95"
85#define ANSI_FG_BRIGHT_CYAN "96"
86#define ANSI_FG_BRIGHT_WHITE "97"
87
88#define ANSI_BG_BLACK "40"
89#define ANSI_BG_RED "41"
90#define ANSI_BG_GREEN "42"
91#define ANSI_BG_YELLOW "43"
92#define ANSI_BG_BLUE "44"
93#define ANSI_BG_MAGENTA "45"
94#define ANSI_BG_CYAN "46"
95#define ANSI_BG_WHITE "47"
96#define ANSI_BG_GRAY "100"
97#define ANSI_BG_BRIGHT_RED "101"
98#define ANSI_BG_BRIGHT_GREEN "102"
99#define ANSI_BG_BRIGHT_YELLOW "103"
100#define ANSI_BG_BRIGHT_BLUE "104"
101#define ANSI_BG_BRIGHT_MAGENTA "105"
102#define ANSI_BG_BRIGHT_CYAN "106"
103#define ANSI_BG_BRIGHT_WHITE "107"
104
127#define logconf_trace(conf, ...) \
128 logconf_log(conf, LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
135#define logconf_debug(conf, ...) \
136 logconf_log(conf, LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
143#define logconf_info(conf, ...) \
144 logconf_log(conf, LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
151#define logconf_warn(conf, ...) \
152 logconf_log(conf, LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
159#define logconf_error(conf, ...) \
160 logconf_log(conf, LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
167#define logconf_fatal(conf, ...) \
168 logconf_log(conf, LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
169
170/* helper function for logconf_log() */
171#define __logconf_log(conf, level, file, line, fmt, ...) \
172 _log_log((conf)->L, level, file, line, "[%s] " fmt "%s", (conf)->id, \
173 __VA_ARGS__)
183#define logconf_log(conf, level, file, line, ...) \
184 if (!(conf)->is_disabled) \
185 __logconf_log(conf, level, file, line, __VA_ARGS__, "")
186
188#define LOGCONF_ID_LEN 64 + 1
189
196 const char *start;
198 size_t size;
199};
200
204 char *start;
206 size_t size;
207};
208
217struct logconf {
221 unsigned pid;
228
233
234 struct {
236 char *fname;
238 FILE *f;
240
242 struct {
243 size_t size;
244 char **ids;
246};
247
249struct loginfo {
251 size_t counter;
253 uint64_t tstamp_ms;
254};
255
265void logconf_setup(struct logconf *conf, const char id[], FILE *fp);
266
279void logconf_branch(struct logconf *branch,
280 struct logconf *orig,
281 const char id[]);
282
288void logconf_cleanup(struct logconf *conf);
289
300 char *const path[],
301 unsigned depth);
302
316void logconf_http(struct logconf *conf,
317 struct loginfo *info,
318 char url[],
319 struct logconf_szbuf header,
320 struct logconf_szbuf body,
321 char label_fmt[],
322 ...) PRINTF_LIKE(6, 7);
323
334void logconf_set_lock(struct logconf *conf, log_LockFn fn, void *udata);
335
344void logconf_set_level(struct logconf *conf, int level);
345
355void logconf_set_quiet(struct logconf *conf, bool enable);
356
371 log_LogFn fn,
372 void *udata,
373 int level);
374
385int logconf_add_fp(struct logconf *conf, FILE *fp, int level);
386
387#ifdef __cplusplus
388}
389#endif /* __cplusplus */
390
391#endif /* LOGCONF_H */
#define PRINTF_LIKE(a, b)
Definition: attributes.h:4
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:188
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:194
const char * start
Definition: logconf.h:196
size_t size
Definition: logconf.h:198
Generic sized-buffer.
Definition: logconf.h:202
char * start
Definition: logconf.h:204
size_t size
Definition: logconf.h:206
A stackful and modularized wrapper over the popular 'log.c' facilities.
Definition: logconf.h:217
char * fname
Definition: logconf.h:236
FILE * f
Definition: logconf.h:238
int * counter
Definition: logconf.h:230
log_Logger * L
Definition: logconf.h:232
char ** ids
Definition: logconf.h:244
size_t size
Definition: logconf.h:243
_Bool is_branch
Definition: logconf.h:223
_Bool is_disabled
Definition: logconf.h:225
struct logconf_szbuf file
Definition: logconf.h:227
struct logconf::@13 * logger
struct logconf::@14 disable_modules
unsigned pid
Definition: logconf.h:221
struct logconf::@13 * http
Store logging information from log_http()
Definition: logconf.h:249
size_t counter
Definition: logconf.h:251
uint64_t tstamp_ms
Definition: logconf.h:253