Reimplement DebugPrint using variadic templates (Closes: #367)

pull/3/head
Hleb Valoshka 2019-08-17 09:56:21 +03:00 committed by eyvallah
parent 71ca974838
commit ca39d82b09
2 changed files with 28 additions and 56 deletions

View File

@ -7,44 +7,7 @@
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <cstdio>
#include <cstdarg>
#include <config.h>
#include <fmt/printf.h>
static int debugVerbosity = 0;
#if defined(DEBUG) || defined(_DEBUG)
void DebugPrint(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
if (level <= debugVerbosity)
{
#ifdef _MSC_VER
if (IsDebuggerPresent())
{
char buf[1024];
fmt::vsprintf(buf, format, args);
OutputDebugStringA(buf);
}
else
{
fmt::vfprintf(stdout, format, args);
}
#else
fmt::vfprintf(stderr, format, args);
#endif
}
va_end(args);
}
#endif /* DEBUG */
int debugVerbosity = 0;
void SetDebugVerbosity(int dv)
{

View File

@ -10,31 +10,40 @@
#ifndef _DEBUG_H_
#define _DEBUG_H_
// Define the DPRINTF macro; g++ supports macros with variable
// length arguments, so we'll use those when we can.
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <iostream>
#include <fmt/printf.h>
#ifdef DPRINTF
#undef DPRINTF // OSX has DPRINTF
#endif // DPRINTF
#ifdef __GNUC__
#if !defined(_DEBUG) && !defined(DEBUG)
#define DPRINTF(level, args...)
#define DPRINTF(level, format, ...)
#else
#define DPRINTF(level, args...) DebugPrint(level, args)
extern void DebugPrint(int level, const char *format, ...);
extern int debugVerbosity;
template <typename... T>
void DPRINTF(int level, const char *format, const T & ... args)
{
if (level <= debugVerbosity)
{
#ifdef _MSC_VER
if (IsDebuggerPresent())
{
OutputDebugStringA(fmt::sprintf(format, args...).c_str());
}
else
{
fmt::fprintf(std::cerr, format, args...);
}
#else
fmt::fprintf(std::cerr, format, args...);
#endif
#else
#if !defined(_DEBUG) && !defined(DEBUG)
#define DPRINTF //
#else
#define DPRINTF DebugPrint
extern void DebugPrint(int level, const char *format, ...);
#endif
#endif // __GNUC__
}
}
#endif /* DEBUG */
extern void SetDebugVerbosity(int);
extern int GetDebugVerbosity();