From cfecad2e0e5efc55394b282cbfbb5caa859f5d16 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Mon, 1 Dec 2008 23:10:24 +0100 Subject: Receive eurephiaVALUES struct with config instead of char *db interface. Get the database driver directly from config file. Implemented also log settings via config file as well, which can be overridden by future command line arguments. --- eurephiadm/client_context.c | 47 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'eurephiadm/client_context.c') diff --git a/eurephiadm/client_context.c b/eurephiadm/client_context.c index adcabcf..e849420 100644 --- a/eurephiadm/client_context.c +++ b/eurephiadm/client_context.c @@ -25,21 +25,60 @@ #include #include +#include +#include #include -eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi) { +eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, eurephiaVALUES *cfg) { eurephiaCTX *ctx = NULL; + char *dbdriver = NULL, *logfile = NULL; + int cfgloglvl = 0; + + dbdriver = eGet_value(cfg, "database_driver"); + if( dbdriver == NULL ) { + eurephia_log(ctx, LOG_PANIC, 0, "No database driver given in config file (database_driver)"); + return NULL; + } ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2); assert(ctx != NULL); memset(ctx, 0, sizeof(eurephiaCTX)+2); ctx->context_type = ECTX_ADMIN_CONSOLE; - ctx->log = log; - ctx->loglevel = loglevel; + // Open log file. Use config file as default if it exists, if not use input param. + 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(ctx); + return NULL; + } + } else if( logfile == NULL ) { + // If not log file is 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; + } - if( !eDBlink_init(ctx, dbi, 2) ){ + if( !eDBlink_init(ctx, dbdriver, 2) ) { eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver"); free_nullsafe(ctx); return NULL; -- cgit