Skip to content

Commit

Permalink
Refactor dbp. NFC.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
whitequark committed May 10, 2020
1 parent 9951da8 commit e00d486
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
48 changes: 48 additions & 0 deletions src/platform/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,5 +633,53 @@ std::vector<std::string> 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

}
}
3 changes: 3 additions & 0 deletions src/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ const void *LoadResource(const std::string &name, size_t *size);
// Startup and command-line argument handling.
std::vector<std::string> InitCli(int argc, char **argv);

// Debug print function.
void DebugPrint(const char *fmt, ...);

}

#endif
11 changes: 0 additions & 11 deletions src/platform/utilunix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions src/platform/utilwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/solvespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit e00d486

Please sign in to comment.