From c212e68bb5f2480c68cb4b47a7b9e2ff94b840d8 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 19 Dec 2023 12:45:27 +0100 Subject: [PATCH] debug: add Signed-off-by: Simon Rozman --- include/stdex/debug.hpp | 118 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 include/stdex/debug.hpp diff --git a/include/stdex/debug.hpp b/include/stdex/debug.hpp new file mode 100644 index 000000000..8667b21dc --- /dev/null +++ b/include/stdex/debug.hpp @@ -0,0 +1,118 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023 Amebis +*/ + +#pragma once + +#include "compat.hpp" +#include "string.hpp" +#include +#include + +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 + 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 + 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 + } + } +}