From e00d4867e1476535390515c67267918c0ee9f35a Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 10 May 2020 08:28:01 +0000 Subject: [PATCH] Refactor dbp. NFC. After this commit, dbp() is renamed to DebugPrint() and moved to platform.cpp, next to other similar functions. The existing short name is provided by a preprocessor macro, similar to ssassert(). This leaves just the (rather hacky) temporary heap in util*.cpp. --- src/platform/platform.cpp | 48 +++++++++++++++++++++++++++++++++++++++ src/platform/platform.h | 3 +++ src/platform/utilunix.cpp | 11 --------- src/platform/utilwin.cpp | 20 ---------------- src/solvespace.h | 10 ++++---- 5 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/platform/platform.cpp b/src/platform/platform.cpp index 9f8ff83..308c3fb 100644 --- a/src/platform/platform.cpp +++ b/src/platform/platform.cpp @@ -633,5 +633,53 @@ std::vector InitCli(int argc, char **argv) { #endif +//----------------------------------------------------------------------------- +// Debug output, on *nix. +//----------------------------------------------------------------------------- + +#if defined(WIN32) + +void DebugPrint(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + int len = _vscprintf(fmt, va) + 1; + va_end(va); + + va_start(va, fmt); + char *buf = (char *)_alloca(len); + _vsnprintf(buf, len, fmt, va); + va_end(va); + + // The native version of OutputDebugString, unlike most others, + // is OutputDebugStringA. + OutputDebugStringA(buf); + OutputDebugStringA("\n"); + +#ifndef NDEBUG + // Duplicate to stderr in debug builds, but not in release; this is slow. + fputs(buf, stderr); + fputc('\n', stderr); +#endif +} + +#endif + +//----------------------------------------------------------------------------- +// Debug output, on *nix. +//----------------------------------------------------------------------------- + +#if !defined(WIN32) + +void DebugPrint(const char *fmt, ...) { + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + fputc('\n', stderr); + va_end(va); +} + +#endif + } } diff --git a/src/platform/platform.h b/src/platform/platform.h index de3f985..adf6228 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -67,6 +67,9 @@ const void *LoadResource(const std::string &name, size_t *size); // Startup and command-line argument handling. std::vector InitCli(int argc, char **argv); +// Debug print function. +void DebugPrint(const char *fmt, ...); + } #endif diff --git a/src/platform/utilunix.cpp b/src/platform/utilunix.cpp index fb38331..9a471c6 100644 --- a/src/platform/utilunix.cpp +++ b/src/platform/utilunix.cpp @@ -8,17 +8,6 @@ namespace SolveSpace { -void dbp(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - vfprintf(stdout, fmt, va); - fputc('\n', stdout); - va_end(va); - - fflush(stdout); -} - //----------------------------------------------------------------------------- // A separate heap, on which we allocate expressions. Maybe a bit faster, // since fragmentation is less of a concern, and it also makes it possible diff --git a/src/platform/utilwin.cpp b/src/platform/utilwin.cpp index 87a2fb0..16bce19 100644 --- a/src/platform/utilwin.cpp +++ b/src/platform/utilwin.cpp @@ -10,26 +10,6 @@ namespace SolveSpace { -void dbp(const char *str, ...) -{ - va_list f; - static char buf[1024*50]; - va_start(f, str); - _vsnprintf(buf, sizeof(buf), str, f); - va_end(f); - - // The native version of OutputDebugString, unlike most others, - // is OutputDebugStringA. - OutputDebugStringA(buf); - OutputDebugStringA("\n"); - -#ifndef NDEBUG - // Duplicate to stderr in debug builds, but not in release; this is slow. - fputs(buf, stderr); - fputc('\n', stderr); -#endif -} - //----------------------------------------------------------------------------- // A separate heap, on which we allocate expressions. Maybe a bit faster, // since no fragmentation issues whatsoever, and it also makes it possible diff --git a/src/solvespace.h b/src/solvespace.h index 2227fa7..7ca2e01 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -71,6 +71,11 @@ typedef struct _cairo_surface cairo_surface_t; } while(0) #endif +#define dbp SolveSpace::Platform::DebugPrint +#define DBPTRI(tri) \ + dbp("tri: (%.3f %.3f %.3f) (%.3f %.3f %.3f) (%.3f %.3f %.3f)", \ + CO((tri).a), CO((tri).b), CO((tri).c)) + #ifndef isnan # define isnan(x) (((x) != (x)) || (x > 1e11) || (x < -1e11)) #endif @@ -141,11 +146,6 @@ const size_t MAX_RECENT = 8; #define AUTOSAVE_EXT "slvs~" -void dbp(const char *str, ...); -#define DBPTRI(tri) \ - dbp("tri: (%.3f %.3f %.3f) (%.3f %.3f %.3f) (%.3f %.3f %.3f)", \ - CO((tri).a), CO((tri).b), CO((tri).c)) - void *AllocTemporary(size_t n); void FreeAllTemporary();