separate stats and log

This commit is contained in:
Enno Rehling 2021-02-14 18:27:24 +01:00
parent b6b95f58ed
commit a59fdf87cb
15 changed files with 207 additions and 214 deletions

View File

@ -11,6 +11,7 @@
#include "util/keyword.h"
#include "util/log.h"
#include "util/stats.h"
#include "automate.h"
#include "laws.h"

View File

@ -13,48 +13,49 @@
#include "study.h"
#include "spy.h"
#include <spells/buildingcurse.h>
#include <spells/regioncurse.h>
#include <spells/unitcurse.h>
#include "spells/buildingcurse.h"
#include "spells/regioncurse.h"
#include "spells/unitcurse.h"
#include <kernel/ally.h>
#include <kernel/alliance.h>
#include <kernel/build.h>
#include <kernel/building.h>
#include <kernel/curse.h>
#include <kernel/equipment.h>
#include <kernel/faction.h>
#include <kernel/group.h>
#include <kernel/item.h>
#include <kernel/messages.h>
#include <kernel/order.h>
#include <kernel/plane.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/ship.h>
#include <kernel/terrain.h>
#include <kernel/unit.h>
#include <kernel/spell.h>
#include "kernel/ally.h"
#include "kernel/alliance.h"
#include "kernel/build.h"
#include "kernel/building.h"
#include "kernel/curse.h"
#include "kernel/equipment.h"
#include "kernel/faction.h"
#include "kernel/group.h"
#include "kernel/item.h"
#include "kernel/messages.h"
#include "kernel/order.h"
#include "kernel/plane.h"
#include "kernel/race.h"
#include "kernel/region.h"
#include "kernel/ship.h"
#include "kernel/terrain.h"
#include "kernel/unit.h"
#include "kernel/spell.h"
#include <reports.h>
#include "reports.h"
/* attributes includes */
#include <attributes/key.h>
#include <attributes/racename.h>
#include <attributes/otherfaction.h>
#include "attributes/key.h"
#include "attributes/racename.h"
#include "attributes/otherfaction.h"
/* util includes */
#include <util/assert.h>
#include <kernel/attrib.h>
#include <util/base36.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/macros.h>
#include <util/parser.h>
#include <util/strings.h>
#include <util/rand.h>
#include <util/rng.h>
#include "util/assert.h"
#include "kernel/attrib.h"
#include "util/base36.h"
#include "util/language.h"
#include "util/lists.h"
#include "util/log.h"
#include "util/macros.h"
#include "util/parser.h"
#include "util/strings.h"
#include "util/stats.h"
#include "util/rand.h"
#include "util/rng.h"
#include <selist.h>

View File

@ -1,7 +1,3 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include <kernel/calendar.h>
#include <kernel/config.h>
#include <kernel/messages.h>
@ -10,6 +6,7 @@
#include <util/language.h>
#include <util/log.h>
#include <util/stats.h>
#include <util/path.h>
#include <util/password.h>
@ -142,9 +139,9 @@ static int get_arg(int argc, char **argv, size_t len, int index, const char **re
return index;
}
static int verbosity_to_flags(int verbosity) {
static int verbosity_to_flags(int value) {
int flags = 0;
switch (verbosity) {
switch (value) {
case 0:
flags = 0;
break;
@ -167,7 +164,7 @@ static int verbosity_to_flags(int verbosity) {
static int parse_args(int argc, char **argv)
{
int i;
int log_stderr, log_flags = 3;
int log_flags = 3;
for (i = 1; i != argc; ++i) {
char *argi = argv[i];
@ -245,9 +242,9 @@ static int parse_args(int argc, char **argv)
log_open(logfile, log_flags);
/* also log to stderr: */
log_stderr = verbosity_to_flags(verbosity);
if (log_stderr) {
log_to_file(log_stderr | LOG_FLUSH | LOG_BRIEF, stderr);
log_flags = verbosity_to_flags(verbosity);
if (log_flags) {
log_to_file(log_flags | LOG_FLUSH | LOG_BRIEF, stderr);
}
return 0;
}

View File

@ -1,7 +1,3 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include "monsters.h"
#include "economy.h"
@ -34,21 +30,22 @@
#include "kernel/unit.h"
/* util includes */
#include <util/base36.h>
#include "util/base36.h"
#include "util/keyword.h"
#include "util/language.h"
#include <util/log.h>
#include <util/rand.h>
#include <util/rng.h>
#include <util/strings.h>
#include "util/log.h"
#include "util/stats.h"
#include "util/rand.h"
#include "util/rng.h"
#include "util/strings.h"
/* attributes includes */
#include <attributes/hate.h>
#include <attributes/otherfaction.h>
#include <attributes/stealth.h>
#include <attributes/targetregion.h>
#include "attributes/hate.h"
#include "attributes/otherfaction.h"
#include "attributes/stealth.h"
#include "attributes/targetregion.h"
#include <spells/regioncurse.h>
#include "spells/regioncurse.h"
#include <selist.h>

View File

@ -1,7 +1,3 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include "randenc.h"
#include "chaos.h"
@ -41,7 +37,7 @@
#include "kernel/attrib.h"
#include "util/language.h"
#include "util/lists.h"
#include "util/log.h"
#include "util/stats.h"
#include "util/rand.h"
#include "util/message.h"
#include "util/rng.h"

View File

@ -1,19 +1,19 @@
#include <platform.h>
#include <kernel/config.h>
#include "teleport.h"
/* kernel includes */
#include <kernel/equipment.h>
#include <kernel/unit.h>
#include <kernel/region.h>
#include <kernel/race.h>
#include <kernel/terrain.h>
#include <kernel/faction.h>
#include <kernel/plane.h>
#include "kernel/config.h"
#include "kernel/equipment.h"
#include "kernel/unit.h"
#include "kernel/region.h"
#include "kernel/race.h"
#include "kernel/terrain.h"
#include "kernel/faction.h"
#include "kernel/plane.h"
/* util includes */
#include <util/log.h>
#include <util/rng.h>
#include "util/log.h"
#include "util/stats.h"
#include "util/rng.h"
#include "skill.h"
#include "monsters.h"

View File

@ -1,14 +1,15 @@
#include <platform.h>
#include <eressea.h>
#include <kernel/config.h>
#include "eressea.h"
#include "kernel/config.h"
#include "util/log.h"
#include "util/stats.h"
#include "util/password.h"
#include "util/strings.h"
#include "util/variant.h"
#include <CuTest.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <util/log.h>
#include <util/password.h>
#include <util/strings.h>
#include <util/variant.h>
#pragma warning(disable: 4210)

View File

@ -10,33 +10,34 @@
#include "kernel/callbacks.h"
#include "kernel/config.h"
#include "kernel/alliance.h"
#include <kernel/equipment.h>
#include <kernel/messages.h>
#include <kernel/plane.h>
#include <kernel/region.h>
#include <kernel/terrain.h>
#include <kernel/terrainid.h>
#include <kernel/item.h>
#include <kernel/unit.h>
#include <kernel/order.h>
#include <kernel/race.h>
#include <kernel/faction.h>
#include <kernel/building.h>
#include <kernel/ship.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <kernel/terrain.h>
#include "kernel/equipment.h"
#include "kernel/messages.h"
#include "kernel/plane.h"
#include "kernel/region.h"
#include "kernel/terrain.h"
#include "kernel/terrainid.h"
#include "kernel/item.h"
#include "kernel/unit.h"
#include "kernel/order.h"
#include "kernel/race.h"
#include "kernel/faction.h"
#include "kernel/building.h"
#include "kernel/ship.h"
#include "kernel/spell.h"
#include "kernel/spellbook.h"
#include "kernel/terrain.h"
#include <util/aliases.h>
#include <util/functions.h>
#include <util/keyword.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/message.h>
#include <util/log.h>
#include <util/param.h>
#include <util/rand.h>
#include <util/assert.h>
#include "util/aliases.h"
#include "util/functions.h"
#include "util/keyword.h"
#include "util/language.h"
#include "util/lists.h"
#include "util/message.h"
#include "util/log.h"
#include "util/stats.h"
#include "util/param.h"
#include "util/rand.h"
#include "util/assert.h"
#include <CuTest.h>

View File

@ -13,6 +13,7 @@ keyword.test.c
language.test.c
# lists.test.c
# log.test.c
# stats.test.c
message.test.c
# nrmessage.test.c
order_parser.test.c
@ -42,6 +43,7 @@ keyword.c
language.c
lists.c
log.c
stats.c
message.c
mt19937ar.c
nrmessage.c

View File

@ -1,4 +1,3 @@
#include <platform.h>
#include "log.h"
#include "path.h"
@ -181,6 +180,7 @@ static void log_stdio(void *data, int level, const char *module, const char *for
const char *prefix = log_prefix(level);
size_t len = strlen(format);
(void)module;
fprintf(out, "%s: ", prefix);
_log_write(out, codepage, format, args);
@ -208,6 +208,9 @@ static void vlog(log_t *lg, int level, const char *format, va_list args) {
static void log_write(int flags, const char *module, const char *format, va_list args) {
log_t *lg;
(void)module;
for (lg = loggers; lg; lg = lg->next) {
int level = flags & LOG_LEVELS;
if (lg->flags & level) {
@ -263,14 +266,6 @@ void log_info(const char *format, ...)
va_end(args);
}
void log_printf(FILE * io, const char *format, ...)
{
va_list args;
va_start(args, format);
log_write(LOG_CPINFO, NULL, format, args);
va_end(args);
}
static FILE *logfile;
void log_close(void)
@ -309,77 +304,3 @@ int log_level(log_t * log, int flags)
log->flags = flags;
return old;
}
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) {
size_t len;
char data[128];
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);
}

View File

@ -1,11 +1,20 @@
#ifndef H_UTIL_LOG
#define H_UTIL_LOG
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
#define LOG_CPERROR 0x01
#define LOG_CPWARNING 0x02
#define LOG_CPINFO 0x04
#define LOG_CPDEBUG 0x08
#define LOG_LEVELS 0x0F
#define LOG_FLUSH 0x10
#define LOG_BRIEF 0x20
#define ERRNO_CHECK() errno_check(__FILE__, __LINE__)
struct log_t;
typedef void(*log_fun)(void *data, int level, const char *module, const char *format, va_list args);
@ -22,29 +31,10 @@ extern "C" {
void log_warning(const char *format, ...);
void log_debug(const char *format, ...);
void log_info(const char *format, ...);
void log_printf(FILE * ios, const char *format, ...);
void errno_check(const char *file, int line);
int stats_count(const char *stat, int delta);
void stats_write(FILE *F, const char *prefix);
int stats_walk(const char *prefix, int (*callback)(const char *key, int val, void * udata), void *udata);
void stats_close(void);
#define ERRNO_CHECK() errno_check(__FILE__, __LINE__)
#define LOG_CPERROR 0x01
#define LOG_CPWARNING 0x02
#define LOG_CPINFO 0x04
#define LOG_CPDEBUG 0x08
#define LOG_LEVELS 0x0F
#define LOG_FLUSH 0x10
#define LOG_BRIEF 0x20
extern int log_stderr;
#ifdef __cplusplus
}
#endif
#endif

View File

@ -3,6 +3,7 @@
#endif
#include "log.h"
#include "stats.h"
#include "macros.h"
#include <CuTest.h>

70
src/util/stats.c Normal file
View File

@ -0,0 +1,70 @@
#include "log.h"
#include "path.h"
#include "strings.h"
#include "unicode.h"
#include <critbit.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
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) {
size_t len;
char data[128];
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;
}
}
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;
(void)key;
(void)keylen;
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);
}
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);
}

15
src/util/stats.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
int stats_count(const char *stat, int delta);
void stats_write(FILE *F, const char *prefix);
int stats_walk(const char *prefix, int (*callback)(const char *key, int val, void * udata), void *udata);
void stats_close(void);
#ifdef __cplusplus
}
#endif

2
tolua

@ -1 +1 @@
Subproject commit 8a658c00574f2e4889f2631be167b597b6f087e7
Subproject commit c36200dae924ffb6eab89b802801d807ff96bfd0