mdbx-windows: refine debug-logging.

This commit is contained in:
Leonid Yuriev 2018-10-05 15:41:27 +03:00
parent 0f82db941b
commit 83f3d820f1
3 changed files with 44 additions and 7 deletions

View File

@ -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)

View File

@ -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

View File

@ -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