stdex/include/stdex/debug.hpp
Simon Rozman 38c6b40b21 Bump Copyright year
Signed-off-by: Simon Rozman <simon@rozman.si>
2024-01-06 10:55:09 +01:00

119 lines
2.8 KiB
C++

/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 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
}
}
}