1
0
mirror of https://github.com/falk-werner/webfused synced 2026-03-02 04:09:19 +00:00

read log config from config file

This commit is contained in:
Falk Werner
2020-03-18 17:33:31 +01:00
parent 1625869696
commit adbfd45951
29 changed files with 772 additions and 72 deletions

View File

@@ -76,7 +76,7 @@ wfd_file_authenticator_create(
{
bool result = false;
char const * filename = wfd_settings_get(settings, "file");
char const * filename = wfd_settings_get_string(settings, "file");
if (NULL != filename)
{
struct wfd_file_authenticator * data = malloc(sizeof(struct wfd_file_authenticator));

View File

@@ -58,4 +58,15 @@ wfd_config_builder_add_filesystem(
return builder.vtable->add_filesystem(builder.data, name, mount_point);
}
bool
wfd_config_builder_set_logger(
struct wfd_config_builder builder,
char const * provider,
int level,
struct wfd_settings * settings)
{
return builder.vtable->set_logger(builder.data, provider, level, settings);
}

View File

@@ -49,6 +49,13 @@ wfd_config_builder_add_filesystem_fn(
char const * name,
char const * mount_point);
typedef bool
wfd_config_builder_set_logger_fn(
void * data,
char const * provider,
int level,
struct wfd_settings * settings);
struct wfd_config_builder_vtable
{
wfd_config_builder_set_server_vhostname_fn * set_server_vhostname;
@@ -58,6 +65,7 @@ struct wfd_config_builder_vtable
wfd_config_builder_set_server_document_root_fn * set_server_document_root;
wfd_config_builder_add_auth_provider_fn * add_auth_provider;
wfd_config_builder_add_filesystem_fn * add_filesystem;
wfd_config_builder_set_logger_fn * set_logger;
};
struct wfd_config_builder
@@ -103,6 +111,13 @@ wfd_config_builder_add_filesystem(
char const * name,
char const * mount_point);
extern bool
wfd_config_builder_set_logger(
struct wfd_config_builder builder,
char const * provider,
int level,
struct wfd_settings * settings);
#ifdef __cplusplus
}
#endif

View File

@@ -3,6 +3,7 @@
#include "webfused/auth/factory.h"
#include "webfused/auth/authenticator.h"
#include "webfused/mountpoint_factory.h"
#include "webfused/log/manager.h"
#include <stdlib.h>
@@ -100,6 +101,17 @@ wfd_config_add_filesystem(
config->mountpoint_factory, name, mount_point);
}
static bool
wfd_config_set_logger(
void * data,
char const * provider,
int level,
struct wfd_settings * settings)
{
struct wfd_config * config = data;
return wfd_log_manager_set_logger(provider, level, settings);
}
static const struct wfd_config_builder_vtable
wfd_config_vtable_config_builder =
{
@@ -109,7 +121,8 @@ wfd_config_vtable_config_builder =
.set_server_cert = &wfd_config_set_server_cert,
.set_server_document_root = &wfd_config_set_server_document_root,
.add_auth_provider = &wfd_config_add_auth_provider,
.add_filesystem = &wfd_config_add_filesystem
.add_filesystem = &wfd_config_add_filesystem,
.set_logger = &wfd_config_set_logger
};
struct wfd_config *

View File

@@ -55,6 +55,59 @@ wfd_config_check_version(
return true;
}
static bool
wfd_config_read_logger(
config_t * config,
struct wfd_config_builder builder)
{
bool result = true;
bool hasLogger = (NULL != config_lookup(config, "log"));
if (hasLogger)
{
char const * provider;
int rc = config_lookup_string(config, "log.provider", &provider);
if (CONFIG_TRUE != rc)
{
WFD_ERROR("missing log provider");
result = false;
}
char const * level_str;
if (result)
{
rc = config_lookup_string(config, "log.level", &level_str);
if (CONFIG_TRUE != rc)
{
WFD_ERROR("missing log level");
result = false;
}
}
int level;
if (result)
{
bool success = wfd_log_level_parse(level_str, &level);
if (!success)
{
WFD_ERROR("failed to parse log level: unknown valuie \'%s\'", level_str);
result = false;
}
}
if (result)
{
config_setting_t * setting = config_lookup(config, "log.settings");
struct wfd_settings settings;
wfd_settings_init(&settings, setting);
result = wfd_config_builder_set_logger(builder, provider, level, &settings);
wfd_settings_cleanup(&settings);
}
}
return result;
}
static bool
wfd_config_read_server(
config_t * config,
@@ -198,6 +251,7 @@ wfd_config_load(
{
bool result = wfd_config_check_version(config)
&& wfd_config_read_logger(config, builder)
&& wfd_config_read_server(config, builder)
&& wfd_config_read_authentication(config, builder)
&& wfd_config_read_filesystems(config, builder)

View File

@@ -20,13 +20,41 @@ wfd_settings_cleanup(
}
char const *
wfd_settings_get(
wfd_settings_get_string(
struct wfd_settings * settings,
char const * key)
{
if (NULL == settings) {return NULL; }
char const * result;
int rc = config_setting_lookup_string(settings->setting, key, &result);
return (CONFIG_TRUE == rc) ? result : NULL;
}
char const *
wfd_settings_get_string_or_default(
struct wfd_settings * settings,
char const * key,
char const * default_value)
{
if (NULL == settings) {return default_value; }
char const * result;
int rc = config_setting_lookup_string(settings->setting, key, &result);
return (CONFIG_TRUE == rc) ? result : default_value;
}
bool
wfd_settings_get_bool(
struct wfd_settings * settings,
char const * key)
{
if (NULL == settings) {return false; }
int result;
int rc = config_setting_lookup_bool(settings->setting, key, &result);
return ((CONFIG_TRUE == rc) && (CONFIG_TRUE == result));
}

View File

@@ -1,6 +1,10 @@
#ifndef WFD_CONFIG_SETTINGS_H
#define WFD_CONFIG_SETTINGS_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
@@ -10,7 +14,18 @@ struct wfd_settings;
extern char const *
wfd_settings_get(
wfd_settings_get_string(
struct wfd_settings * settings,
char const * key);
extern char const *
wfd_settings_get_string_or_default(
struct wfd_settings * settings,
char const * key,
char const * default_value);
extern bool
wfd_settings_get_bool(
struct wfd_settings * settings,
char const * key);

View File

@@ -98,7 +98,7 @@ static void on_interrupt(int signal_id)
int wfd_daemon_run(int argc, char * argv[])
{
wfd_stderr_logger_init(WFD_LOGLEVEL_ALL);
wfd_stderr_logger_init(WFD_LOGLEVEL_ALL, NULL);
struct args args;
int result = parse_arguments(argc, argv, &args);

62
src/webfused/log/log.c Normal file
View File

@@ -0,0 +1,62 @@
#include "webfused/log/log.h"
#include <strings.h>
char const *
wfd_log_level_tostring(int level)
{
switch (level)
{
case WFD_LOGLEVEL_NONE: return "none";
case WFD_LOGLEVEL_FATAL: return "fatal";
case WFD_LOGLEVEL_ERROR: return "error";
case WFD_LOGLEVEL_WARN: return "warn";
case WFD_LOGLEVEL_INFO: return "info";
case WFD_LOGLEVEL_DEBUG: return "debug";
case WFD_LOGLEVEL_ALL: return "all";
default: return "<unknown>";
}
}
bool
wfd_log_level_parse(
char const * level,
int * result)
{
bool success = true;
if (0 == strcasecmp("all", level))
{
*result = WFD_LOGLEVEL_ALL;
}
else if (0 == strcasecmp("none", level))
{
*result = WFD_LOGLEVEL_NONE;
}
else if (0 == strcasecmp("fatal", level))
{
*result = WFD_LOGLEVEL_FATAL;
}
else if (0 == strcasecmp("error", level))
{
*result = WFD_LOGLEVEL_ERROR;
}
else if ((0 == strcasecmp("warn", level)) ||
(0 == strcasecmp("warning", level)))
{
*result = WFD_LOGLEVEL_WARN;
}
else if (0 == strcasecmp("info", level))
{
*result = WFD_LOGLEVEL_INFO;
}
else if (0 == strcasecmp("debug", level))
{
*result = WFD_LOGLEVEL_DEBUG;
}
else
{
return false;
}
return success;
}

View File

@@ -2,6 +2,7 @@
#define WFD_LOG_H
#ifndef __cplusplus
#include <stdbool.h>
#include <stdarg.h>
#else
#include <cstdarg>
@@ -16,6 +17,7 @@ extern "C"
#define WFD_LOGLEVEL WFD_LOGLEVEL_ALL
#endif
#define WFD_LOGLEVEL_NONE -1
#define WFD_LOGLEVEL_FATAL 1
#define WFD_LOGLEVEL_ERROR 3
#define WFD_LOGLEVEL_WARN 4
@@ -51,6 +53,14 @@ wfd_log(
char const * format,
...);
extern char const *
wfd_log_level_tostring(int level);
extern bool
wfd_log_level_parse(
char const * level,
int * result);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,30 @@
#include "webfused/log/manager.h"
#include "webfused/log/log.h"
#include "webfused/log/stderr_logger.h"
#include "webfused/log/syslog_logger.h"
#include <string.h>
bool
wfd_log_manager_set_logger(
char const * provider,
int level,
struct wfd_settings * settings)
{
bool result = false;
if (0 == strcmp("syslog", provider))
{
result = wfd_syslog_logger_init(level, settings);
}
else if (0 == strcmp("stderr", provider))
{
result = wfd_stderr_logger_init(level, settings);
}
else
{
WFD_ERROR("failed to init log: unknown provider \'%s\'", provider);
}
return result;
}

View File

@@ -0,0 +1,25 @@
#ifndef WFD_LOG_MANAGER_H
#define WFD_LOG_MANAGER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfd_settings;
extern bool
wfd_log_manager_set_logger(
char const * provider,
int level,
struct wfd_settings * settings);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -5,36 +5,25 @@
#include <stdio.h>
#include <stdlib.h>
static const char *
wfd_stderr_logger_level_str(int level)
{
switch (level)
{
case WFD_LOGLEVEL_FATAL: return "fatal";
case WFD_LOGLEVEL_ERROR: return "error";
case WFD_LOGLEVEL_WARN: return "warn";
case WFD_LOGLEVEL_INFO: return "info";
case WFD_LOGLEVEL_DEBUG: return "debug";
default: return "notice";
}
}
void
static void
wfd_stderr_logger_log(
void * user_data,
int level,
char const * format,
va_list args)
{
fprintf(stderr, "%s: ", wfd_stderr_logger_level_str(level));
fprintf(stderr, "%s: ", wfd_log_level_tostring(level));
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
void
bool
wfd_stderr_logger_init(
int level)
int level,
struct wfd_settings * settings)
{
(void) settings;
wfd_logger_init(level, &wfd_stderr_logger_log, NULL, NULL);
return true;
}

View File

@@ -2,9 +2,7 @@
#define WFD_LOG_STDERR_LOGGER_H
#ifndef __cplusplus
#include <stdarg.h>
#else
#include <cstdarg>
#include <stdbool.h>
#endif
#ifdef __cplusplus
@@ -12,17 +10,12 @@ extern "C"
{
#endif
extern void
wfd_stderr_logger_log(
void * user_data,
int level,
char const * format,
va_list args);
struct wfd_settings;
extern void
extern bool
wfd_stderr_logger_init(
int level);
int level,
struct wfd_settings * settings);
#ifdef __cplusplus
}

View File

@@ -1,8 +1,11 @@
#include "webfused/log/syslog_logger.h"
#include "webfused/log/logger.h"
#include "webfused/log/log.h"
#include "webfused/config/settings.h"
#include <syslog.h>
#include <strings.h>
#include <stddef.h>
static int
@@ -40,17 +43,80 @@ wfd_syslog_logger_close(
closelog();
}
void
struct wfd_syslog_facility
{
char const * name;
int value;
};
static bool
wfd_syslog_logger_parse_facility(
char const * facility,
int * result)
{
static struct wfd_syslog_facility const facilities[] =
{
{"auth", LOG_AUTH},
{"authpriv", LOG_AUTHPRIV},
{"cron", LOG_CRON},
{"daemon", LOG_DAEMON},
{"fpt", LOG_FTP},
{"kern", LOG_KERN},
{"local0", LOG_LOCAL0},
{"local1", LOG_LOCAL1},
{"local2", LOG_LOCAL2},
{"local3", LOG_LOCAL3},
{"local4", LOG_LOCAL4},
{"local5", LOG_LOCAL5},
{"local6", LOG_LOCAL6},
{"local7", LOG_LOCAL7},
{"lpr", LOG_LPR},
{"mail", LOG_MAIL},
{"news", LOG_NEWS},
{"syslog", LOG_SYSLOG},
{"user", LOG_USER},
{"uucp", LOG_UUCP}
};
static size_t const facilites_count = sizeof(facilities) / sizeof(facilities[0]);
for (size_t i = 0; i < facilites_count; i++)
{
if (0 == strcasecmp(facility, facilities[i].name))
{
*result = facilities[i].value;
return true;
}
}
return false;
}
bool
wfd_syslog_logger_init(
int level,
char const * ident,
int options,
int facility)
struct wfd_settings * settings)
{
wfd_logger_init(level,
&wfd_syslog_logger_log,
&wfd_syslog_logger_close,
NULL);
char const * ident = wfd_settings_get_string_or_default(settings, "ident", "webfused");
char const * facility_str = wfd_settings_get_string_or_default(settings, "facility", "daemon");
bool log_pid = wfd_settings_get_bool(settings, "log_pid");
openlog(ident, options, facility);
int facility;
bool result = wfd_syslog_logger_parse_facility(facility_str, &facility);
if (result)
{
int options = (log_pid) ? LOG_PID : 0;
wfd_logger_init(level,
&wfd_syslog_logger_log,
&wfd_syslog_logger_close,
NULL);
openlog(ident, options, facility);
}
else
{
WFD_ERROR("failed to init syslog logger: invalid log facility: \'%s\'", facility_str);
}
return result;
}

View File

@@ -1,17 +1,21 @@
#ifndef WFD_LOG_SYSLOG_LOGGER_H
#define WFD_LOG_SYSLOG_LOGGER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern void
struct wfd_settings;
extern bool
wfd_syslog_logger_init(
int level,
char const * ident,
int options,
int facility);
struct wfd_settings * settings);
#ifdef __cplusplus
}