forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleOut.hpp
157 lines (121 loc) · 3.73 KB
/
ConsoleOut.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
//---------------------------------------------------------------------------//
//
// ConsoleOut.hpp
// デバッグウィンドウへの出力関数
// Copyright (C) 2014-2015 tapetums
//
//---------------------------------------------------------------------------//
#ifndef CONSOLE_OUT
#define console_outA(x, ...)
#define console_outW(x, ...)
#define console_out(x, ...)
#else
inline void console_outA(const char* format, ...);
inline void console_outW(const wchar_t* format, ...);
#if defined(_UNICODE) || defined(UNICODE)
#define console_out console_outW
#else
#define console_out console_outA
#endif
#endif
//---------------------------------------------------------------------------//
#ifdef CONSOLE_OUT
#include <array>
#include <windows.h>
#include <strsafe.h>
//---------------------------------------------------------------------------//
// 定数
//---------------------------------------------------------------------------//
constexpr size_t TIME_BUFSIZE = 32;
constexpr size_t CONSOLE_BUFSIZE = 1024;
//---------------------------------------------------------------------------//
// 排他ロック
//---------------------------------------------------------------------------//
#include "Lock.hpp"
struct lock
{
static tapetums::CsLock::Lock& get()
{
static tapetums::CsLock::Lock g_lock;
return g_lock;
}
};
//---------------------------------------------------------------------------//
// 関数
//---------------------------------------------------------------------------//
inline void console_outA(const char* format, ...)
{
if ( nullptr == format )
{
return;
}
thread_local std::array<char, TIME_BUFSIZE> time;
thread_local std::array<char, CONSOLE_BUFSIZE> buff;
thread_local SYSTEMTIME st;
thread_local DWORD threadId = ::GetCurrentThreadId();
// 現在時刻を取得
::GetLocalTime(&st);
// 時刻を文字列に
::StringCchPrintfA
(
time.data(), time.size(),
"[%08x] %02u:%02u:%02u;%03u> ", threadId,
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
);
// 引数を文字列に
va_list al;
va_start(al, format);
{
::StringCchVPrintfA(buff.data(), CONSOLE_BUFSIZE, format, al);
}
va_end(al);
// 排他制御
{
tapetums::CsLock::CS cs(lock::get());
cs.enter();
::OutputDebugStringA(time.data());
::OutputDebugStringA(buff.data());
::OutputDebugStringA("\n");
}
}
//---------------------------------------------------------------------------//
inline void console_outW(const wchar_t* format, ...)
{
if ( nullptr == format )
{
return;
}
thread_local std::array<wchar_t, TIME_BUFSIZE> time;
thread_local std::array<wchar_t, CONSOLE_BUFSIZE> buff;
thread_local SYSTEMTIME st;
thread_local DWORD threadId = ::GetCurrentThreadId();
// 現在時刻を取得
::GetLocalTime(&st);
// 時刻を文字列に
::StringCchPrintfW
(
time.data(), time.size(),
L"[%08x] %02u:%02u:%02u;%03u> ", threadId,
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds
);
// 引数を文字列に
va_list al;
va_start(al, format);
{
::StringCchVPrintfW(buff.data(), CONSOLE_BUFSIZE, format, al);
}
va_end(al);
// 排他制御
{
tapetums::CsLock::CS cs(lock::get());
cs.enter();
::OutputDebugStringW(time.data());
::OutputDebugStringW(buff.data());
::OutputDebugStringW(L"\n");
}
}
//---------------------------------------------------------------------------//
#endif
//---------------------------------------------------------------------------//
// ConsoleOut.hpp