/* vi: set ts=2: +-------------------+ Christian Schlittchen | | Enno Rehling | Eressea PBEM host | Katja Zedel | (c) 1998 - 2001 | Henning Peters | | Ingo Wilken +-------------------+ Stefan Reich This program may not be used, modified or distributed without prior permission by the authors of Eressea. */ #include #include "log.h" #include #include #include #include /* TODO: set from external function */ static int flags = LOG_FLUSH|LOG_CPERROR|LOG_CPWARNING; static FILE * logfile; void log_puts(const char * str) { if (!logfile) logfile = stderr; fputs(str, logfile); } void log_printf(const char * format, ...) { va_list marker; if (!logfile) logfile = stderr; va_start(marker, format); vfprintf(logfile, format, marker); va_end(marker); } void log_open(const char * filename) { if (logfile) log_close(); logfile = fopen(filename, "a"); if (logfile) { /* Get UNIX-style time and display as number and string. */ time_t ltime; time( <ime ); log_printf( "===\n=== Logfile started at %s===\n", ctime( <ime ) ); } } void log_close(void) { if (!logfile || logfile == stderr || logfile == stdout) return; if (logfile) { /* Get UNIX-style time and display as number and string. */ time_t ltime; time( <ime ); log_printf("===\n=== Logfile closed at %s===\n\n", ctime( <ime ) ); } fclose(logfile); logfile = 0; } void _log_warn(const char * format, ...) { va_list marker; if (!logfile) logfile = stderr; fputs("WARNING: ", logfile); va_start(marker, format); vfprintf(logfile, format, marker); va_end(marker); if (logfile!=stderr) { if (flags & LOG_CPWARNING) { fputs("\bWARNING: ", stderr); va_start(marker, format); vfprintf(stderr, format, marker); va_end(marker); } if (flags & LOG_FLUSH) { fflush(logfile); } } } void _log_error(const char * format, ...) { va_list marker; if (!logfile) logfile = stderr; fputs("ERROR: ", logfile); va_start(marker, format); vfprintf(logfile, format, marker); va_end(marker); if (logfile!=stderr) { if (flags & LOG_CPERROR) { fputs("\bERROR: ", stderr); va_start(marker, format); vfprintf(stderr, format, marker); va_end(marker); } if (flags & LOG_FLUSH) { fflush(logfile); } } } void _log_info(unsigned int flag, const char * format, ...) { va_list marker; if (!logfile) logfile = stderr; fprintf(logfile, "INFO[%u]: ", flag); va_start(marker, format); vfprintf(logfile, format, marker); va_end(marker); if (logfile!=stderr) { if (flags & flag) { fprintf(stderr, "\bINFO[%u]: ", flag); va_start(marker, format); vfprintf(stderr, format, marker); va_end(marker); } if (flags & LOG_FLUSH) { fflush(logfile); } } }