debug: add

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-12-19 12:45:27 +01:00
parent 088dd86d63
commit c212e68bb5

118
include/stdex/debug.hpp Normal file
View File

@ -0,0 +1,118 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include "string.hpp"
#include <stdarg.h>
#include <stdio.h>
namespace stdex
{
namespace diag {
/// \cond internal
inline void vprintf(_In_z_ _Printf_format_string_ const char* format, _In_ va_list arg)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
UNREFERENCED_PARAMETER(arg);
#elif defined(_WIN32)
auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
OutputDebugStringA(tmp.c_str());
#else
vfprintf(stdout, format, arg);
#endif
}
inline void vprintf(_In_z_ _Printf_format_string_ const wchar_t* format, _In_ va_list arg)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
UNREFERENCED_PARAMETER(arg);
#elif defined(_WIN32)
auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
OutputDebugStringW(tmp.c_str());
#else
vfwprintf(stdout, format, arg);
#endif
}
/// \endcond
///
/// Outputs diagnostic message
///
/// On Windows, the message is sent using `OutputDebugStringA`. On other platforms, message is printed to stdout.
///
/// \note When compiled with #define NDEBUG, no output is performed.
///
/// \param[in] format String template using `printf()` style
///
template <class T>
inline void printf(_In_z_ _Printf_format_string_ const T* format, ...)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
#else
va_list arg;
va_start(arg, format);
vprintf(format, arg);
va_end(arg);
#endif
}
}
namespace err {
/// \cond internal
inline void vprintf(_In_z_ _Printf_format_string_ const char* format, _In_ va_list arg)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
UNREFERENCED_PARAMETER(arg);
#elif defined(_WIN32)
auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
OutputDebugStringA(tmp.c_str());
#else
vfprintf(stderr, format, arg);
#endif
}
inline void vprintf(_In_z_ _Printf_format_string_ const wchar_t* format, _In_ va_list arg)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
UNREFERENCED_PARAMETER(arg);
#elif defined(_WIN32)
auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
OutputDebugStringW(tmp.c_str());
#else
vfwprintf(stderr, format, arg);
#endif
}
/// \endcond
///
/// Outputs error message
///
/// On Windows, the message is sent using `OutputDebugStringA`. On other platforms, message is printed to stderr.
///
/// \note When compiled with #define NDEBUG, no output is performed.
///
/// \param[in] format String template using `printf()` style
///
template <class T>
inline void printf(_In_z_ _Printf_format_string_ const T* format, ...)
{
#if defined(NDEBUG)
UNREFERENCED_PARAMETER(format);
#else
va_list arg;
va_start(arg, format);
vprintf(format, arg);
va_end(arg);
#endif
}
}
}