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 --- utils/errors.h | 5 ++++- utils/log.c | 29 +++++++++++++++++++++++++++++ utils/log.h | 23 +++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) (limited to 'utils') 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