Skip to content

Commit

Permalink
Set log level from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
wtoorop committed Aug 12, 2022
1 parent a1a6f6c commit d477475
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* Strengthen version checking for LibIDN2 during the cmake step so LibIDN2
version 2.3.3 can be properly found (thanks jpbion).
* Updated systemd/stubby.service file. Thanks Bruno Pagani
* log level configurable in config file with log_level setting
Command line setting (with -v or -l) overrules the config file setting

* 2021-06-04: Version 0.4.0
* We announce the intention to remove the dnsovertls*.sinodun.com
Expand Down
39 changes: 28 additions & 11 deletions src/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ static const char *default_config =
static getdns_dict *listen_dict = NULL;
static getdns_list *listen_list = NULL;

static getdns_return_t parse_config(getdns_context *context, const char *config_str, int yaml_config)
static getdns_return_t parse_config(getdns_context *context,
const char *config_str, int yaml_config, long *log_level)
{
getdns_dict *config_dict;
getdns_list *list;
getdns_return_t r;
uint32_t config_log_level;

if (yaml_config) {
r = getdns_yaml2dict(config_str, &config_dict);
Expand Down Expand Up @@ -202,6 +204,15 @@ static getdns_return_t parse_config(getdns_context *context, const char *config_
(void) getdns_dict_remove_name(
config_dict, "listen_addresses");
}
if (!(r = getdns_dict_get_int(
config_dict, "log_level", &config_log_level))) {
if (config_log_level > GETDNS_LOG_DEBUG)
stubby_error("log level '%d' from config file is invalid or out of range (0-7)", (int)config_log_level);
else
*log_level = config_log_level;
(void) getdns_dict_remove_name(
config_dict, "log_level");
}
if (!r && (r = getdns_context_config(context, config_dict))) {
stubby_error("Could not configure context with "
"config dict: %s", stubby_getdns_strerror(r));
Expand All @@ -210,7 +221,9 @@ static getdns_return_t parse_config(getdns_context *context, const char *config_
return r;
}

static getdns_return_t parse_config_file(getdns_context *context, const char *fn)
extern long log_level;
static getdns_return_t parse_config_file(getdns_context *context,
const char *fn, long *config_log_level)
{
FILE *fh;
char *config_file = NULL;
Expand Down Expand Up @@ -250,17 +263,20 @@ static getdns_return_t parse_config_file(getdns_context *context, const char *fn
fclose(fh);
r = parse_config(context, config_file,
strstr(fn, ".yml") != NULL ||
strstr(fn, ".yaml") != NULL);
strstr(fn, ".yaml") != NULL,
config_log_level);
free(config_file);
if (r == GETDNS_RETURN_GOOD)
if (r == GETDNS_RETURN_GOOD
&& ( log_level <= GETDNS_LOG_DEBUG
|| *config_log_level >= GETDNS_LOG_DEBUG))
stubby_log(NULL,GETDNS_LOG_UPSTREAM_STATS, GETDNS_LOG_DEBUG,
"Read config from file %s", fn);
return r;
}

void init_config(getdns_context *context)
void init_config(getdns_context *context, long *log_level)
{
(void) parse_config(context, default_config, 0);
(void) parse_config(context, default_config, 0, log_level);
}

void delete_config(void)
Expand All @@ -272,7 +288,8 @@ void delete_config(void)
listen_list = NULL;
}

int read_config(getdns_context *context, const char *custom_config_fn, int *validate_dnssec)
int read_config(getdns_context *context, const char *custom_config_fn,
int *validate_dnssec, long *log_level)
{
char *conf_fn;
int found_conf = 0;
Expand All @@ -281,9 +298,9 @@ int read_config(getdns_context *context, const char *custom_config_fn, int *vali
getdns_list *api_info_keys = NULL;
int dnssec_validation = 0;

(void) parse_config(context, default_config, 0);
(void) parse_config(context, default_config, 0, log_level);
if (custom_config_fn) {
if ((r = parse_config_file(context, custom_config_fn))) {
if ((r = parse_config_file(context, custom_config_fn, log_level))) {
stubby_error("Could not parse config file "
"\"%s\": %s", custom_config_fn,
stubby_getdns_strerror(r));
Expand All @@ -295,7 +312,7 @@ int read_config(getdns_context *context, const char *custom_config_fn, int *vali
stubby_error("Error getting user config file");
return 0;
}
r = parse_config_file(context, conf_fn);
r = parse_config_file(context, conf_fn, log_level);
if (r == GETDNS_RETURN_GOOD)
found_conf = 1;
else if (r != GETDNS_RETURN_IO_ERROR)
Expand All @@ -309,7 +326,7 @@ int read_config(getdns_context *context, const char *custom_config_fn, int *vali
stubby_error("Error getting system config file");
return 0;
}
r = parse_config_file(context, conf_fn);
r = parse_config_file(context, conf_fn, log_level);
if (r == GETDNS_RETURN_GOOD)
found_conf = 1;
else if (r != GETDNS_RETURN_IO_ERROR)
Expand Down
5 changes: 3 additions & 2 deletions src/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ char *system_config_file(void);
char *system_service_config_file(void);
#endif

void init_config(getdns_context *context);
void init_config(getdns_context *context, long *log_level);
void delete_config(void);

int read_config(getdns_context *context, const char *custom_config_fn, int *validate_dnssec);
int read_config(getdns_context *context, const char *custom_config_fn,
int *validate_dnssec, long *log_level);

char *config_get_api_info(getdns_context *context);

Expand Down
4 changes: 4 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static void default_stubby_verror(getdns_loglevel_type level, const char *fmt, v
(void) fputc('\n', stderr);
}

long log_level = GETDNS_LOG_DEBUG + 1;

static void default_stubby_vlog(void *userarg, uint64_t system,
getdns_loglevel_type level,
const char *fmt, va_list ap)
Expand All @@ -54,12 +56,14 @@ static void default_stubby_vlog(void *userarg, uint64_t system,
#if defined(STUBBY_ON_WINDOWS)
struct _timeb timeb;
time_t tsec;
if (level > log_level) return;

_ftime_s(&timeb);
tsec = (time_t)timeb.time;
tv.tv_usec = timeb.millitm * 1000;
gmtime_s(&tm, &tsec);
#else
if (level > log_level) return;
gettimeofday(&tv, NULL);
gmtime_r(&tv.tv_sec, &tm);
#endif
Expand Down
18 changes: 11 additions & 7 deletions src/stubby.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ print_version(FILE *out)
fprintf(out, STUBBY_PACKAGE_STRING "\n");
}

extern long log_level;
#define NO_LOGGING 999
int
main(int argc, char **argv)
{
Expand All @@ -114,7 +116,7 @@ main(int argc, char **argv)
getdns_context *context = NULL;
getdns_return_t r;
int opt;
long log_level = 7;
long config_log_level = NO_LOGGING;
char *ep;

while ((opt = getopt(argc, argv, "C:ighlv:w:V")) != -1) {
Expand Down Expand Up @@ -159,10 +161,6 @@ main(int argc, char **argv)
exit(EXIT_FAILURE);
}
}

stubby_log(NULL,GETDNS_LOG_UPSTREAM_STATS, GETDNS_LOG_INFO,
"Stubby version: %s", STUBBY_PACKAGE_STRING);

#if defined(ENABLE_WINDOWS_SERVICE)
if ( windows_service ) {
windows_service_command(windows_service_arg, log_connections ? log_level : 0, custom_config_fn);
Expand All @@ -178,9 +176,15 @@ main(int argc, char **argv)
if (log_connections)
stubby_set_getdns_logging(context, (int)log_level);

init_config(context);
if ( !read_config(context, custom_config_fn, &dnssec_validation) )
init_config(context, &config_log_level);
if ( !read_config(context, custom_config_fn, &dnssec_validation, &config_log_level) )
exit(EXIT_FAILURE);
if (config_log_level != NO_LOGGING && !log_connections) {
log_connections = 1;
log_level = config_log_level;
}
stubby_log(NULL,GETDNS_LOG_UPSTREAM_STATS, GETDNS_LOG_INFO,
"Stubby version: %s", STUBBY_PACKAGE_STRING);

if (print_api_info) {
char *api_information_str = config_get_api_info(context);
Expand Down
9 changes: 6 additions & 3 deletions stubby.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
# in future: https://github.com/getdnsapi/stubby/issues/79

################################### LOGGING ####################################
# Logging is currently configured at runtime using command line arguments. See
# > stubby -h
# for details.
# Define at which level messages will be logged to stdout. Can be one of:
# GETDNS_LOG_EMERG, GETDNS_LOG_ALERT, GETDNS_LOG_CRIT, GETDNS_LOG_ERR,
# GETDNS_LOG_WARNING, GETDNS_LOG_NOTICE, GETDNS_LOG_INFO or GETDNS_LOG_DEBUG
# where GETDNS_LOG_EMERG is the least and GETDNS_LOG_DEBUG the most verbose.
log_level: GETDNS_LOG_NOTICE


########################## BASIC & PRIVACY SETTINGS ############################
# Specifies whether to run as a recursive or stub resolver
Expand Down

0 comments on commit d477475

Please sign in to comment.