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>
|
2017-12-29 11:44:14 +01:00
|
|
|
|
#include "log.h"
|
2017-12-30 11:09:09 +01:00
|
|
|
|
|
2018-10-06 20:47:23 +02:00
|
|
|
|
#include "path.h"
|
2017-12-30 19:49:21 +01:00
|
|
|
|
#include "strings.h"
|
2018-10-06 20:47:23 +02:00
|
|
|
|
#include "unicode.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2018-10-21 19:22:30 +02:00
|
|
|
|
#include <critbit.h>
|
|
|
|
|
|
2012-06-04 03:41:07 +02:00
|
|
|
|
#include <assert.h>
|
2015-11-04 14:55:13 +01:00
|
|
|
|
#include <errno.h>
|
2017-12-29 11:58:39 +01:00
|
|
|
|
#include <limits.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2018-05-03 22:40:54 +02:00
|
|
|
|
void errno_check(const char * file, int line) {
|
|
|
|
|
if (errno) {
|
|
|
|
|
log_info("errno is %d (%s) at %s:%d",
|
|
|
|
|
errno, strerror(errno), file, line);
|
|
|
|
|
errno = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#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));
|
2018-11-26 22:01:18 +01:00
|
|
|
|
if (!lgr) abort();
|
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)
|
|
|
|
|
{
|
2017-12-29 11:44:14 +01:00
|
|
|
|
char buffer[2][PATH_MAX];
|
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 */
|
2018-11-26 22:01:18 +01:00
|
|
|
|
static char last_message[32] = { 0 }; /* STATIC_XCALL: used across calls */
|
2015-01-30 20:37:14 +01:00
|
|
|
|
static int dupes = 0; /* STATIC_XCALL: used across calls */
|
|
|
|
|
if (strncmp(last_message, format, sizeof(last_message)) == 0) {
|
2017-02-18 21:15:14 +01: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;
|
|
|
|
|
}
|
2017-12-30 19:49:21 +01:00
|
|
|
|
str_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
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 19:32:10 +01:00
|
|
|
|
/*
|
|
|
|
|
* Notes for va_copy compatibility:
|
|
|
|
|
* MSVC: https://social.msdn.microsoft.com/Forums/vstudio/en-US/53a4fd75-9f97-48b2-aa63-2e2e5a15efa3/stdcversion-problem?forum=vclanguage
|
|
|
|
|
* GNU: https://www.gnu.org/software/libc/manual/html_node/Argument-Macros.html
|
|
|
|
|
*/
|
|
|
|
|
static void vlog(log_t *lg, int level, const char *format, va_list args) {
|
|
|
|
|
va_list copy;
|
|
|
|
|
va_copy(copy, args);
|
|
|
|
|
lg->log(lg->data, level, NULL, format, copy);
|
|
|
|
|
va_end(copy);
|
|
|
|
|
}
|
2017-12-29 17:00:16 +01:00
|
|
|
|
|
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;
|
|
|
|
|
if (lg->flags & LOG_BRIEF) {
|
|
|
|
|
dupe = check_dupe(format, level);
|
|
|
|
|
}
|
|
|
|
|
if (dupe == 0) {
|
2018-12-11 19:32:10 +01:00
|
|
|
|
vlog(lg, level, format, args);
|
2016-01-31 19:54:49 +01:00
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-16 01:56:25 +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);
|
2017-10-14 08:43:01 +02:00
|
|
|
|
abort();
|
2014-06-17 04:12:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 21:15:14 +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
|
|
|
|
}
|
2018-10-21 19:22:30 +02:00
|
|
|
|
|
|
|
|
|
static critbit_tree stats = CRITBIT_TREE();
|
|
|
|
|
|
|
|
|
|
int stats_count(const char *stat, int delta) {
|
|
|
|
|
void * match;
|
|
|
|
|
if (cb_find_prefix_str(&stats, stat, &match, 1, 0) == 0) {
|
2018-12-02 05:04:49 +01:00
|
|
|
|
size_t len;
|
|
|
|
|
char data[128];
|
2018-10-21 19:22:30 +02:00
|
|
|
|
len = cb_new_kv(stat, strlen(stat), &delta, sizeof(delta), data);
|
|
|
|
|
cb_insert(&stats, data, len);
|
|
|
|
|
return delta;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
int *num;
|
|
|
|
|
cb_get_kv_ex(match, (void **)&num);
|
|
|
|
|
return *num += delta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
#define STATS_BATCH 8
|
|
|
|
|
void stats_walk(const char *prefix, void(*callback)(const char *, int, void *), void *udata) {
|
|
|
|
|
void *match[STATS_BATCH];
|
|
|
|
|
int n, off = 0;
|
|
|
|
|
do {
|
|
|
|
|
int i;
|
|
|
|
|
n = cb_find_prefix_str(&stats, prefix, match, STATS_BATCH, off);
|
|
|
|
|
if (n == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
off += n;
|
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
|
const void *kv = match[i];
|
|
|
|
|
int *num;
|
|
|
|
|
cb_get_kv_ex(kv, &(void *)num);
|
|
|
|
|
callback(kv, *num, udata);
|
|
|
|
|
}
|
|
|
|
|
} while (n == STATS_BATCH);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
struct walk_data {
|
|
|
|
|
int (*callback)(const char *, int, void *);
|
|
|
|
|
void *udata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int walk_cb(void * match, const void * key, size_t keylen, void *udata) {
|
|
|
|
|
struct walk_data *data = (struct walk_data *)udata;
|
|
|
|
|
int *num;
|
|
|
|
|
cb_get_kv_ex(match, (void **)&num);
|
|
|
|
|
return data->callback((const char*)match, *num, data->udata);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int stats_walk(const char *prefix, int (*callback)(const char *, int, void *), void *udata) {
|
|
|
|
|
struct walk_data data;
|
|
|
|
|
data.callback = callback;
|
|
|
|
|
data.udata = udata;
|
|
|
|
|
return cb_foreach(&stats, prefix, strlen(prefix), walk_cb, &data);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static int write_cb(const char *key, int val, void *udata) {
|
|
|
|
|
FILE * F = (FILE *)udata;
|
|
|
|
|
fprintf(F, "%s: %d\n", (const char *)key, val);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stats_write(FILE *F, const char *prefix) {
|
|
|
|
|
stats_walk(prefix, write_cb, F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stats_close(void) {
|
|
|
|
|
cb_clear(&stats);
|
|
|
|
|
}
|