diff --git a/Makefile b/Makefile index 146b2f7cb..d6723e046 100644 --- a/Makefile +++ b/Makefile @@ -575,6 +575,13 @@ CXXFLAGS += -DNETSURF_HOMEPAGE=\"$(NETSURF_HOMEPAGE)\" CFLAGS += -DNETSURF_LOG_LEVEL=$(NETSURF_LOG_LEVEL) CXXFLAGS += -DNETSURF_LOG_LEVEL=$(NETSURF_LOG_LEVEL) +# and the logging filter +CFLAGS += -DNETSURF_BUILTIN_LOG_FILTER=\"$(NETSURF_BUILTIN_LOG_FILTER)\" +CXXFLAGS += -DNETSURF_BUILTIN_LOG_FILTER=\"$(NETSURF_BUILTIN_LOG_FILTER)\" +# and the verbose logging filter +CFLAGS += -DNETSURF_BUILTIN_VERBOSE_FILTER=\"$(NETSURF_BUILTIN_VERBOSE_FILTER)\" +CXXFLAGS += -DNETSURF_BUILTIN_VERBOSE_FILTER=\"$(NETSURF_BUILTIN_VERBOSE_FILTER)\" + # ---------------------------------------------------------------------------- # General make rules # ---------------------------------------------------------------------------- diff --git a/Makefile.defaults b/Makefile.defaults index d6637da9e..1f9ce5fdf 100644 --- a/Makefile.defaults +++ b/Makefile.defaults @@ -79,6 +79,11 @@ NETSURF_USE_NSLOG := AUTO # The minimum logging level *compiled* into netsurf # Valid options are: DEEPDEBUG, DEBUG, VERBOSE, INFO, WARNING, ERROR, CRITICAL NETSURF_LOG_LEVEL := INFO +# The log filter set during log initialisation before options are available +NETSURF_BUILTIN_LOG_FILTER := level:WARNING +# The log filter set during log initialisation before options are available +# if the logging level is set to verbose +NETSURF_BUILTIN_VERBOSE_FILTER := level:VERBOSE # Enable stripping the NetSurf binary # Valid options: YES, NO diff --git a/desktop/options.h b/desktop/options.h index d91898c6e..9b7064efa 100644 --- a/desktop/options.h +++ b/desktop/options.h @@ -289,3 +289,8 @@ NSOPTION_COLOUR(sys_colour_ThreeDShadow, 0x00d5d5d5) NSOPTION_COLOUR(sys_colour_Window, 0x00f1f1f1) NSOPTION_COLOUR(sys_colour_WindowFrame, 0x004e4e4e) NSOPTION_COLOUR(sys_colour_WindowText, 0x00000000) + +/** Filter for non-verbose logging */ +NSOPTION_STRING(log_filter, "level:WARNING") +/** Filter for verbose logging */ +NSOPTION_STRING(verbose_filter, "level:VERBOSE") diff --git a/utils/log.c b/utils/log.c index 5ebe51f99..68a470aa9 100644 --- a/utils/log.c +++ b/utils/log.c @@ -20,6 +20,7 @@ #include #include "utils/config.h" +#include "utils/nsoption.h" #include "utils/sys_time.h" #include "utils/utsname.h" #include "desktop/version.h" @@ -105,21 +106,43 @@ netsurf_render_log(void *_ctx, const char *fmt, va_list args) { - if (verbose_log) { - fprintf(logfile, - "%s %.*s:%i %.*s: ", - nslog_gettime(), - ctx->filenamelen, - ctx->filename, - ctx->lineno, - ctx->funcnamelen, - ctx->funcname); + fprintf(logfile, + "%s %.*s:%i %.*s: ", + nslog_gettime(), + ctx->filenamelen, + ctx->filename, + ctx->lineno, + ctx->funcnamelen, + ctx->funcname); - vfprintf(logfile, fmt, args); + vfprintf(logfile, fmt, args); - /* Log entries aren't newline terminated add one for clarity */ - fputc('\n', logfile); + /* Log entries aren't newline terminated add one for clarity */ + fputc('\n', logfile); +} + +/* exported interface documented in utils/log.h */ +nserror +nslog_set_filter(const char *filter) +{ + nslog_error err; + nslog_filter_t *filt = NULL; + + err = nslog_filter_from_text(filter, &filt); + if (err != NSLOG_NO_ERROR) { + if (err == NSLOG_NO_MEMORY) + return NSERROR_NOMEM; + else + return NSERROR_INVALID; } + + err = nslog_filter_set_active(filt, NULL); + if (err != NSLOG_NO_ERROR) { + nslog_filter_unref(filt); + return NSERROR_NOSPACE; + } + + return NSERROR_OK; } #else @@ -147,6 +170,15 @@ nslog_log(const char *file, const char *func, int ln, const char *format, ...) } } +/* exported interface documented in utils/log.h */ +nserror +nslog_set_filter(const char *filter) +{ + (void)(filter); + return NSERROR_OK; +} + + #endif nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) @@ -195,7 +227,7 @@ nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) /* ensure we actually show logging */ verbose_log = true; } - } else if (verbose_log == true) { + } else { /* default is logging to stderr */ logfile = stderr; } @@ -211,10 +243,14 @@ nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) #ifdef WITH_NSLOG - if (nslog_set_render_callback(netsurf_render_log, NULL) != NSLOG_NO_ERROR) { + if (nslog_set_filter(verbose_log ? + NETSURF_BUILTIN_VERBOSE_FILTER : + NETSURF_BUILTIN_LOG_FILTER) != NSERROR_OK) { + ret = NSERROR_INIT_FAILED; + verbose_log = false; + } else if (nslog_set_render_callback(netsurf_render_log, NULL) != NSLOG_NO_ERROR) { ret = NSERROR_INIT_FAILED; verbose_log = false; - } else if (nslog_uncork() != NSLOG_NO_ERROR) { ret = NSERROR_INIT_FAILED; verbose_log = false; @@ -241,3 +277,13 @@ nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) return ret; } + +/* exported interface documented in utils/log.h */ +nserror +nslog_set_filter_by_options() +{ + if (verbose_log) + return nslog_set_filter(nsoption_charp(verbose_filter)); + else + return nslog_set_filter(nsoption_charp(log_filter)); +} diff --git a/utils/log.h b/utils/log.h index 7b5bcc3e8..e73c8c3ee 100644 --- a/utils/log.h +++ b/utils/log.h @@ -47,6 +47,18 @@ typedef bool(nslog_ensure_t)(FILE *fptr); */ extern nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv); +/** + * Set the logging filter. + * + * Compiles and enables the given logging filter. + */ +extern nserror nslog_set_filter(const char *filter); + +/** + * Set the logging filter according to the options. + */ +extern nserror nslog_set_filter_by_options(void); + /* ensure a logging level is defined */ #ifndef NETSURF_LOG_LEVEL #define NETSURF_LOG_LEVEL INFO diff --git a/utils/nsoption.c b/utils/nsoption.c index 15f89ca5d..09529a0d0 100644 --- a/utils/nsoption.c +++ b/utils/nsoption.c @@ -187,7 +187,7 @@ static void nsoption_validate(struct nsoption_s *opts, struct nsoption_s *defs) break; } } - if (black == true) { + if (black == true && defs != NULL) { for (cloop = NSOPTION_SYS_COLOUR_START; cloop <= NSOPTION_SYS_COLOUR_END; cloop++) { @@ -209,6 +209,9 @@ static void nsoption_validate(struct nsoption_s *opts, struct nsoption_s *defs) opts[NSOPTION_max_retried_fetches].value.u) > 60) && (opts[NSOPTION_max_retried_fetches].value.u > 1)) opts[NSOPTION_max_retried_fetches].value.u--; + + /* We ignore the result because we can't fail to validate. Yay */ + (void)nslog_set_filter_by_options(); } /** @@ -820,6 +823,8 @@ nsoption_commandline(int *pargc, char **argv, struct nsoption_s *opts) } *pargc -= (idx - 1); + nsoption_validate(opts, nsoptions_default); + return NSERROR_OK; }