/* context.c -- Handles eurephia contexts used by admin interfaces * * GPLv2 - Copyright (C) 2008 David Sommerseth * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #include #include #include #include #include #include #include #include #include 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 ) { fprintf(stderr, "ERROR: No database driver given in config file (database_driver)\n"); return NULL; } ctx = (eurephiaCTX *) malloc(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"); 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 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; } if( !eDBlink_init(ctx, dbdriver, 2) ) { eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver"); free_nullsafe(ctx); return NULL; } return ctx; } void eurephiaCTX_destroy(eurephiaCTX *ctx) { if( ctx == NULL ) { return; } if( (ctx->dbc != NULL) && (ctx->dbc->dbhandle != NULL) ) { eDBdisconnect(ctx); } if( ctx->eurephia_driver != NULL ) { 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; } free_nullsafe(ctx); }