Last active
October 1, 2022 08:01
-
-
Save uemuraj/7189085879411f9f25e35138ede21b93 to your computer and use it in GitHub Desktop.
畳み込みを使った何か。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "trace.h" | |
#define WIN32_LEAN_AND_MEAN | |
#define NOMINMAX | |
#include <Windows.h> | |
#include <streambuf> | |
#include <system_error> | |
namespace trace | |
{ | |
struct mutex | |
{ | |
HANDLE m_handle; | |
mutex() : m_handle(::CreateMutexW(nullptr, false, nullptr)) | |
{ | |
if (!m_handle) | |
{ | |
throw std::system_error(::GetLastError(), std::system_category(), __FUNCTION__); | |
} | |
} | |
~mutex() noexcept | |
{ | |
::CloseHandle(m_handle); | |
} | |
void lock() | |
{ | |
if (::WaitForSingleObject(m_handle, INFINITE) == WAIT_FAILED) | |
{ | |
throw std::system_error(::GetLastError(), std::system_category(), __FUNCTION__); | |
} | |
} | |
void unlock() noexcept | |
{ | |
::ReleaseMutex(m_handle); | |
} | |
}; | |
class streambuf : public std::basic_streambuf<wchar_t> | |
{ | |
wchar_t * m_buf; | |
public: | |
streambuf() : m_buf(new wchar_t[2048]) | |
{ | |
this->setp(m_buf, m_buf + (2048 - 1)); | |
this->setg(m_buf, m_buf, m_buf + (2048 - 1)); | |
} | |
~streambuf() | |
{ | |
delete[] m_buf; | |
} | |
protected: | |
int sync() override | |
{ | |
wchar_t * p = this->pbase(); | |
wchar_t * q = this->pptr(); | |
*q = L'\0'; | |
::OutputDebugStringW(p); | |
this->pbump((int) (p - q)); | |
return 0; | |
} | |
int_type overflow(int_type ch) override | |
{ | |
this->sync(); | |
this->sputc(ch); | |
return false; | |
} | |
}; | |
static streambuf g_buf; | |
static std::wostream g_ostream(&g_buf); | |
static mutex g_mutex; | |
syncstream::syncstream(const wchar_t * header) : m_os(g_ostream) | |
{ | |
g_mutex.lock(); | |
m_os << header; | |
} | |
syncstream::~syncstream() | |
{ | |
m_os << std::endl << std::flush; | |
g_mutex.unlock(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define WIN32_LEAN_AND_MEAN | |
#define NOMINMAX | |
#include <Windows.h> | |
#include <ostream> | |
#define TRACE(...) if (::IsDebuggerPresent()) { trace::message(TEXT(__FILE__ "(" _CRT_STRINGIZE(__LINE__) "):"), __VA_ARGS__); } | |
#if !defined(__cpp_fold_expressions) | |
#error | |
#endif | |
namespace trace | |
{ | |
struct syncstream | |
{ | |
std::wostream & m_os; | |
syncstream(const wchar_t * header); | |
~syncstream(); | |
template <typename param_type> | |
syncstream & operator<<(param_type && param) | |
{ | |
m_os << L' ' << param; | |
return *this; | |
} | |
}; | |
template<typename... param_types> | |
void message(const wchar_t * header, param_types && ... params) | |
{ | |
(syncstream(header) << ... << params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment