From 83f3d820f19360a3d733af79e89af352bbf0aa14 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Fri, 5 Oct 2018 15:41:27 +0300 Subject: [PATCH] mdbx-windows: refine debug-logging. --- src/mdbx.c | 24 ++++++++++++++++++++++++ src/osal.c | 23 +++++++++++++++++------ src/osal.h | 4 +++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index 883b7a31..d621eb57 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -1389,6 +1389,30 @@ void __cold mdbx_debug_log(int type, const char *function, int line, if (mdbx_debug_logger) mdbx_debug_logger(type, function, line, fmt, args); else { +#if defined(_WIN32) || defined(_WIN64) + if (IsDebuggerPresent()) { + int prefix_len = 0; + char *prefix = nullptr; + if (function && line > 0) + prefix_len = mdbx_asprintf(&prefix, "%s:%d ", function, line); + else if (function) + prefix_len = mdbx_asprintf(&prefix, "%s: ", function); + else if (line > 0) + prefix_len = mdbx_asprintf(&prefix, "%d: ", line); + if (prefix_len > 0 && prefix) { + OutputDebugStringA(prefix); + free(prefix); + } + char *msg = nullptr; + int msg_len = mdbx_vasprintf(&msg, fmt, args); + if (msg_len > 0 && msg) { + OutputDebugStringA(msg); + free(msg); + } + va_end(args); + return; + } +#endif if (function && line > 0) fprintf(stderr, "%s:%d ", function, line); else if (function) diff --git a/src/osal.c b/src/osal.c index c2fced0b..2a037447 100644 --- a/src/osal.c +++ b/src/osal.c @@ -193,11 +193,9 @@ __cold void mdbx_panic(const char *fmt, ...) { /*----------------------------------------------------------------------------*/ -#ifndef mdbx_asprintf -int mdbx_asprintf(char **strp, const char *fmt, ...) { - va_list ap, ones; - - va_start(ap, fmt); +#ifndef mdbx_vasprintf +int mdbx_vasprintf(char **strp, const char *fmt, va_list ap) { + va_list ones; va_copy(ones, ap); #ifdef _MSC_VER int needed = _vscprintf(fmt, ap); @@ -207,7 +205,6 @@ int mdbx_asprintf(char **strp, const char *fmt, ...) { #else #error FIXME #endif - va_end(ap); if (unlikely(needed < 0 || needed >= INT_MAX)) { *strp = nullptr; @@ -218,7 +215,11 @@ int mdbx_asprintf(char **strp, const char *fmt, ...) { *strp = malloc(needed + 1); if (unlikely(*strp == nullptr)) { va_end(ones); +#if defined(_WIN32) || defined(_WIN64) SetLastError(MDBX_ENOMEM); +#else + errno = MDBX_ENOMEM; +#endif return -1; } @@ -237,6 +238,16 @@ int mdbx_asprintf(char **strp, const char *fmt, ...) { } return actual; } +#endif /* mdbx_vasprintf */ + +#ifndef mdbx_asprintf +int mdbx_asprintf(char **strp, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int rc = mdbx_vasprintf(strp, fmt, ap); + va_end(ap); + return rc; +} #endif /* mdbx_asprintf */ #ifndef mdbx_memalign_alloc diff --git a/src/osal.h b/src/osal.h index 5cca43b6..0f3da8a8 100644 --- a/src/osal.h +++ b/src/osal.h @@ -386,8 +386,10 @@ void mdbx_assert_fail(const MDBX_env *env, const char *msg, const char *func, #if __GLIBC_PREREQ(2, 1) #define mdbx_asprintf asprintf +#define mdbx_vasprintf vasprintf #else -int mdbx_asprintf(char **strp, const char *fmt, ...); +__printf_args(2, 3) int mdbx_asprintf(char **strp, const char *fmt, ...); +int mdbx_vasprintf(char **strp, const char *fmt, va_list ap); #endif #ifdef _MSC_VER