Concord - C Discord API library
A Discord API wrapper library written in C
logconf.h File Reference
#include <stdint.h>
#include "attributes.h"
#include "log.h"
Include dependency graph for logconf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  logconf_field
 The read-only config.json field. More...
 
struct  logconf_szbuf
 Generic sized-buffer. More...
 
struct  logconf
 A stackful and modularized wrapper over the popular 'log.c' facilities. More...
 
struct  loginfo
 Store logging information from log_http() More...
 

Macros

#define __ERR(fmt, ...)   log_fatal(fmt "%s", __VA_ARGS__)
 
#define ERR(...)
 Print error message and abort. More...
 
#define ASSERT_S(expr, msg)
 Assert that allows printing a error message. More...
 
#define VASSERT_S(expr, fmt, ...)
 Assert that allows printing a error message in a printf-like fashion. More...
 
#define ANSICOLOR(str, color)   str
 
#define ANSI_FG_BLACK   "30"
 
#define ANSI_FG_RED   "31"
 
#define ANSI_FG_GREEN   "32"
 
#define ANSI_FG_YELLOW   "33"
 
#define ANSI_FG_BLUE   "34"
 
#define ANSI_FG_MAGENTA   "35"
 
#define ANSI_FG_CYAN   "36"
 
#define ANSI_FG_WHITE   "37"
 
#define ANSI_FG_GRAY   "90"
 
#define ANSI_FG_BRIGHT_RED   "91"
 
#define ANSI_FG_BRIGHT_GREEN   "92"
 
#define ANSI_FG_BRIGHT_YELLOW   "93"
 
#define ANSI_FG_BRIGHT_BLUE   "94"
 
#define ANSI_FG_BRIGHT_MAGENTA   "95"
 
#define ANSI_FG_BRIGHT_CYAN   "96"
 
#define ANSI_FG_BRIGHT_WHITE   "97"
 
#define ANSI_BG_BLACK   "40"
 
#define ANSI_BG_RED   "41"
 
#define ANSI_BG_GREEN   "42"
 
#define ANSI_BG_YELLOW   "43"
 
#define ANSI_BG_BLUE   "44"
 
#define ANSI_BG_MAGENTA   "45"
 
#define ANSI_BG_CYAN   "46"
 
#define ANSI_BG_WHITE   "47"
 
#define ANSI_BG_GRAY   "100"
 
#define ANSI_BG_BRIGHT_RED   "101"
 
#define ANSI_BG_BRIGHT_GREEN   "102"
 
#define ANSI_BG_BRIGHT_YELLOW   "103"
 
#define ANSI_BG_BRIGHT_BLUE   "104"
 
#define ANSI_BG_BRIGHT_MAGENTA   "105"
 
#define ANSI_BG_BRIGHT_CYAN   "106"
 
#define ANSI_BG_BRIGHT_WHITE   "107"
 
#define logconf_trace(conf, ...)    logconf_log(conf, LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
 Log level trace. More...
 
#define logconf_debug(conf, ...)    logconf_log(conf, LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
 Log level debug. More...
 
#define logconf_info(conf, ...)    logconf_log(conf, LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
 Log level info. More...
 
#define logconf_warn(conf, ...)    logconf_log(conf, LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
 Log level warn. More...
 
#define logconf_error(conf, ...)    logconf_log(conf, LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
 Log level error. More...
 
#define logconf_fatal(conf, ...)    logconf_log(conf, LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
 Log level fatal. More...
 
#define __logconf_log(conf, level, file, line, fmt, ...)
 
#define logconf_log(conf, level, file, line, ...)
 Run-time configurable log level. More...
 
#define LOGCONF_ID_LEN   64 + 1
 

Functions

void logconf_setup (struct logconf *conf, const char id[], FILE *fp)
 Initialize a struct logconf module from a config file. More...
 
void logconf_branch (struct logconf *branch, struct logconf *orig, const char id[])
 Branch and link a struct logconf module to a existing one. More...
 
void logconf_cleanup (struct logconf *conf)
 Cleanup a struct logconf module. More...
 
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. More...
 
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. More...
 
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. More...
 
void logconf_set_level (struct logconf *conf, int level)
 Set the current logging level. More...
 
void logconf_set_quiet (struct logconf *conf, bool enable)
 Toggle quiet mode. More...
 
void logconf_add_callback (struct logconf *conf, log_LogFn fn, void *udata, int level)
 Callback functions called when logging data. More...
 
int logconf_add_fp (struct logconf *conf, FILE *fp, int level)
 File where the log will be written. More...
 

Macro Definition Documentation

◆ __ERR

#define __ERR (   fmt,
  ... 
)    log_fatal(fmt "%s", __VA_ARGS__)

◆ ERR

#define ERR (   ...)
Value:
do { \
__ERR(__VA_ARGS__, ""); \
abort(); \
} while (0)

Print error message and abort.

Parameters
...printf-like format and variadic arguments (if any)

◆ ASSERT_S

#define ASSERT_S (   expr,
  msg 
)
Value:
do { \
if (!(expr)) { \
ERR(ANSICOLOR( \
"\n\tAssert Failed", \
ANSI_FG_RED) ":\t%s\n\t" ANSICOLOR("Expected", \
ANSI_FG_RED) ":" \
"\t" msg, \
#expr); \
} \
} while (0)
#define ANSI_FG_RED
Definition: logconf.h:72
#define ANSICOLOR(str, color)
Definition: logconf.h:68

Assert that allows printing a error message.

Parameters
exprconditional expression that's expected to be true
msgerror message

◆ VASSERT_S

#define VASSERT_S (   expr,
  fmt,
  ... 
)
Value:
do { \
if (!(expr)) { \
ERR(ANSICOLOR("\n\tAssert Failed", \
ANSI_FG_RED) ":\t" fmt \
"\n\t" ANSICOLOR( \
"Expected", ANSI_FG_RED) ":\t %s", \
__VA_ARGS__, #expr); \
} \
} while (0)

Assert that allows printing a error message in a printf-like fashion.

Warning
if no variadic arguments are specified there will be errors, in that case use ASSERT_S.
Parameters
exprconditional expression that's expected to be true
fmtprintf-like formatting string for the error message
...printf-like variadic arguments to be matched to fmt

◆ ANSICOLOR

#define ANSICOLOR (   str,
  color 
)    str

◆ ANSI_FG_BLACK

#define ANSI_FG_BLACK   "30"

◆ ANSI_FG_RED

#define ANSI_FG_RED   "31"

◆ ANSI_FG_GREEN

#define ANSI_FG_GREEN   "32"

◆ ANSI_FG_YELLOW

#define ANSI_FG_YELLOW   "33"

◆ ANSI_FG_BLUE

#define ANSI_FG_BLUE   "34"

◆ ANSI_FG_MAGENTA

#define ANSI_FG_MAGENTA   "35"

◆ ANSI_FG_CYAN

#define ANSI_FG_CYAN   "36"

◆ ANSI_FG_WHITE

#define ANSI_FG_WHITE   "37"

◆ ANSI_FG_GRAY

#define ANSI_FG_GRAY   "90"

◆ ANSI_FG_BRIGHT_RED

#define ANSI_FG_BRIGHT_RED   "91"

◆ ANSI_FG_BRIGHT_GREEN

#define ANSI_FG_BRIGHT_GREEN   "92"

◆ ANSI_FG_BRIGHT_YELLOW

#define ANSI_FG_BRIGHT_YELLOW   "93"

◆ ANSI_FG_BRIGHT_BLUE

#define ANSI_FG_BRIGHT_BLUE   "94"

◆ ANSI_FG_BRIGHT_MAGENTA

#define ANSI_FG_BRIGHT_MAGENTA   "95"

◆ ANSI_FG_BRIGHT_CYAN

#define ANSI_FG_BRIGHT_CYAN   "96"

◆ ANSI_FG_BRIGHT_WHITE

#define ANSI_FG_BRIGHT_WHITE   "97"

◆ ANSI_BG_BLACK

#define ANSI_BG_BLACK   "40"

◆ ANSI_BG_RED

#define ANSI_BG_RED   "41"

◆ ANSI_BG_GREEN

#define ANSI_BG_GREEN   "42"

◆ ANSI_BG_YELLOW

#define ANSI_BG_YELLOW   "43"

◆ ANSI_BG_BLUE

#define ANSI_BG_BLUE   "44"

◆ ANSI_BG_MAGENTA

#define ANSI_BG_MAGENTA   "45"

◆ ANSI_BG_CYAN

#define ANSI_BG_CYAN   "46"

◆ ANSI_BG_WHITE

#define ANSI_BG_WHITE   "47"

◆ ANSI_BG_GRAY

#define ANSI_BG_GRAY   "100"

◆ ANSI_BG_BRIGHT_RED

#define ANSI_BG_BRIGHT_RED   "101"

◆ ANSI_BG_BRIGHT_GREEN

#define ANSI_BG_BRIGHT_GREEN   "102"

◆ ANSI_BG_BRIGHT_YELLOW

#define ANSI_BG_BRIGHT_YELLOW   "103"

◆ ANSI_BG_BRIGHT_BLUE

#define ANSI_BG_BRIGHT_BLUE   "104"

◆ ANSI_BG_BRIGHT_MAGENTA

#define ANSI_BG_BRIGHT_MAGENTA   "105"

◆ ANSI_BG_BRIGHT_CYAN

#define ANSI_BG_BRIGHT_CYAN   "106"

◆ ANSI_BG_BRIGHT_WHITE

#define ANSI_BG_BRIGHT_WHITE   "107"

◆ logconf_trace

#define logconf_trace (   conf,
  ... 
)     logconf_log(conf, LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)

Log level trace.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ logconf_debug

#define logconf_debug (   conf,
  ... 
)     logconf_log(conf, LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)

Log level debug.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ logconf_info

#define logconf_info (   conf,
  ... 
)     logconf_log(conf, LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)

Log level info.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ logconf_warn

#define logconf_warn (   conf,
  ... 
)     logconf_log(conf, LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)

Log level warn.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ logconf_error

#define logconf_error (   conf,
  ... 
)     logconf_log(conf, LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)

Log level error.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ logconf_fatal

#define logconf_fatal (   conf,
  ... 
)     logconf_log(conf, LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)

Log level fatal.

Parameters
confthe struct logconf module
...the printf-like format string and successive arguments

◆ __logconf_log

#define __logconf_log (   conf,
  level,
  file,
  line,
  fmt,
  ... 
)
Value:
_log_log((conf)->L, level, file, line, "[%s] " fmt "%s", (conf)->id, \
__VA_ARGS__)
log_Logger L
void _log_log(log_Logger *L, int level, const char *file, int line, const char *fmt,...)

◆ logconf_log

#define logconf_log (   conf,
  level,
  file,
  line,
  ... 
)
Value:
if (!(conf)->is_disabled) \
__logconf_log(conf, level, file, line, __VA_ARGS__, "")

Run-time configurable log level.

Parameters
confthe struct logconf module
levelthe log level enumerator from log.c
filethe origin file name
linethe origin file line
...the printf-like format string and successive arguments

◆ LOGCONF_ID_LEN

#define LOGCONF_ID_LEN   64 + 1

Maximum length for module id

Function Documentation

◆ logconf_setup()

void logconf_setup ( struct logconf conf,
const char  id[],
FILE *  fp 
)

Initialize a struct logconf module from a config file.

Parameters
confpointer to the struct logconf structure to be initialized
idthe struct logconf module id
fpthe configuration file pointer that will fill struct logconf fields
See also
logconf_get_field() for fetching config file field's value

◆ logconf_branch()

void logconf_branch ( struct logconf branch,
struct logconf orig,
const char  id[] 
)

Branch and link a struct logconf module to a existing one.

Initialize a branch logging module thats expected to share common resources with its parent module orig. The common resources include: config file directives, logging output and disabled modules list.

Parameters
branchpointer to the struct logconf structure to be initialized as orig branch
origpointer to the struct logconf structure that holds the parent module
idthe branch module id

◆ logconf_cleanup()

void logconf_cleanup ( struct logconf conf)

Cleanup a struct logconf module.

Parameters
confthe struct logconf structure to be cleaned

◆ logconf_get_field()

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.

Parameters
confthe struct logconf module
paththe JSON key path
depththe path depth
Returns
a read-only sized buffer containing the field's contents
See also
logconf_setup() for initializing conf with a config file

◆ logconf_http()

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.

Parameters
confthe struct logconf module
inforetrieve information on this logging
urlthe transfer URL
headerthe transfer header
bodythe transfer body
label_fmta printf() like formatting string to provide additional logging description, such as the transfer's HTTP method, or HTTP response code.
...subsequent arguments that are converted for output

◆ logconf_set_lock()

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.

The function is passed the boolean true if the lock should be acquired or false if the lock should be released and the given udata value.

Parameters
confthe struct logconf module
fnlock callback
udatauser arbitrary data

◆ logconf_set_level()

void logconf_set_level ( struct logconf conf,
int  level 
)

Set the current logging level.

All logs below the given level will not be written to stderr. By default the level is LOG_TRACE, such that nothing is ignored.

Parameters
confthe struct logconf module
levellogging level

◆ logconf_set_quiet()

void logconf_set_quiet ( struct logconf conf,
bool  enable 
)

Toggle quiet mode.

Quiet-mode can be enabled by settings enable to true. While this mode is enabled the library will not output anything to stderr, but will continue to write to files and callbacks if any are set.

Parameters
confthe struct logconf module
enabletrue enables quiet-mode

◆ logconf_add_callback()

void logconf_add_callback ( struct logconf conf,
log_LogFn  fn,
void *  udata,
int  level 
)

Callback functions called when logging data.

One or more callback functions which are called with the log data can be provided to the library. A callback function is passed a log_Event structure containing the line number, filename, fmt string, va printf va_list, level and the given udata.

Parameters
confthe struct logconf module
fnthe callback function
udatauser arbitrary data
levellogging level to trigger callback

◆ logconf_add_fp()

int logconf_add_fp ( struct logconf conf,
FILE *  fp,
int  level 
)

File where the log will be written.

One or more file pointers where the log will be written can be provided to the library. Any messages below the given level are ignored. If the library failed to add a file pointer a value less-than-zero is returned.

Parameters
confthe struct logconf module
fpthe write-to file pointer
levellogging level condition for writing to fp