From 40dd718dd18f1a6e09317d85ea90cc68a8a55f76 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Fri, 4 Jan 2019 21:25:13 +0100 Subject: [PATCH] Redirect libTIFF errors and Warnings --- src/gfx2log.c | 16 +++++++--------- src/tifformat.c | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/gfx2log.c b/src/gfx2log.c index 7cb541a9..f6f04597 100644 --- a/src/gfx2log.c +++ b/src/gfx2log.c @@ -41,15 +41,6 @@ extern void GFX2_Log(GFX2_Log_priority_T priority, const char * fmt, ...) va_start(ap, fmt); GFX2_LogV(priority, fmt, ap); va_end(ap); -#if defined(_MSC_VER) && defined(_DEBUG) - { - char message[1024]; - va_start(ap, fmt); - vsnprintf(message, sizeof(message), fmt, ap); - va_end(ap); - OutputDebugStringA(message); - } -#endif } extern void GFX2_LogV(GFX2_Log_priority_T priority, const char * fmt, va_list ap) @@ -83,6 +74,13 @@ extern void GFX2_LogV(GFX2_Log_priority_T priority, const char * fmt, va_list ap #else vfprintf((unsigned)priority >= GFX2_INFO ? stdout : stderr, fmt, ap); #endif +#if defined(_MSC_VER) && defined(_DEBUG) + { + char message[1024]; + vsnprintf(message, sizeof(message), fmt, ap); + OutputDebugStringA(message); + } +#endif } extern void GFX2_LogHexDump(GFX2_Log_priority_T priority, const char * header, const byte * data, long offset, long count) diff --git a/src/tifformat.c b/src/tifformat.c index ac4655f7..97403ac8 100644 --- a/src/tifformat.c +++ b/src/tifformat.c @@ -22,8 +22,6 @@ ///@file tifformat.c /// Support of TIFF /// -/// @todo use TIFFSetErrorHandler() and TIFFSetWarningHandler() to -/// redirect warning/error output to our own functions #ifndef __no_tifflib__ @@ -43,6 +41,37 @@ extern char Program_version[]; // generated in pversion.c extern const char SVN_revision[]; // generated in version.c + +static void TIFF_LogError(const char* module, const char* fmt, va_list ap) +{ + char format[256]; + snprintf(format, sizeof(format), "%s: %s\n", module, fmt); + GFX2_LogV(GFX2_ERROR, format, ap); +} + +static void TIFF_LogWarning(const char* module, const char* fmt, va_list ap) +{ + char format[256]; + snprintf(format, sizeof(format), "%s: %s\n", module, fmt); + GFX2_LogV(GFX2_WARNING, format, ap); +} + +/// Initialisation for using the TIFF library +static void TIFF_Init(void) +{ + static int init_done = 0; + + if (init_done) + return; + + /// use TIFFSetErrorHandler() and TIFFSetWarningHandler() to + /// redirect warning/error output to our own functions + TIFFSetErrorHandler(TIFF_LogError); + TIFFSetWarningHandler(TIFF_LogWarning); + + init_done = 1; +} + /// test for a valid TIFF void Test_TIFF(T_IO_Context * context, FILE * file) { @@ -366,6 +395,7 @@ void Load_TIFF(T_IO_Context * context) file = Open_file_read(context); if (file != NULL) { + TIFF_Init(); tif = TIFFFdOpen(fileno(file), context->File_name, "r"); if (tif != NULL) { @@ -379,6 +409,7 @@ void Load_TIFF(T_IO_Context * context) File_error = 1; Get_full_filename(filename, context->File_name, context->File_directory); + TIFF_Init(); tif = TIFFOpen(filename, "r"); if (tif != NULL) { @@ -468,6 +499,7 @@ void Save_TIFF(T_IO_Context * context) File_error = 1; + TIFF_Init(); #if defined(WIN32) if (context->File_name_unicode != NULL && context->File_name_unicode[0] != 0) tif = TIFFOpenW(context->File_name_unicode, "w");