From a59fdf87cb02fc5f082683c9d4b439ebb28fb650 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Feb 2021 18:27:24 +0100 Subject: [PATCH] separate stats and log --- src/automate.c | 1 + src/battle.c | 73 +++++++++++++++++----------------- src/main.c | 17 ++++---- src/monsters.c | 25 ++++++------ src/randenc.c | 6 +-- src/teleport.c | 22 +++++------ src/test_eressea.c | 15 +++---- src/tests.c | 53 +++++++++++++------------ src/util/CMakeLists.txt | 2 + src/util/log.c | 87 ++--------------------------------------- src/util/log.h | 32 ++++++--------- src/util/log.test.c | 1 + src/util/stats.c | 70 +++++++++++++++++++++++++++++++++ src/util/stats.h | 15 +++++++ tolua | 2 +- 15 files changed, 207 insertions(+), 214 deletions(-) create mode 100644 src/util/stats.c create mode 100644 src/util/stats.h diff --git a/src/automate.c b/src/automate.c index e3f5b656a..53ac074a7 100644 --- a/src/automate.c +++ b/src/automate.c @@ -11,6 +11,7 @@ #include "util/keyword.h" #include "util/log.h" +#include "util/stats.h" #include "automate.h" #include "laws.h" diff --git a/src/battle.c b/src/battle.c index 2d45c6df8..ff0544442 100644 --- a/src/battle.c +++ b/src/battle.c @@ -13,48 +13,49 @@ #include "study.h" #include "spy.h" -#include -#include -#include +#include "spells/buildingcurse.h" +#include "spells/regioncurse.h" +#include "spells/unitcurse.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#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 +#include "reports.h" /* attributes includes */ -#include -#include -#include +#include "attributes/key.h" +#include "attributes/racename.h" +#include "attributes/otherfaction.h" /* util includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#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 diff --git a/src/main.c b/src/main.c index b937c80fa..aa908b8ea 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,3 @@ -#ifdef _MSC_VER -#include -#endif - #include #include #include @@ -10,6 +6,7 @@ #include #include +#include #include #include @@ -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; } diff --git a/src/monsters.c b/src/monsters.c index 148e6f46d..058d98b9f 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -1,7 +1,3 @@ -#ifdef _MSC_VER -#include -#endif - #include "monsters.h" #include "economy.h" @@ -34,21 +30,22 @@ #include "kernel/unit.h" /* util includes */ -#include +#include "util/base36.h" #include "util/keyword.h" #include "util/language.h" -#include -#include -#include -#include +#include "util/log.h" +#include "util/stats.h" +#include "util/rand.h" +#include "util/rng.h" +#include "util/strings.h" /* attributes includes */ -#include -#include -#include -#include +#include "attributes/hate.h" +#include "attributes/otherfaction.h" +#include "attributes/stealth.h" +#include "attributes/targetregion.h" -#include +#include "spells/regioncurse.h" #include diff --git a/src/randenc.c b/src/randenc.c index 5464f0063..56e53832e 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -1,7 +1,3 @@ -#ifdef _MSC_VER -#include -#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" diff --git a/src/teleport.c b/src/teleport.c index 067764946..486b7bd34 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -1,19 +1,19 @@ -#include -#include #include "teleport.h" /* kernel includes */ -#include -#include -#include -#include -#include -#include -#include +#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 -#include +#include "util/log.h" +#include "util/stats.h" +#include "util/rng.h" #include "skill.h" #include "monsters.h" diff --git a/src/test_eressea.c b/src/test_eressea.c index 452b847c5..b644bc771 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -1,14 +1,15 @@ -#include -#include -#include +#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 #include #include #include -#include -#include -#include -#include #pragma warning(disable: 4210) diff --git a/src/tests.c b/src/tests.c index 9739591c8..7972b3adb 100644 --- a/src/tests.c +++ b/src/tests.c @@ -10,33 +10,34 @@ #include "kernel/callbacks.h" #include "kernel/config.h" #include "kernel/alliance.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#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 -#include -#include -#include -#include -#include -#include -#include -#include -#include +#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 diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 5a0b57e6b..261086307 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -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 diff --git a/src/util/log.c b/src/util/log.c index 47bf09320..2d91240bb 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -1,4 +1,3 @@ -#include #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); -} diff --git a/src/util/log.h b/src/util/log.h index ce254c406..7d8b2634b 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -1,11 +1,20 @@ -#ifndef H_UTIL_LOG -#define H_UTIL_LOG +#pragma once #ifdef __cplusplus extern "C" { #endif #include #include +#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 diff --git a/src/util/log.test.c b/src/util/log.test.c index 928ba5660..527776d4b 100644 --- a/src/util/log.test.c +++ b/src/util/log.test.c @@ -3,6 +3,7 @@ #endif #include "log.h" +#include "stats.h" #include "macros.h" #include diff --git a/src/util/stats.c b/src/util/stats.c new file mode 100644 index 000000000..c203907ec --- /dev/null +++ b/src/util/stats.c @@ -0,0 +1,70 @@ +#include "log.h" + +#include "path.h" +#include "strings.h" +#include "unicode.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +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); +} diff --git a/src/util/stats.h b/src/util/stats.h new file mode 100644 index 000000000..d1a574d00 --- /dev/null +++ b/src/util/stats.h @@ -0,0 +1,15 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif +#include + + 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 diff --git a/tolua b/tolua index 8a658c005..c36200dae 160000 --- a/tolua +++ b/tolua @@ -1 +1 @@ -Subproject commit 8a658c00574f2e4889f2631be167b597b6f087e7 +Subproject commit c36200dae924ffb6eab89b802801d807ff96bfd0