stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
debug.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "string.hpp"
10#include <stdarg.h>
11#include <stdint.h>
12#include <stdio.h>
13#include <chrono>
14
15 namespace stdex
16{
17 namespace diag {
19 inline void vprintf(_In_z_ _Printf_format_string_ const char* format, _In_ va_list arg)
20 {
21#if defined(NDEBUG)
22 _Unreferenced_(format);
23 _Unreferenced_(arg);
24#elif defined(_WIN32)
25 auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
26 OutputDebugStringA(tmp.c_str());
27#else
28 vfprintf(stdout, format, arg);
29#endif
30 }
31
32 inline void vprintf(_In_z_ _Printf_format_string_ const wchar_t* format, _In_ va_list arg)
33 {
34#if defined(NDEBUG)
35 _Unreferenced_(format);
36 _Unreferenced_(arg);
37#elif defined(_WIN32)
38 auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
39 OutputDebugStringW(tmp.c_str());
40#else
41 vfwprintf(stdout, format, arg);
42#endif
43 }
45
55 template <class T>
56 inline void printf(_In_z_ _Printf_format_string_ const T* format, ...)
57 {
58#if defined(NDEBUG)
59 _Unreferenced_(format);
60#else
61 va_list arg;
62 va_start(arg, format);
63 vprintf(format, arg);
64 va_end(arg);
65#endif
66 }
67 }
68
69 namespace err {
71 inline void vprintf(_In_z_ _Printf_format_string_ const char* format, _In_ va_list arg)
72 {
73#if defined(NDEBUG)
74 _Unreferenced_(format);
75 _Unreferenced_(arg);
76#elif defined(_WIN32)
77 auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
78 OutputDebugStringA(tmp.c_str());
79#else
80 vfprintf(stderr, format, arg);
81#endif
82 }
83
84 inline void vprintf(_In_z_ _Printf_format_string_ const wchar_t* format, _In_ va_list arg)
85 {
86#if defined(NDEBUG)
87 _Unreferenced_(format);
88 _Unreferenced_(arg);
89#elif defined(_WIN32)
90 auto tmp = stdex::vsprintf(format, stdex::locale_default, arg);
91 OutputDebugStringW(tmp.c_str());
92#else
93 vfwprintf(stderr, format, arg);
94#endif
95 }
97
107 template <class T>
108 inline void printf(_In_z_ _Printf_format_string_ const T* format, ...)
109 {
110#if defined(NDEBUG)
111 _Unreferenced_(format);
112#else
113 va_list arg;
114 va_start(arg, format);
115 vprintf(format, arg);
116 va_end(arg);
117#endif
118 }
119 }
120
124 class benchmark
125 {
126 public:
132 benchmark(_In_z_ const char* task_name) :
133 m_task_name(task_name),
134 m_start(std::chrono::high_resolution_clock::now())
135 {}
136
140 ~benchmark()
141 {
142 auto duration(std::chrono::high_resolution_clock::now() - m_start);
143 stdex::diag::printf("%s took %I64i ns\n", m_task_name, static_cast<int64_t>(duration.count()));
144 }
145
146 protected:
147 const char* m_task_name;
148 std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
149 };
150}
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