# stdex: include/stdex/debug.hpp Source File

Loading...
Searching...
No Matches
debug.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2023-2024 Amebis
*/
#pragma once
#include "compat.hpp"
#include "string.hpp"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <chrono>
namespace stdex
{
namespace diag {
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
}
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 {
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
}
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
}
}
class benchmark
{
public:
benchmark(_In_z_ const char* task_name) :
m_task_name(task_name),
m_start(std::chrono::high_resolution_clock::now())
{}
~benchmark()
{
auto duration(std::chrono::high_resolution_clock::now() - m_start);
stdex::diag::printf("%s took %I64i ns\n", m_task_name, static_cast<int64_t>(duration.count()));
}
protected:
const char* m_task_name;
std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
};
}
stdex::benchmark
Measures time between initialization and going out of scope.
Definition debug.hpp:125
stdex::benchmark::benchmark
benchmark(const char *task_name)
Starts the measurement.
Definition debug.hpp:132
stdex::benchmark::~benchmark
~benchmark()
Stops the measurement and outputs the result to the diagnostic console.
Definition debug.hpp:140