From 7ae9a74c9c3bdab619ac5c0cefe1c8269bb06603 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Thu, 24 Sep 2009 00:16:53 +0200 Subject: Rewritten the eurephia_log() to support syslog logging as well Also simplified the initialisation of the logging module. By calling the eurephia_log_init(eurephiaCTX *, char *dest, int loglevel) function, a log context will be setup inside the eurephiaCTX. To close the log file, eurephia_log_close(eurephiaCTX *) must be called. The destination string to eurephia_log_init() can be: - stdout: Log everything to stdout - stderr: Log everything to stderr - none: Do no logging at all - syslog: Log via syslog. can be: user, local[0-7], daemon or authpriv. - Filename All logging goes to the given filename. If the filename string is not recognised by any of the reserved words above, it will be handled as a filename. --- eurephiadm/client_context.c | 49 +++++++++------------------------------------ eurephiadm/client_context.h | 2 +- eurephiadm/eurephiadm.c | 8 ++------ 3 files changed, 13 insertions(+), 46 deletions(-) (limited to 'eurephiadm') diff --git a/eurephiadm/client_context.c b/eurephiadm/client_context.c index aaec995..1a8fc0b 100644 --- a/eurephiadm/client_context.c +++ b/eurephiadm/client_context.c @@ -43,13 +43,13 @@ * Initialises a new eurephiaCTX. This function also initialises the database driver, which must * be configured in the configuration. * - * @param log FILE pointer where to put log data + * @param log String containing log destination * @param loglevel Set the log level (verbosity) * @param cfg eurephiaVALUES pointer to the configuration * * @return Returns a pointer to a eurephiaCTX, otherwise NULL. */ -eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, eurephiaVALUES *cfg) { +eurephiaCTX *eurephiaCTX_init(const char *log, const int loglevel, eurephiaVALUES *cfg) { eurephiaCTX *ctx = NULL; char *dbdriver = NULL, *logfile = NULL; int cfgloglvl = 0; @@ -62,39 +62,21 @@ eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, eurephiaVALUES *cfg ctx = (eurephiaCTX *) malloc_nullsafe(NULL, sizeof(eurephiaCTX)+2); assert(ctx != NULL); - memset(ctx, 0, sizeof(eurephiaCTX)+2); ctx->context_type = ECTX_ADMIN_CONSOLE; // Open log file. Use config file as default if it exists, if not use input param. - logfile = eGet_value(cfg, "log"); + cfgloglvl = ((eGet_value(cfg, "log_level") == NULL) + ? loglevel : atoi_nullsafe(eGet_value(cfg, "log_level"))); + + logfile = eGet_value(cfg, "log"); if( (logfile != NULL) && (log == NULL) ) { - if( strcmp(logfile, "stdout:") == 0 ) { - ctx->log = stdout; - } else if( strcmp(logfile, "stderr:") == 0 ) { - ctx->log = stderr; - } else if( strcmp(logfile, "none:") == 0 ) { - ctx->log = NULL; - } else if( (ctx->log = fopen(logfile, "aw")) == NULL ) { - fprintf(stderr, "ERROR: Could not open log file: %s\n", logfile); - free_nullsafe(NULL, ctx); - return NULL; - } + eurephia_log_init(ctx, logfile, (loglevel > 0 ? loglevel : cfgloglvl)); } else { // If log file is not set in config, use input log parameter. But if // no log file is defined even here, use stderr. If no logging is wanted, it // must be defined as none: in the config file. - ctx->log = (log != NULL ? log : stderr); - } - - // Set log level. Use config file as default if it exists, if not input param defaults. - // But if input param loglevel > 0, then override config file. - // Only set loglevel if logging is enabled. - if( ctx->log != NULL ) { - cfgloglvl = ((eGet_value(cfg, "log_level") == NULL) - ? loglevel : atoi_nullsafe(eGet_value(cfg, "log_level"))); - ctx->loglevel = (loglevel > 0 ? loglevel : cfgloglvl); - } else { - ctx->loglevel = 0; + eurephia_log_init(ctx, (log != NULL ? log : "stderr:"), + (loglevel > 0 ? loglevel : cfgloglvl)); } if( !eDBlink_init(ctx, dbdriver, 2) ) { @@ -125,17 +107,6 @@ void eurephiaCTX_destroy(eurephiaCTX *ctx) { eDBlink_close(ctx); } - if( ctx->log != NULL ) { - fflush(ctx->log); - - // Do not close log file if we're on stdout or stderr - if( (ctx->log != stderr) && (ctx->log != stdout) ) { - eurephia_log(ctx, LOG_INFO, 2, "Closing log file"); - fclose(ctx->log); - } - - ctx->log = NULL; - ctx->loglevel = 0; - } + eurephia_log_close(ctx); free_nullsafe(ctx, ctx); } diff --git a/eurephiadm/client_context.h b/eurephiadm/client_context.h index 56e85ed..ef6f5a4 100644 --- a/eurephiadm/client_context.h +++ b/eurephiadm/client_context.h @@ -31,7 +31,7 @@ #ifndef EUREPHIA_CLIENT_CONTEXT #define EUREPHIA_CLIENT_CONTEXT -eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, eurephiaVALUES *cfg); +eurephiaCTX *eurephiaCTX_init(const char *log, const int loglevel, eurephiaVALUES *cfg); void eurephiaCTX_destroy(eurephiaCTX *ctx); #endif diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c index 518f8a5..4209e6a 100644 --- a/eurephiadm/eurephiadm.c +++ b/eurephiadm/eurephiadm.c @@ -327,7 +327,7 @@ char *args2string(int argc, char **argv) { * @return returns 0 on success, otherwise a value > 0 */ int main(int argc, char **argv) { - FILE *logfile = NULL; + char *logfile = NULL; int loglevel = 0; eurephiaCTX *ctx = NULL; eurephiaSESSION *session = NULL; @@ -367,11 +367,7 @@ int main(int argc, char **argv) { return 0; case 'l': - if( (logfile = fopen(optargs[0], "wb")) == NULL ) { - fprintf(stderr, "%s: ERROR :: Could not open log file: %s\n", - basename(argv[0]), optargs[0]); - return 0; - } + logfile = optargs[0]; break; case 'L': -- cgit