Lib
QOLを高める
debug.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "util.h"
8 #include "exceptions.h"
9 
10 #define STR2WSTR0(s) L ## s
11 #define STR2WSTR(s) STR2WSTR0(s)
12 
18 #define ASSERT(x) ASSERT0(x, \
19  L"Assertion failed: " #x, \
20  STR2WSTR(__FUNCSIG__), __FILEW__, __LINE__)
21 
22 #define ASSERT0(x, msg, sig, file, line) do { \
23  if (!(x)) { \
24  yappy::debug::writeLine(msg); \
25  yappy::debug::writef(L"%s (%s: %d)", sig, file, line); \
26  ::DebugBreak(); \
27  } \
28 } while (0)
29 
30 namespace yappy {
32 namespace debug {
33 
36 bool enableDebugOutput() noexcept;
39 bool enableConsoleOutput() noexcept;
43 bool enableFileOutput(const wchar_t *fileName) noexcept;
46 void shutdownDebugOutput() noexcept;
47 
52 void write(const wchar_t *str, bool newline = false) noexcept;
57 inline void write(const char *str, bool newline = false) noexcept
58 {
59  write(util::utf82wc(str).get(), newline);
60 }
64 inline void writeLine(const wchar_t *str = L"") noexcept
65 {
66  write(str, true);
67 }
71 inline void writeLine(const char *str) noexcept
72 {
73  write(util::utf82wc(str).get(), true);
74 }
79 void writef(const wchar_t *fmt, ...) noexcept;
84 void writef(const char *fmt, ...) noexcept;
89 void writef_nonl(const wchar_t *fmt, ...) noexcept;
94 void writef_nonl(const char *fmt, ...) noexcept;
95 
96 
100 class StopWatch : private util::noncopyable {
101 public:
105  explicit StopWatch(const wchar_t *msg) : m_msg(msg)
106  {
107  BOOL b = ::QueryPerformanceCounter(&m_begin);
108  error::checkWin32Result(b != 0, "QueryPerformanceCounter() failed");
109  }
114  {
115  BOOL b;
116  LARGE_INTEGER end, freq;
117  b = ::QueryPerformanceCounter(&end);
118  error::checkWin32Result(b != 0, "QueryPerformanceCounter() failed");
119  b = ::QueryPerformanceFrequency(&freq);
120  error::checkWin32Result(b != 0, "QueryPerformanceFrequency() failed");
121  double sec = static_cast<double>(end.QuadPart - m_begin.QuadPart) / freq.QuadPart;
122  writef(L"%s: %.3f us", m_msg, sec * 1e6);
123  }
124 private:
125  const wchar_t *m_msg;
126  LARGE_INTEGER m_begin;
127 };
128 
129 } // namespace debug
130 
132 namespace trace {
133 
137 void initialize(size_t bufsize = 1024 * 1024);
140 void output();
141 
146 void write(const char *str);
147 
148 } // namespace trace
149 } // namespace yappy
bool enableDebugOutput() noexcept
Enables writing to OutputDebugString().
Definition: debug.cpp:19
Noncopyable class.
Definition: util.h:24
void write(const wchar_t *str, bool newline) noexcept
Write debug string.
Definition: debug.cpp:73
Stop watch utility for performance measurement.
Definition: debug.h:100
const char * str
Definition: input.cpp:197
void writef(const wchar_t *fmt,...) noexcept
Write debug message using format string like printf.
Definition: debug.cpp:103
~StopWatch()
Destructor. Stop the timer.
Definition: debug.h:113
void initialize(size_t bufsize)
Initialize trace buffer.
Definition: debug.cpp:179
void shutdownDebugOutput() noexcept
Flush buffers and free resources.
Definition: debug.cpp:57
StopWatch(const wchar_t *msg)
Constructor. Starts the timer.
Definition: debug.h:105
Definition: config.cpp:6
void writeLine(const wchar_t *str=L"") noexcept
Write debug string and new line.
Definition: debug.h:64
void output()
Output to a temporary file and open it with notepad.
Definition: debug.cpp:190
Utilities.
char msg[LINE_DATA_SIZE-sizeof(LARGE_INTEGER)-sizeof(uint32_t)]
Definition: debug.cpp:159
bool enableFileOutput(const wchar_t *fileName) noexcept
Enables writing to a file.
Definition: debug.cpp:43
std::unique_ptr< wchar_t[]> utf82wc(const char *in)
UTF-8 to wide char.
Definition: util.h:117
bool enableConsoleOutput() noexcept
Shows a console window and enables writing to it.
Definition: debug.cpp:25
void checkWin32Result(bool cond, const std::string &msg)
Definition: exceptions.h:50
void writef_nonl(const wchar_t *fmt,...) noexcept
Write debug message using format string like printf. (No new line)
Definition: debug.cpp:125