2015-07-06 19:53:09 +02:00
|
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
|
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
|
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
|
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
|
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
|
|
|
|
|
+-------------------+ Stefan Reich <reich@halbling.de>
|
|
|
|
|
|
2012-06-04 03:41:07 +02:00
|
|
|
|
This program may not be used, modified or distributed
|
2010-08-08 10:06:34 +02:00
|
|
|
|
without prior permission by the authors of Eressea.
|
|
|
|
|
*/
|
|
|
|
|
#include <platform.h>
|
|
|
|
|
#include "log.h"
|
2015-10-29 09:04:23 +01:00
|
|
|
|
#include "bsdstring.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include "unicode.h"
|
|
|
|
|
|
2012-06-04 03:41:07 +02:00
|
|
|
|
#include <assert.h>
|
2015-11-04 14:55:13 +01:00
|
|
|
|
#include <errno.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#ifdef STDIO_CP
|
|
|
|
|
static int stdio_codepage = STDIO_CP;
|
|
|
|
|
#else
|
|
|
|
|
static int stdio_codepage = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
|
typedef struct log_t {
|
2016-01-31 19:54:49 +01:00
|
|
|
|
void(*log)(void *data, int level, const char *module, const char *format, va_list args);
|
|
|
|
|
void *data;
|
|
|
|
|
int flags;
|
2016-02-05 23:10:05 +01:00
|
|
|
|
struct log_t *next;
|
|
|
|
|
} log_t;
|
2016-01-31 19:54:49 +01:00
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
|
static log_t *loggers;
|
2016-01-31 19:54:49 +01:00
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
|
log_t *log_create(int flags, void *data, log_fun call) {
|
|
|
|
|
log_t *lgr = malloc(sizeof(log_t));
|
2016-01-31 19:54:49 +01:00
|
|
|
|
lgr->log = call;
|
|
|
|
|
lgr->flags = flags;
|
|
|
|
|
lgr->data = data;
|
|
|
|
|
lgr->next = loggers;
|
|
|
|
|
loggers = lgr;
|
2016-02-05 23:10:05 +01:00
|
|
|
|
return lgr;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 13:47:30 +02:00
|
|
|
|
void log_destroy(log_t *handle) {
|
|
|
|
|
log_t ** lp = &loggers;
|
2016-05-01 13:39:25 +02:00
|
|
|
|
while (*lp) {
|
2016-05-01 13:47:30 +02:00
|
|
|
|
log_t *lg = *lp;
|
|
|
|
|
if (lg==handle) {
|
2016-05-01 13:39:25 +02:00
|
|
|
|
*lp = lg->next;
|
|
|
|
|
free(lg);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lp = &lg->next;
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
#define MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */
|
|
|
|
|
#define LOG_MAXBACKUPS 5
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
static int
|
2016-10-02 13:12:47 +02:00
|
|
|
|
cp_convert(const char *format, unsigned char *buffer, size_t length, int codepage)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
|
/* when console output on MSDOS, convert to codepage */
|
|
|
|
|
const char *input = format;
|
2016-10-02 13:12:47 +02:00
|
|
|
|
unsigned char *pos = buffer;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
|
while (pos + 1 < buffer + length && *input) {
|
2015-07-06 19:53:09 +02:00
|
|
|
|
size_t size = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
|
int result = 0;
|
|
|
|
|
if (codepage == 437) {
|
2015-07-06 19:53:09 +02:00
|
|
|
|
result = unicode_utf8_to_cp437(pos, input, &size);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
|
|
|
|
else if (codepage == 1252) {
|
2015-07-06 19:53:09 +02:00
|
|
|
|
result = unicode_utf8_to_cp1252(pos, input, &size);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
*pos = 0; /* just in case caller ignores our return value */
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
++pos;
|
|
|
|
|
input += length;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
*pos = 0;
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-05-16 09:11:49 +02:00
|
|
|
|
void log_rotate(const char *filename, int maxindex)
|
|
|
|
|
{
|
2015-11-05 14:05:58 +01:00
|
|
|
|
char buffer[2][MAX_PATH];
|
2016-08-09 18:28:15 +02:00
|
|
|
|
int dst = 1;
|
2015-11-05 14:05:58 +01:00
|
|
|
|
assert(strlen(filename) < sizeof(buffer[0]) - 4);
|
2015-11-04 14:16:15 +01:00
|
|
|
|
|
2015-11-05 14:05:58 +01:00
|
|
|
|
sprintf(buffer[dst], "%s.%d", filename, maxindex);
|
2016-08-09 18:28:15 +02:00
|
|
|
|
if (remove(buffer[dst]) != 0) {
|
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
|
fprintf(stderr, "log rotate %s: %d %s", buffer[dst], errno, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
|
|
|
|
}
|
2016-07-31 18:48:27 +02:00
|
|
|
|
|
2015-11-05 14:05:58 +01:00
|
|
|
|
while (maxindex > 0) {
|
2016-08-05 20:15:05 +02:00
|
|
|
|
int src = 1 - dst;
|
2015-11-05 14:05:58 +01:00
|
|
|
|
sprintf(buffer[src], "%s.%d", filename, --maxindex);
|
2016-08-09 18:28:15 +02:00
|
|
|
|
if (rename(buffer[src], buffer[dst]) != 0) {
|
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
|
fprintf(stderr, "log rotate %s: %d %s", buffer[dst], errno, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
2015-11-05 14:05:58 +01:00
|
|
|
|
dst = src;
|
|
|
|
|
}
|
2016-08-09 18:28:15 +02:00
|
|
|
|
if (rename(filename, buffer[dst]) != 0) {
|
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
|
fprintf(stderr, "log rotate %s: %d %s", buffer[dst], errno, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
errno = 0;
|
2012-05-16 09:11:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
static const char *log_prefix(int level) {
|
|
|
|
|
const char * prefix = "ERROR";
|
|
|
|
|
if (level == LOG_CPWARNING) prefix = "WARNING";
|
|
|
|
|
else if (level == LOG_CPDEBUG) prefix = "DEBUG";
|
|
|
|
|
else if (level == LOG_CPINFO) prefix = "INFO";
|
|
|
|
|
return prefix;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 20:55:27 +02:00
|
|
|
|
static int check_dupe(const char *format, int level)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
static int last_type; /* STATIC_XCALL: used across calls */
|
2015-01-30 20:37:14 +01:00
|
|
|
|
static char last_message[32]; /* STATIC_XCALL: used across calls */
|
|
|
|
|
static int dupes = 0; /* STATIC_XCALL: used across calls */
|
|
|
|
|
if (strncmp(last_message, format, sizeof(last_message)) == 0) {
|
2016-08-29 14:49:39 +02:00
|
|
|
|
// TODO: C6054: String 'last_message' might not be zero - terminated.
|
2015-01-30 20:37:14 +01:00
|
|
|
|
++dupes;
|
|
|
|
|
return 1;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
if (dupes) {
|
2016-05-20 20:55:27 +02:00
|
|
|
|
if (level & LOG_CPERROR) {
|
|
|
|
|
fprintf(stderr, "%s: last message repeated %d times\n", log_prefix(last_type),
|
|
|
|
|
dupes + 1);
|
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
dupes = 0;
|
|
|
|
|
}
|
2015-10-29 09:04:23 +01:00
|
|
|
|
strlcpy(last_message, format, sizeof(last_message));
|
2016-05-20 20:55:27 +02:00
|
|
|
|
last_type = level;
|
2015-01-30 20:37:14 +01:00
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
static void _log_write(FILE * stream, int codepage, const char *format, va_list args)
|
2012-05-16 01:56:25 +02:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
if (codepage) {
|
|
|
|
|
char buffer[MAXLENGTH];
|
2016-10-02 13:12:47 +02:00
|
|
|
|
unsigned char converted[MAXLENGTH];
|
2016-01-31 19:54:49 +01:00
|
|
|
|
vsnprintf(buffer, sizeof(buffer), format, args);
|
|
|
|
|
if (cp_convert(buffer, converted, MAXLENGTH, codepage) == 0) {
|
2016-10-02 13:12:47 +02:00
|
|
|
|
fputs((char *)converted, stream);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
2016-01-31 19:54:49 +01:00
|
|
|
|
/* fall back to non-converted output */
|
2015-01-30 20:37:14 +01:00
|
|
|
|
vfprintf(stream, format, args);
|
|
|
|
|
}
|
2012-05-16 01:56:25 +02:00
|
|
|
|
}
|
2016-01-31 19:54:49 +01:00
|
|
|
|
else {
|
|
|
|
|
vfprintf(stream, format, args);
|
|
|
|
|
}
|
2012-05-16 07:21:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
static void log_stdio(void *data, int level, const char *module, const char *format, va_list args) {
|
|
|
|
|
FILE *out = (FILE *)data;
|
|
|
|
|
int codepage = (out == stderr || out == stdout) ? stdio_codepage : 0;
|
|
|
|
|
const char *prefix = log_prefix(level);
|
2014-08-11 12:02:16 +02:00
|
|
|
|
size_t len = strlen(format);
|
2016-01-31 19:54:49 +01:00
|
|
|
|
|
|
|
|
|
fprintf(out, "%s: ", prefix);
|
|
|
|
|
|
|
|
|
|
_log_write(out, codepage, format, args);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
if (format[len - 1] != '\n') {
|
2016-01-31 19:54:49 +01:00
|
|
|
|
fputc('\n', out);
|
2014-08-11 12:02:16 +02:00
|
|
|
|
}
|
2016-05-20 20:55:27 +02:00
|
|
|
|
fflush(out);
|
2014-08-11 12:02:16 +02:00
|
|
|
|
}
|
2012-05-16 07:21:59 +02:00
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
|
log_t *log_to_file(int flags, FILE *out) {
|
|
|
|
|
return log_create(flags, out, log_stdio);
|
2016-01-31 19:54:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void log_write(int flags, const char *module, const char *format, va_list args) {
|
2016-02-05 23:10:05 +01:00
|
|
|
|
log_t *lg;
|
2016-01-31 19:54:49 +01:00
|
|
|
|
for (lg = loggers; lg; lg = lg->next) {
|
|
|
|
|
int level = flags & LOG_LEVELS;
|
|
|
|
|
if (lg->flags & level) {
|
|
|
|
|
int dupe = 0;
|
2016-05-01 13:40:01 +02:00
|
|
|
|
va_list copy;
|
2016-05-01 13:39:25 +02:00
|
|
|
|
|
2016-05-01 13:40:01 +02:00
|
|
|
|
va_copy(copy, args);
|
2016-01-31 19:54:49 +01:00
|
|
|
|
if (lg->flags & LOG_BRIEF) {
|
|
|
|
|
dupe = check_dupe(format, level);
|
|
|
|
|
}
|
|
|
|
|
if (dupe == 0) {
|
2016-05-01 13:40:01 +02:00
|
|
|
|
lg->log(lg->data, level, NULL, format, copy);
|
2016-01-31 19:54:49 +01:00
|
|
|
|
}
|
2016-05-01 13:40:01 +02:00
|
|
|
|
va_end(copy);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-16 01:56:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-17 04:12:55 +02:00
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
void log_fatal(const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPERROR, NULL, format, args);
|
|
|
|
|
va_end(args);
|
2014-06-17 04:12:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-23 19:19:04 +01:00
|
|
|
|
void log_error(const char *format, ...) //-V524
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPERROR, NULL, format, args);
|
|
|
|
|
va_end(args);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
void log_warning(const char *format, ...)
|
2015-02-23 02:47:08 +01:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPWARNING, NULL, format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
2015-02-23 02:47:08 +01:00
|
|
|
|
|
2016-01-31 19:54:49 +01:00
|
|
|
|
void log_debug(const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPDEBUG, NULL, format, args);
|
|
|
|
|
va_end(args);
|
2015-02-23 02:47:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-06-17 07:38:44 +02:00
|
|
|
|
void log_info(const char *format, ...)
|
2012-05-16 07:21:59 +02:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPINFO, NULL, format, args);
|
|
|
|
|
va_end(args);
|
2012-05-16 07:21:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void log_printf(FILE * io, const char *format, ...)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2016-01-31 19:54:49 +01:00
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
log_write(LOG_CPINFO, NULL, format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static FILE *logfile;
|
|
|
|
|
|
|
|
|
|
void log_close(void)
|
|
|
|
|
{
|
|
|
|
|
while (loggers) {
|
2016-02-05 23:10:05 +01:00
|
|
|
|
log_t *lgr = loggers;
|
2016-01-31 19:54:49 +01:00
|
|
|
|
loggers = lgr->next;
|
|
|
|
|
free(lgr);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
2016-01-31 19:54:49 +01:00
|
|
|
|
if (logfile) {
|
|
|
|
|
time_t ltime;
|
|
|
|
|
time(<ime);
|
|
|
|
|
fprintf(logfile, "===\n=== Logfile closed at %s===\n\n", ctime(<ime));
|
|
|
|
|
fclose(logfile);
|
|
|
|
|
}
|
|
|
|
|
logfile = 0;
|
|
|
|
|
}
|
2012-05-16 07:21:59 +02:00
|
|
|
|
|
2016-02-05 23:10:05 +01:00
|
|
|
|
log_t *log_open(const char *filename, int log_flags)
|
2016-01-31 19:54:49 +01:00
|
|
|
|
{
|
|
|
|
|
log_rotate(filename, LOG_MAXBACKUPS);
|
|
|
|
|
logfile = fopen(filename, "a");
|
|
|
|
|
if (logfile) {
|
|
|
|
|
/* Get UNIX-style time and display as number and string. */
|
|
|
|
|
time_t ltime;
|
|
|
|
|
time(<ime);
|
|
|
|
|
fprintf(logfile, "===\n=== Logfile started at %s===\n", ctime(<ime));
|
2016-02-05 23:10:05 +01:00
|
|
|
|
return log_create(log_flags, logfile, log_stdio);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2016-05-20 20:55:27 +02:00
|
|
|
|
return NULL;
|
2016-02-05 23:10:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int log_level(log_t * log, int flags)
|
|
|
|
|
{
|
|
|
|
|
int old = log->flags;
|
|
|
|
|
log->flags = flags;
|
|
|
|
|
return old;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|