/* eurephia_log.c -- eurephia logging * * 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 "eurephia_log.h" const char *erp_logtypes[] = { "\0", "-- INFO -- \0", "-- DEBUG -- \0", "** WARNING ** \0", "** ERROR ** \0", "** CRITICAL ** \0", "** - FATAL - ** \0", "** * PANIC * ** \0" }; // POSIX Mutex to avoild simultaneously logging activity from // several threads at the same time pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; // Simple log function ... Write log data to the context log file void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ) { // Only log if we have an open log file and which has high enough log level if( (ctx != NULL) && (ctx->log != NULL) && (ctx->loglevel >= loglvl) ) { va_list ap; char tstmp_str[200]; time_t tstmp; struct tm *loctstmp; // Get timestamp memset(&tstmp_str, 0, 200); tstmp = time(NULL); loctstmp = localtime(&tstmp); if( loctstmp != NULL ) { if( strftime(tstmp_str, 198, "%Y-%m-%d %H:%M:%S %Z", loctstmp) == 0 ) { snprintf(tstmp_str, 198, "(error getting timestamp string)"); } } else { snprintf(tstmp_str, 198, "(error getting timestamp)"); } va_start(ap, fmt); pthread_mutex_lock(&log_mutex); // Block other threads from writing when we write fprintf(ctx->log, "[%s] %s [%i] ", tstmp_str, erp_logtypes[logdst], loglvl); vfprintf(ctx->log, fmt, ap); fprintf(ctx->log, "\n"); fflush(ctx->log); pthread_mutex_unlock(&log_mutex); // Unblock other threads va_end(ap); } }