Merge branch 'master' into develop

Conflicts:
	src/buildno.h
	src/util/log.c
	src/util/log.h
This commit is contained in:
Enno Rehling 2016-05-01 13:47:30 +02:00
commit c4d31d368e
7 changed files with 61 additions and 6 deletions

View File

@ -74,9 +74,6 @@ faction *factions;
static void free_faction(faction * f) static void free_faction(faction * f)
{ {
funhash(f); funhash(f);
if (f->alliance && f->alliance->_leader == f) {
setalliance(f, 0);
}
if (f->msgs) { if (f->msgs) {
free_messagelist(f->msgs->begin); free_messagelist(f->msgs->begin);
free(f->msgs); free(f->msgs);
@ -454,7 +451,7 @@ void destroyfaction(faction ** fp)
} }
#endif #endif
if (f->alliance && f->alliance->_leader == f) { if (f->alliance) {
setalliance(f, 0); setalliance(f, 0);
} }

View File

@ -13,6 +13,7 @@
#include "monster.h" #include "monster.h"
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
#include <quicklist.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
@ -46,8 +47,10 @@ static void test_remove_empty_factions_alliance(CuTest *tc) {
al = makealliance(0, "Hodor"); al = makealliance(0, "Hodor");
setalliance(f, al); setalliance(f, al);
CuAssertPtrEquals(tc, f, alliance_get_leader(al)); CuAssertPtrEquals(tc, f, alliance_get_leader(al));
CuAssertIntEquals(tc, 1, ql_length(al->members));
remove_empty_factions(); remove_empty_factions();
CuAssertPtrEquals(tc, 0, al->_leader); CuAssertPtrEquals(tc, 0, al->_leader);
CuAssertIntEquals(tc, 0, ql_length(al->members));
test_cleanup(); test_cleanup();
} }
@ -61,6 +64,9 @@ static void test_remove_empty_factions(CuTest *tc) {
f = test_create_faction(0); f = test_create_faction(0);
fno = f->no; fno = f->no;
remove_empty_factions(); remove_empty_factions();
CuAssertIntEquals(tc, false, f->_alive);
CuAssertPtrEquals(tc, fm, factions);
CuAssertPtrEquals(tc, NULL, fm->next);
CuAssertPtrEquals(tc, 0, findfaction(fno)); CuAssertPtrEquals(tc, 0, findfaction(fno));
CuAssertPtrEquals(tc, fm, get_monsters()); CuAssertPtrEquals(tc, fm, get_monsters());
test_cleanup(); test_cleanup();

View File

@ -81,6 +81,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(umlaut); ADD_SUITE(umlaut);
ADD_SUITE(unicode); ADD_SUITE(unicode);
ADD_SUITE(strings); ADD_SUITE(strings);
ADD_SUITE(log);
ADD_SUITE(rng); ADD_SUITE(rng);
/* items */ /* items */
ADD_SUITE(xerewards); ADD_SUITE(xerewards);

View File

@ -9,6 +9,7 @@ attrib.test.c
strings.test.c strings.test.c
bsdstring.test.c bsdstring.test.c
functions.test.c functions.test.c
log.test.c
umlaut.test.c umlaut.test.c
unicode.test.c unicode.test.c
rng.test.c rng.test.c

View File

@ -50,6 +50,19 @@ log_t *log_create(int flags, void *data, log_fun call) {
return lgr; 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 MAXLENGTH 4096 /* because I am lazy, CP437 output is limited to this many chars */
#define LOG_MAXBACKUPS 5 #define LOG_MAXBACKUPS 5
@ -137,7 +150,6 @@ static void _log_write(FILE * stream, int codepage, const char *format, va_list
if (codepage) { if (codepage) {
char buffer[MAXLENGTH]; char buffer[MAXLENGTH];
char converted[MAXLENGTH]; char converted[MAXLENGTH];
vsnprintf(buffer, sizeof(buffer), format, args); vsnprintf(buffer, sizeof(buffer), format, args);
if (cp_convert(buffer, converted, MAXLENGTH, codepage) == 0) { if (cp_convert(buffer, converted, MAXLENGTH, codepage) == 0) {
fputs(converted, stream); 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; int level = flags & LOG_LEVELS;
if (lg->flags & level) { if (lg->flags & level) {
int dupe = 0; int dupe = 0;
va_list copy;
va_copy(copy, args);
if (lg->flags & LOG_BRIEF) { if (lg->flags & LOG_BRIEF) {
dupe = check_dupe(format, level); dupe = check_dupe(format, level);
} }
if (dupe == 0) { if (dupe == 0) {
lg->log(lg->data, level, NULL, format, args); lg->log(lg->data, level, NULL, format, copy);
} }
va_end(copy);
} }
} }
} }

View File

@ -23,6 +23,7 @@ extern "C" {
struct log_t * log_open(const char *filename, int flags); struct log_t * log_open(const char *filename, int flags);
struct log_t * log_create(int flags, void *data, log_fun call); 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); struct log_t * log_to_file(int flags, FILE *out);
int log_level(struct log_t *log, int flags); int log_level(struct log_t *log, int flags);
void log_close(void); void log_close(void);

33
src/util/log.test.c Normal file
View File

@ -0,0 +1,33 @@
#include <CuTest.h>
#include "log.h"
#include <stdarg.h>
#include <string.h>
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;
}