Sort out the logging so that -v etc do the right thing

This commit is contained in:
Daniel Silverstone 2017-09-08 21:15:41 +01:00
parent 8b88e44090
commit bb056e55b1
6 changed files with 96 additions and 16 deletions

View File

@ -575,6 +575,13 @@ CXXFLAGS += -DNETSURF_HOMEPAGE=\"$(NETSURF_HOMEPAGE)\"
CFLAGS += -DNETSURF_LOG_LEVEL=$(NETSURF_LOG_LEVEL) CFLAGS += -DNETSURF_LOG_LEVEL=$(NETSURF_LOG_LEVEL)
CXXFLAGS += -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 # General make rules
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------

View File

@ -79,6 +79,11 @@ NETSURF_USE_NSLOG := AUTO
# The minimum logging level *compiled* into netsurf # The minimum logging level *compiled* into netsurf
# Valid options are: DEEPDEBUG, DEBUG, VERBOSE, INFO, WARNING, ERROR, CRITICAL # Valid options are: DEEPDEBUG, DEBUG, VERBOSE, INFO, WARNING, ERROR, CRITICAL
NETSURF_LOG_LEVEL := INFO 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 # Enable stripping the NetSurf binary
# Valid options: YES, NO # Valid options: YES, NO

View File

@ -289,3 +289,8 @@ NSOPTION_COLOUR(sys_colour_ThreeDShadow, 0x00d5d5d5)
NSOPTION_COLOUR(sys_colour_Window, 0x00f1f1f1) NSOPTION_COLOUR(sys_colour_Window, 0x00f1f1f1)
NSOPTION_COLOUR(sys_colour_WindowFrame, 0x004e4e4e) NSOPTION_COLOUR(sys_colour_WindowFrame, 0x004e4e4e)
NSOPTION_COLOUR(sys_colour_WindowText, 0x00000000) 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")

View File

@ -20,6 +20,7 @@
#include <stdio.h> #include <stdio.h>
#include "utils/config.h" #include "utils/config.h"
#include "utils/nsoption.h"
#include "utils/sys_time.h" #include "utils/sys_time.h"
#include "utils/utsname.h" #include "utils/utsname.h"
#include "desktop/version.h" #include "desktop/version.h"
@ -105,21 +106,43 @@ netsurf_render_log(void *_ctx,
const char *fmt, const char *fmt,
va_list args) va_list args)
{ {
if (verbose_log) { fprintf(logfile,
fprintf(logfile, "%s %.*s:%i %.*s: ",
"%s %.*s:%i %.*s: ", nslog_gettime(),
nslog_gettime(), ctx->filenamelen,
ctx->filenamelen, ctx->filename,
ctx->filename, ctx->lineno,
ctx->lineno, ctx->funcnamelen,
ctx->funcnamelen, ctx->funcname);
ctx->funcname);
vfprintf(logfile, fmt, args); vfprintf(logfile, fmt, args);
/* Log entries aren't newline terminated add one for clarity */ /* Log entries aren't newline terminated add one for clarity */
fputc('\n', logfile); 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 #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 #endif
nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) 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 */ /* ensure we actually show logging */
verbose_log = true; verbose_log = true;
} }
} else if (verbose_log == true) { } else {
/* default is logging to stderr */ /* default is logging to stderr */
logfile = stderr; logfile = stderr;
} }
@ -211,10 +243,14 @@ nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv)
#ifdef WITH_NSLOG #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; ret = NSERROR_INIT_FAILED;
verbose_log = false; verbose_log = false;
} else if (nslog_uncork() != NSLOG_NO_ERROR) { } else if (nslog_uncork() != NSLOG_NO_ERROR) {
ret = NSERROR_INIT_FAILED; ret = NSERROR_INIT_FAILED;
verbose_log = false; verbose_log = false;
@ -241,3 +277,13 @@ nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv)
return ret; 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));
}

View File

@ -47,6 +47,18 @@ typedef bool(nslog_ensure_t)(FILE *fptr);
*/ */
extern nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv); 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 */ /* ensure a logging level is defined */
#ifndef NETSURF_LOG_LEVEL #ifndef NETSURF_LOG_LEVEL
#define NETSURF_LOG_LEVEL INFO #define NETSURF_LOG_LEVEL INFO

View File

@ -187,7 +187,7 @@ static void nsoption_validate(struct nsoption_s *opts, struct nsoption_s *defs)
break; break;
} }
} }
if (black == true) { if (black == true && defs != NULL) {
for (cloop = NSOPTION_SYS_COLOUR_START; for (cloop = NSOPTION_SYS_COLOUR_START;
cloop <= NSOPTION_SYS_COLOUR_END; cloop <= NSOPTION_SYS_COLOUR_END;
cloop++) { 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) > 60) &&
(opts[NSOPTION_max_retried_fetches].value.u > 1)) (opts[NSOPTION_max_retried_fetches].value.u > 1))
opts[NSOPTION_max_retried_fetches].value.u--; 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); *pargc -= (idx - 1);
nsoption_validate(opts, nsoptions_default);
return NSERROR_OK; return NSERROR_OK;
} }