From 74ffc40e9238c9897ae47b8118f642565e9654a0 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 2 Apr 2011 13:05:55 +0000 Subject: move logging initialisation svn path=/trunk/netsurf/; revision=12152 --- desktop/gui.h | 5 ++++- desktop/netsurf.c | 19 ++++++------------- utils/errors.h | 5 ++++- utils/log.c | 29 +++++++++++++++++++++++++++++ utils/log.h | 23 +++++++++++++++++++++++ 5 files changed, 66 insertions(+), 15 deletions(-) diff --git a/desktop/gui.h b/desktop/gui.h index 141e82af5..596587631 100644 --- a/desktop/gui.h +++ b/desktop/gui.h @@ -67,7 +67,10 @@ typedef enum { GUI_POINTER_DEFAULT, GUI_POINTER_POINT, GUI_POINTER_CARET, #include "desktop/search.h" #include "utils/errors.h" -void gui_stdout(void); +/** \todo remove these when each frontend calls nslog_init */ +#include +bool nslog_ensure(FILE *fptr); + void gui_multitask(void); void gui_poll(bool active); void gui_quit(void); diff --git a/desktop/netsurf.c b/desktop/netsurf.c index 0d6f5f809..3707b1c72 100644 --- a/desktop/netsurf.c +++ b/desktop/netsurf.c @@ -108,21 +108,14 @@ nserror netsurf_init(int *pargc, signal(SIGPIPE, SIG_IGN); #endif - if (((*pargc) > 1) && - ((*pargv)[1][0] == '-') && - ((*pargv)[1][1] == 'v') && - ((*pargv)[1][2] == 0)) { - int argcmv; - verbose_log = true; - for (argcmv = 2; argcmv < (*pargc); argcmv++) { - (*pargv)[argcmv - 1] = (*pargv)[argcmv]; - } - (*pargc)--; - #ifndef HAVE_STDOUT - gui_stdout(); + ret = nslog_init(nslog_ensure, pargc, *pargv); +#else + ret = nslog_init(NULL, pargc, *pargv); #endif - } + + if (ret != NSERROR_OK) + return ret; #ifdef _MEMDEBUG_H_ memdebug_memdebug("memdump"); diff --git a/utils/errors.h b/utils/errors.h index 1a634f615..8bca2819a 100644 --- a/utils/errors.h +++ b/utils/errors.h @@ -37,7 +37,10 @@ typedef enum { NSERROR_SAVE_FAILED, /**< Failed to save data */ - NSERROR_CLONE_FAILED /**< Failed to clone handle */ + NSERROR_CLONE_FAILED, /**< Failed to clone handle */ + + NSERROR_INIT_FAILED /**< Initialisation failed */ + } nserror; #endif diff --git a/utils/log.c b/utils/log.c index 47fdfaad0..31ca89d13 100644 --- a/utils/log.c +++ b/utils/log.c @@ -31,6 +31,35 @@ static struct timeval start_tv; static char buff[32]; +nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv) +{ + nserror ret = NSERROR_OK; + + if (((*pargc) > 1) && + (argv[1][0] == '-') && + (argv[1][1] == 'v') && + (argv[1][2] == 0)) { + int argcmv; + for (argcmv = 2; argcmv < (*pargc); argcmv++) { + argv[argcmv - 1] = argv[argcmv]; + } + (*pargc)--; + + /* ensure we actually show logging */ + verbose_log = true; + + /* ensure stderr is available */ + if (ensure != NULL) { + if (ensure(stderr) == false) { + /* failed to ensure output */ + ret = NSERROR_INIT_FAILED; + } + } + } + return ret; +} + + const char *nslog_gettime(void) { struct timeval tv; diff --git a/utils/log.h b/utils/log.h index b59b6d3f8..84a211d95 100644 --- a/utils/log.h +++ b/utils/log.h @@ -22,11 +22,34 @@ #include #include "desktop/netsurf.h" +#include "utils/errors.h" #ifdef NDEBUG # define LOG(x) ((void) 0) #else + +/** + * Ensures the FILE handle is available to write logging to. + * + * This is provided by the frontends if required + */ +typedef bool(nslog_ensure_t)(FILE *fptr); + +/** + * Initialise the logging system. + * + * Sets up everything required for logging. Processes the argv passed + * to remove the -v switch for verbose logging. If necessary ensures + * the output file handle is available. + */ +extern nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv); + +/** + * Obtain a formatted string suitable for prepending to a log message + * + * \return formatted string of the time since first log call + */ extern const char *nslog_gettime(void); extern void nslog_log(const char *format, ...); -- cgit v1.2.3