Implemented a custom assert() that uses the log system if possible
Bonus: no need to include assert.h
This commit is contained in:
parent
6530a39bd2
commit
455039edb4
17 changed files with 38 additions and 21 deletions
2
src/assert.h
Normal file
2
src/assert.h
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
#error Do not include assert.h, Taisei provides its own implementation.
|
|
@ -6,7 +6,6 @@
|
|||
* Copyright (C) 2012, Alexeyew Andrew <http://akari.thebadasschoobs.org/>
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "color.h"
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "global.h"
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
|
||||
#include "hashtable.h"
|
||||
#include "list.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <zlib.h>
|
||||
#include <stdio.h>
|
||||
|
@ -301,13 +301,13 @@ void hashtable_print_stringkeys(Hashtable *ht) {
|
|||
int total = 0;
|
||||
int collisions = 0;
|
||||
|
||||
printf("------ %p:\n", (void*)ht);
|
||||
log_debug("------ %p:", (void*)ht);
|
||||
for(size_t i = 0; i < ht->table_size; ++i) {
|
||||
int elems = 0;
|
||||
printf("[bucket %lu] %p\n", (unsigned long)i, (void*)ht->table[i]);
|
||||
log_debug("[bucket %lu] %p", (unsigned long)i, (void*)ht->table[i]);
|
||||
|
||||
for(HashtableElement *e = ht->table[i]; e; e = e->next) {
|
||||
printf(" -- %s (%lu): %p\n", (char*)e->key, (unsigned long)e->hash, e->data);
|
||||
log_debug(" -- %s (%lu): %p", (char*)e->key, (unsigned long)e->hash, e->data);
|
||||
++elems;
|
||||
++total;
|
||||
}
|
||||
|
@ -323,7 +323,7 @@ void hashtable_print_stringkeys(Hashtable *ht) {
|
|||
}
|
||||
}
|
||||
|
||||
printf("%i total elements, %i unused buckets, %i collisions, max %i elems per bucket, %lu approx overhead\n",
|
||||
log_debug("%i total elements, %i unused buckets, %i collisions, max %i elems per bucket, %lu approx overhead",
|
||||
total, free_buckets, collisions, max_elems,
|
||||
(unsigned long int)hashtable_get_approx_overhead(ht));
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ void hashtable_print_stringkeys(Hashtable *ht) {
|
|||
static void hashtable_printstrings(Hashtable *ht) {
|
||||
for(size_t i = 0; i < ht->table_size; ++i) {
|
||||
for(HashtableElement *e = ht->table[i]; e; e = e->next) {
|
||||
printf("[HT %lu] %s (%lu): %s\n", (unsigned long)i, (char*)e->key, (unsigned long)e->hash, (char*)e->data);
|
||||
log_info("[HT %lu] %s (%lu): %s\n", (unsigned long)i, (char*)e->key, (unsigned long)e->hash, (char*)e->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -365,7 +365,7 @@ int hashtable_test(void) {
|
|||
}
|
||||
|
||||
hashtable_printstrings(ht);
|
||||
printf("-----\n");
|
||||
log_info("-----\n");
|
||||
hashtable_set_string(ht, "12345", "asdfg");
|
||||
hashtable_unset_string(ht, "test");
|
||||
hashtable_set_string(ht, "herp", "deeeeeerp");
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <SDL_bits.h>
|
||||
#include <SDL_mutex.h>
|
||||
|
||||
|
@ -34,7 +33,7 @@ static const char *level_prefix_map[] = { "D", "I", "W", "E" };
|
|||
|
||||
static const char* level_prefix(LogLevel lvl) {
|
||||
int idx = SDL_MostSignificantBitIndex32(lvl);
|
||||
assert(idx >= 0 && idx < sizeof(level_prefix_map) / sizeof(char*));
|
||||
assert_nolog(idx >= 0 && idx < sizeof(level_prefix_map) / sizeof(char*));
|
||||
return level_prefix_map[idx];
|
||||
}
|
||||
|
||||
|
@ -171,6 +170,10 @@ void log_shutdown(void) {
|
|||
log_mutex = NULL;
|
||||
}
|
||||
|
||||
bool log_initialized(void) {
|
||||
return log_mutex;
|
||||
}
|
||||
|
||||
void log_add_output(LogLevel levels, SDL_RWops *output) {
|
||||
if(!output) {
|
||||
return;
|
||||
|
|
|
@ -60,6 +60,7 @@ void log_shutdown(void);
|
|||
void log_add_output(LogLevel levels, SDL_RWops *output);
|
||||
void log_backtrace(LogLevel lvl);
|
||||
LogLevel log_parse_levels(LogLevel lvls, const char *lvlmod);
|
||||
bool log_initialized(void);
|
||||
|
||||
#ifdef DEBUG
|
||||
#define log_debug(...) _taisei_log(LOG_DEBUG, false, __func__, __VA_ARGS__)
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "menu.h"
|
||||
#include "global.h"
|
||||
#include "video.h"
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
#include "replay.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include "font.h"
|
||||
#include "global.h"
|
||||
#include "paths/native.h"
|
||||
#include <assert.h>
|
||||
|
||||
struct Fonts _fonts;
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
#include "font.h"
|
||||
#include "model.h"
|
||||
#include "hashtable.h"
|
||||
#include "assert.h"
|
||||
#include "paths/native.h"
|
||||
|
||||
typedef enum ResourceType {
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
*/
|
||||
|
||||
#include <png.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "texture.h"
|
||||
#include "resource.h"
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include "rwops_segment.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#include <SDL.h>
|
||||
#include <stdint.h>
|
||||
#include <zlib.h>
|
||||
#include <assert.h>
|
||||
#include "rwops_zlib.h"
|
||||
#include "util.h"
|
||||
|
||||
|
|
|
@ -5,8 +5,6 @@
|
|||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "util.h"
|
||||
#include "stage.h"
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* Copyright (C) 2011, Lukas Weber <laochailan@web.de>
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "global.h"
|
||||
|
||||
#define TAISEIGL_NO_EXT_ABSTRACTION
|
||||
|
|
11
src/util.c
11
src/util.c
|
@ -419,6 +419,17 @@ void tsfprintf(FILE *out, const char *restrict fmt, ...) {
|
|||
// misc utils
|
||||
//
|
||||
|
||||
void _ts_assert_fail(const char *cond, const char *func, const char *file, int line, bool use_log) {
|
||||
use_log = use_log && log_initialized();
|
||||
|
||||
if(use_log) {
|
||||
log_fatal("%s:%i: %s(): assertion `%s` failed", file, line, func, cond);
|
||||
} else {
|
||||
tsfprintf(stderr, "%s:%i: %s(): assertion `%s` failed", file, line, func, cond);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
int getenvint(const char *v) {
|
||||
char *e = getenv(v);
|
||||
|
||||
|
|
12
src/util.h
12
src/util.h
|
@ -115,6 +115,18 @@ void tsfprintf(FILE *out, const char *restrict fmt, ...) __attribute__((format(p
|
|||
|
||||
int getenvint(const char *v) __attribute__((pure));
|
||||
void png_setup_error_handlers(png_structp png);
|
||||
noreturn void _ts_assert_fail(const char *cond, const char *func, const char *file, int line, bool use_log);
|
||||
|
||||
#undef assert
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define _assert(cond,uselog)
|
||||
#else
|
||||
#define _assert(cond,uselog) ((cond) ? (void)0 : _ts_assert_fail(#cond, __func__, __FILE__, __LINE__, uselog))
|
||||
#endif
|
||||
|
||||
#define assert(cond) _assert(cond, true)
|
||||
#define assert_nolog(cond) _assert(cond, false)
|
||||
|
||||
//
|
||||
// safeguards against some dangerous or otherwise undesirable practices
|
||||
|
|
Loading…
Reference in a new issue