diff --git a/src/kernel/faction.c b/src/kernel/faction.c index cf8d73143..9270fcf06 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -74,9 +74,6 @@ faction *factions; static void free_faction(faction * f) { funhash(f); - if (f->alliance && f->alliance->_leader == f) { - setalliance(f, 0); - } if (f->msgs) { free_messagelist(f->msgs->begin); free(f->msgs); @@ -454,7 +451,7 @@ void destroyfaction(faction ** fp) } #endif - if (f->alliance && f->alliance->_leader == f) { + if (f->alliance) { setalliance(f, 0); } diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 94a1910f8..ecd0df2ab 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -13,6 +13,7 @@ #include "monster.h" #include #include +#include #include #include @@ -46,8 +47,10 @@ static void test_remove_empty_factions_alliance(CuTest *tc) { al = makealliance(0, "Hodor"); setalliance(f, al); CuAssertPtrEquals(tc, f, alliance_get_leader(al)); + CuAssertIntEquals(tc, 1, ql_length(al->members)); remove_empty_factions(); CuAssertPtrEquals(tc, 0, al->_leader); + CuAssertIntEquals(tc, 0, ql_length(al->members)); test_cleanup(); } @@ -61,6 +64,9 @@ static void test_remove_empty_factions(CuTest *tc) { f = test_create_faction(0); fno = f->no; remove_empty_factions(); + CuAssertIntEquals(tc, false, f->_alive); + CuAssertPtrEquals(tc, fm, factions); + CuAssertPtrEquals(tc, NULL, fm->next); CuAssertPtrEquals(tc, 0, findfaction(fno)); CuAssertPtrEquals(tc, fm, get_monsters()); test_cleanup(); diff --git a/src/test_eressea.c b/src/test_eressea.c index 658f20b9b..50ff37d19 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -81,6 +81,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(umlaut); ADD_SUITE(unicode); ADD_SUITE(strings); + ADD_SUITE(log); ADD_SUITE(rng); /* items */ ADD_SUITE(xerewards); diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 6fc0b5dea..3d5032462 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -9,6 +9,7 @@ attrib.test.c strings.test.c bsdstring.test.c functions.test.c +log.test.c umlaut.test.c unicode.test.c rng.test.c diff --git a/src/util/log.c b/src/util/log.c index 4afee68b4..6d8d5a7f4 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -50,6 +50,19 @@ log_t *log_create(int flags, void *data, log_fun call) { return lgr; } +void log_destroy(log_t *handle) { + log_t ** lp = &loggers; + while (*lp) { + log_t *lg = *lp; + if (lg==handle) { + *lp = lg->next; + free(lg); + break; + } + lp = &lg->next; + } +} + #define MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */ #define LOG_MAXBACKUPS 5 @@ -137,7 +150,6 @@ static void _log_write(FILE * stream, int codepage, const char *format, va_list if (codepage) { char buffer[MAXLENGTH]; char converted[MAXLENGTH]; - vsnprintf(buffer, sizeof(buffer), format, args); if (cp_convert(buffer, converted, MAXLENGTH, codepage) == 0) { fputs(converted, stream); @@ -176,12 +188,16 @@ static void log_write(int flags, const char *module, const char *format, va_list int level = flags & LOG_LEVELS; if (lg->flags & level) { int dupe = 0; + va_list copy; + + va_copy(copy, args); if (lg->flags & LOG_BRIEF) { dupe = check_dupe(format, level); } if (dupe == 0) { - lg->log(lg->data, level, NULL, format, args); + lg->log(lg->data, level, NULL, format, copy); } + va_end(copy); } } } diff --git a/src/util/log.h b/src/util/log.h index 56adfa1c3..bd38cfa7d 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -23,6 +23,7 @@ extern "C" { struct log_t * log_open(const char *filename, int flags); struct log_t * log_create(int flags, void *data, log_fun call); + void log_destroy(struct log_t *handle); struct log_t * log_to_file(int flags, FILE *out); int log_level(struct log_t *log, int flags); void log_close(void); diff --git a/src/util/log.test.c b/src/util/log.test.c new file mode 100644 index 000000000..7dbe65f74 --- /dev/null +++ b/src/util/log.test.c @@ -0,0 +1,33 @@ +#include + +#include "log.h" + +#include +#include + +void log_string(void *data, int level, const char *module, const char *format, va_list args) { + char *str = (char *)data; + const char *arg = va_arg(args, const char *); + strncpy(str, arg, 32); +} + +static void test_logging(CuTest * tc) +{ + char str1[32]; + char str2[32]; + struct log_t * id1 = log_create(LOG_CPWARNING, str1, log_string); + struct log_t * id2 = log_create(LOG_CPWARNING, str2, log_string); + CuAssertTrue(tc, id1!=id2); + log_warning("Hello %s", "World"); + CuAssertStrEquals(tc, str1, "World"); + CuAssertStrEquals(tc, str2, "World"); + log_destroy(id1); + log_destroy(id2); +} + +CuSuite *get_log_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_logging); + return suite; +}