diff options
-rw-r--r-- | hugin.c | 49 |
1 files changed, 44 insertions, 5 deletions
@@ -351,12 +351,39 @@ static int hug_output_fd(int fd, const char *msg, int withdate) return 0; } +// These functions returns how big a buffer is needed to "render" the arg list. +static int vscprintf(const char *fmt, va_list va) +{ +#ifdef WIN32 + return _vscprintf(fmt, va); +#else + return vsnprintf(NULL, 0, fmt, va); +#endif +} + +static int scprintf(const char *fmt, ...) +{ + int size; + va_list va; + va_start(va, fmt); + size = vscprintf(fmt, va); + va_end(va); + + return size; +} + +#define HDR_ARGS debug_class_str[(unsigned)cl], ch, func, line + int __debug(const char *func, const int line, const enum __debug_class cl, const char *ch, const char *fmt, ...) { int result = 0; int sz; + char *buf = NULL; + int hdr_bufsz = 0; + int msg_bufsz = 0; + const char hdr_fmt[] = "%s:%s:%s:%d "; // NOTE: This must be identical to the debug_class_str in debug_filter.c const char * const debug_class_str[] = @@ -372,13 +399,23 @@ int __debug(const char *func, const int line, // Generate message // - char buf[1024]; - sz = snprintf(buf, sizeof(buf), - "%s:%s:%s:%d ", - debug_class_str[(unsigned)cl], ch, func, line); + // Get number of bytes needed for the buffer: va_list va; va_start(va, fmt); - sz += vsnprintf(buf + sz, sizeof(buf) - sz, fmt, va); + + hdr_bufsz = scprintf(hdr_fmt, HDR_ARGS); + msg_bufsz = vscprintf(fmt, va); + if(hdr_bufsz < 0 || msg_bufsz < 0) return 1; // Bad format? + + buf = (char*)malloc(hdr_bufsz + msg_bufsz + 1); + if(!buf) return 1; // Out of memory + + sz = sprintf(buf, hdr_fmt, HDR_ARGS); + if(sz < 0) return 1; // Unknown error + + sz = vsprintf(buf + sz, fmt, va); + if(sz < 0) return 1; // Unknown error + va_end(va); // @@ -412,6 +449,8 @@ done: #endif hug_mutex_unlock(); + if(buf) free(buf); + return result; } |