more correct and portable printing of size_t and fixed-width integers

previously it didn't work properly on windows
This commit is contained in:
Andrei "Akari" Alexeyev 2017-04-21 02:11:53 +03:00
parent 4611a2077f
commit e3da5f27f6
6 changed files with 29 additions and 20 deletions

View file

@ -111,11 +111,11 @@ int color_test(void) {
clr2 = rgba(clra[0], clra[1], clra[2], clra[3]);
clr3 = derive_color(clr1, CLRMASK_A, rgba(0, 0, 0, -1.0));
printf("1: %016llx (%f %f %f %f)\n", (unsigned long long int)clr1,
printf("1: %016"PRIxMAX" (%f %f %f %f)\n", (uintmax_t)clr1,
color_component(clr1, CLR_R), color_component(clr1, CLR_G), color_component(clr1, CLR_B), color_component(clr1, CLR_A));
printf("2: %016llx (%f %f %f %f)\n", (unsigned long long int)clr2,
printf("2: %016"PRIxMAX" (%f %f %f %f)\n", (uintmax_t)clr2,
color_component(clr2, CLR_R), color_component(clr2, CLR_G), color_component(clr2, CLR_B), color_component(clr2, CLR_A));
printf("3: %016llx (%f %f %f %f)\n", (unsigned long long int)clr3,
printf("3: %016"PRIxMAX" (%f %f %f %f)\n", (uintmax_t)clr3,
color_component(clr3, CLR_R), color_component(clr3, CLR_G), color_component(clr3, CLR_B), color_component(clr3, CLR_A));
assert(clr1 == clr2);

View file

@ -253,7 +253,7 @@ static size_t hashtable_find_optimal_size(Hashtable *ht) {
int cols = hashtable_check_collisions_with_new_size(ht, s);
if(cols < col_tolerance) {
log_debug("Optimal size for %p is %llu (%i collisions)", (void*)ht, (unsigned long long)s, cols);
log_debug("Optimal size for %p is %"PRIuMAX" (%i collisions)", (void*)ht, (uintmax_t)s, cols);
return s;
}
@ -263,7 +263,7 @@ static size_t hashtable_find_optimal_size(Hashtable *ht) {
}
}
log_debug("Optimal size for %p is %llu (%i collisions)", (void*)ht, (unsigned long long)best_size, min_cols);
log_debug("Optimal size for %p is %"PRIuMAX" (%i collisions)", (void*)ht, (uintmax_t)best_size, min_cols);
return best_size;
}
@ -287,8 +287,8 @@ static void hashtable_resize_internal(Hashtable *ht, size_t new_size) {
ht->table = new_table;
log_debug("Resized hashtable at %p: %llu -> %llu",
(void*)ht, (unsigned long long)ht->table_size, (unsigned long long)new_size);
log_debug("Resized hashtable at %p: %"PRIuMAX" -> %"PRIuMAX"",
(void*)ht, (uintmax_t)ht->table_size, (uintmax_t)new_size);
ht->table_size = new_size;
}
@ -501,10 +501,10 @@ void hashtable_print_stringkeys(Hashtable *ht) {
log_debug("------ %p:", (void*)ht);
for(size_t i = 0; i < ht->table_size; ++i) {
log_debug("[bucket %lu] %p", (unsigned long)i, (void*)ht->table[i]);
log_debug("[bucket %"PRIuMAX"] %p", (uintmax_t)i, (void*)ht->table[i]);
for(HashtableElement *e = ht->table[i]; e; e = e->next) {
log_debug(" -- %s (%lu): %p", (char*)e->key, (unsigned long)e->hash, e->data);
log_debug(" -- %s (%"PRIuMAX"): %p", (char*)e->key, (uintmax_t)e->hash, e->data);
}
}
@ -526,7 +526,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) {
log_info("[HT %lu] %s (%lu): %s\n", (unsigned long)i, (char*)e->key, (unsigned long)e->hash, (char*)e->data);
log_info("[HT %"PRIuMAX"] %s (%"PRIuMAX"): %s\n", (uintmax_t)i, (char*)e->key, (uintmax_t)e->hash, (char*)e->data);
}
}
}

View file

@ -62,16 +62,24 @@ void log_backtrace(LogLevel lvl);
LogLevel log_parse_levels(LogLevel lvls, const char *lvlmod);
bool log_initialized(void);
#ifdef __WINDOWS__
// hack to supporess warnings about MS format string extensions, like %I64.
// taisei of course never uses them directly, but MinGW uses them in stdint.h format macros.
#define LOG_PREFIX __extension__
#else
#define LOG_PREFIX
#endif
#ifdef DEBUG
#define log_debug(...) _taisei_log(LOG_DEBUG, false, __func__, __VA_ARGS__)
#define log_debug(...) LOG_PREFIX _taisei_log(LOG_DEBUG, false, __func__, __VA_ARGS__)
#else
#define log_debug(...)
#endif
#define log_info(...) _taisei_log(LOG_INFO, false, __func__, __VA_ARGS__)
#define log_warn(...) _taisei_log(LOG_WARN, false, __func__, __VA_ARGS__)
#define log_fatal(...) _taisei_log_fatal(LOG_FATAL, __func__, __VA_ARGS__)
#define log_custom(lvl, ...) _taisei_log(lvl, false, __func__, __VA_ARGS__)
#define log_info(...) LOG_PREFIX _taisei_log(LOG_INFO, false, __func__, __VA_ARGS__)
#define log_warn(...) LOG_PREFIX _taisei_log(LOG_WARN, false, __func__, __VA_ARGS__)
#define log_fatal(...) LOG_PREFIX _taisei_log_fatal(LOG_FATAL, __func__, __VA_ARGS__)
#define log_custom(lvl, ...) LOG_PREFIX _taisei_log(lvl, false, __func__, __VA_ARGS__)
//
// don't call these directly, use the macros

View file

@ -93,7 +93,7 @@ static void progress_read(SDL_RWops *file) {
}
if(filesize > PROGRESS_MAXFILESIZE) {
log_warn("Progress file is huge (%li bytes, %i max)", (long)filesize, PROGRESS_MAXFILESIZE);
log_warn("Progress file is huge (%"PRIi64" bytes, %i max)", filesize, PROGRESS_MAXFILESIZE);
return;
}

View file

@ -254,7 +254,7 @@ int replay_write(Replay *rpy, SDL_RWops *file, bool compression) {
}
#ifdef REPLAY_LOAD_GARBAGE_TEST
#define PRINTPROP(prop,fmt) log_debug(#prop " = %" # fmt " [%li / %li]", prop, (long int)SDL_RWtell(file), (long int)filesize)
#define PRINTPROP(prop,fmt) log_debug(#prop " = %" # fmt " [%"PRIi64" / %"PRIi64"]", prop, SDL_RWtell(file), filesize)
#else
#define PRINTPROP(prop,fmt) (void)(prop)
#endif
@ -379,14 +379,14 @@ int replay_read(Replay *rpy, SDL_RWops *file, ReplayReadMode mode) {
if(filesize < 0) {
log_warn("SDL_RWsize() failed: %s", SDL_GetError());
} else {
log_debug("%li bytes", (long int)filesize);
log_debug("%"PRIi64" bytes", filesize);
}
if(mode & REPLAY_READ_META) {
memset(rpy, 0, sizeof(Replay));
if(filesize > 0 && filesize <= sizeof(replay_magic_header) + 2) {
log_warn("Replay file is too short (%li)", (long int)filesize);
log_warn("Replay file is too short (%"PRIi64")", filesize);
return false;
}
@ -400,7 +400,7 @@ int replay_read(Replay *rpy, SDL_RWops *file, ReplayReadMode mode) {
if(rpy->version & REPLAY_VERSION_COMPRESSION_BIT) {
if(rpy->fileoffset < SDL_RWtell(file)) {
log_warn("Invalid offset %li", (long int)rpy->fileoffset);
log_warn("Invalid offset %"PRIi32"", rpy->fileoffset);
return false;
}

View file

@ -18,6 +18,7 @@
#ifndef __GNUC__ // clang defines this too
#define __attribute__(...)
#define __extension__
#endif
//