mirror of
https://github.com/falk-werner/webfused
synced 2026-03-02 04:09:19 +00:00
added logging support
This commit is contained in:
58
src/webfused/log/log.h
Normal file
58
src/webfused/log/log.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef WFD_LOG_H
|
||||
#define WFD_LOG_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef WFD_LOGLEVEL
|
||||
#define WFD_LOGLEVEL WFD_LOGLEVEL_ALL
|
||||
#endif
|
||||
|
||||
#define WFD_LOGLEVEL_FATAL 1
|
||||
#define WFD_LOGLEVEL_ERROR 3
|
||||
#define WFD_LOGLEVEL_WARN 4
|
||||
#define WFD_LOGLEVEL_INFO 5
|
||||
#define WFD_LOGLEVEL_DEBUG 7
|
||||
#define WFD_LOGLEVEL_ALL 8
|
||||
|
||||
#define WFD_FATAL(...) \
|
||||
WFD_LOG(WFD_LOGLEVEL_FATAL, __VA_ARGS__)
|
||||
|
||||
#define WFD_ERROR(...) \
|
||||
WFD_LOG(WFD_LOGLEVEL_ERROR, __VA_ARGS__)
|
||||
|
||||
#define WFD_WARN(...) \
|
||||
WFD_LOG(WFD_LOGLEVEL_WARN, __VA_ARGS__)
|
||||
|
||||
#define WFD_INFO(...) \
|
||||
WFD_LOG(WFD_LOGLEVEL_INFO, __VA_ARGS__)
|
||||
|
||||
#define WFD_DEBUG(...) \
|
||||
WFD_LOG(WFD_LOGLEVEL_DEBUG, __VA_ARGS__)
|
||||
|
||||
#define WFD_LOG(level, ...) \
|
||||
do { \
|
||||
if (WFD_LOGLEVEL >= (level)) { \
|
||||
wfd_log((level), __VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
extern void
|
||||
wfd_log(
|
||||
int level,
|
||||
char const * format,
|
||||
...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
86
src/webfused/log/logger.c
Normal file
86
src/webfused/log/logger.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "webfused/log/logger.h"
|
||||
#include "webfused/log/log.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
struct wfd_logger
|
||||
{
|
||||
int level;
|
||||
wfd_logger_log_fn * log;
|
||||
wfd_logger_onclose_fn * onclose;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
static void wfd_logger_default_log(
|
||||
void * user_data,
|
||||
int level,
|
||||
char const * format,
|
||||
va_list args);
|
||||
|
||||
static struct wfd_logger g_wfd_logger =
|
||||
{
|
||||
.level = WFD_LOGLEVEL_ALL,
|
||||
.log = &wfd_logger_default_log,
|
||||
.onclose = NULL,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
void
|
||||
wfd_logger_init(
|
||||
int level,
|
||||
wfd_logger_log_fn * log,
|
||||
wfd_logger_onclose_fn * onclose,
|
||||
void * user_data)
|
||||
{
|
||||
wfd_logger_close();
|
||||
|
||||
g_wfd_logger.level = level;
|
||||
g_wfd_logger.log = log;
|
||||
g_wfd_logger.onclose = onclose;
|
||||
g_wfd_logger.user_data = user_data;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_logger_close(void)
|
||||
{
|
||||
if (NULL != g_wfd_logger.onclose)
|
||||
{
|
||||
g_wfd_logger.onclose(g_wfd_logger.user_data);
|
||||
}
|
||||
|
||||
g_wfd_logger.level = WFD_LOGLEVEL_ALL;
|
||||
g_wfd_logger.log = &wfd_logger_default_log;
|
||||
g_wfd_logger.onclose = NULL;
|
||||
g_wfd_logger.user_data = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_log(
|
||||
int level,
|
||||
char const * format,
|
||||
...)
|
||||
{
|
||||
if (g_wfd_logger.level >= level)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
g_wfd_logger.log(
|
||||
g_wfd_logger.user_data,
|
||||
level,
|
||||
format,
|
||||
args);
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
static void wfd_logger_default_log(
|
||||
void * user_data,
|
||||
int level,
|
||||
char const * format,
|
||||
va_list args)
|
||||
{
|
||||
(void) user_data;
|
||||
(void) level;
|
||||
(void) format;
|
||||
(void) args;
|
||||
}
|
||||
40
src/webfused/log/logger.h
Normal file
40
src/webfused/log/logger.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WFD_LOGGER_H
|
||||
#define WFD_LOGGER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <cstdarg>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void
|
||||
wfd_logger_log_fn(
|
||||
void * user_data,
|
||||
int level,
|
||||
char const * format,
|
||||
va_list args);
|
||||
|
||||
typedef void
|
||||
wfd_logger_onclose_fn(
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wfd_logger_init(
|
||||
int level,
|
||||
wfd_logger_log_fn * log,
|
||||
wfd_logger_onclose_fn * onclose,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wfd_logger_close(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user