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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// https://github.com/Const-me/vis_avs_dx/blob/master/avs_dx/DxVisuals/Interop/ConsoleLogger.cpp
#include "stdafx.h"
#include "DebugConsole.h"
#include "miscUtils.h"
#include "../AppState.h"
#include "logger.h"
namespace
{
using Whisper::eLogLevel;
constexpr uint16_t defaultAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
inline uint16_t textAttributes( eLogLevel lvl )
{
switch( lvl )
{
case eLogLevel::Error:
return FOREGROUND_RED | FOREGROUND_INTENSITY;
case eLogLevel::Warning:
return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
case eLogLevel::Info:
return FOREGROUND_GREEN | FOREGROUND_INTENSITY;
case eLogLevel::Debug:
return FOREGROUND_BLUE | FOREGROUND_INTENSITY;
}
return defaultAttributes;
}
// Background stuff: accumulate messages in a small buffer, in case user will want to see them in the console.
// Ideally, we should accumulate them in a more efficient data structure, maybe a circular buffer.
// However, we don't have that many messages per second, this simple solution that uses std::deque is probably good enough for the job.
static constexpr uint16_t bufferSize = 64;
using Lock = CComCritSecLock<CComAutoCriticalSection>;
#define LOCK() Lock __lock{ critSec }
thread_local CStringA threadError;
}
HRESULT DebugConsole::Entry::print( HANDLE hConsole, CString& tempString ) const
{
if( !SetConsoleTextAttribute( hConsole, textAttributes( level ) ) )
return getLastHr();
makeUtf16( tempString, message );
tempString += L"\r\n";
if( !WriteConsoleW( hConsole, tempString, (DWORD)tempString.GetLength(), nullptr, nullptr ) )
return getLastHr();
return S_OK;
}
void clearLastError()
{
threadError = "";
}
bool getLastError( CString& rdi )
{
if( threadError.GetLength() <= 0 )
{
rdi = L"";
return false;
}
else
{
makeUtf16( rdi, threadError );
threadError = "";
return true;
}
}
inline void DebugConsole::logSink( eLogLevel lvl, const char* message )
{
LOCK();
// Add to the buffer
while( buffer.size() >= bufferSize )
buffer.pop_front();
buffer.emplace_back( Entry{ lvl, message } );
// If the console window is shown, print there, too.
if( output )
buffer.rbegin()->print( output, tempString );
}
void __stdcall DebugConsole::logSinkStatic( void* context, eLogLevel lvl, const char* message )
{
if( lvl == eLogLevel::Error )
threadError = message;
DebugConsole* con = (DebugConsole*)context;
con->logSink( lvl, message );
}
HRESULT DebugConsole::initialize( Whisper::eLogLevel level )
{
if( nullptr != pGlobalInstance )
return HRESULT_FROM_WIN32( ERROR_ALREADY_INITIALIZED );
pGlobalInstance = this;
Whisper::sLoggerSetup setup;
setup.sink = &logSinkStatic;
setup.context = this;
setup.level = level;
setup.flags = Whisper::eLoggerFlags::SkipFormatMessage;
return Whisper::setupLogger( setup );
}
DebugConsole::~DebugConsole()
{
hide();
Whisper::sLoggerSetup setup;
memset( &setup, 0, sizeof( setup ) );
Whisper::setupLogger( setup );
pGlobalInstance = nullptr;
}
DebugConsole* DebugConsole::pGlobalInstance = nullptr;
void DebugConsole::windowClosed()
{
LOCK();
if( FreeConsole() )
{
// Apparently, FreeConsole already closes that handle: https://stackoverflow.com/q/12676312/126995
output.Detach();
}
output.Close();
for( CButton* b : checkboxes )
{
if( !*b )
continue;
if( !b->IsWindow() )
continue;
PostMessage( *b, BM_SETCHECK, BST_UNCHECKED, 0 );
}
}
BOOL __stdcall DebugConsole::consoleHandlerRoutine( DWORD dwCtrlType )
{
switch( dwCtrlType )
{
case CTRL_CLOSE_EVENT:
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
pGlobalInstance->windowClosed();
return TRUE;
}
return TRUE;
}
HRESULT DebugConsole::show()
{
HWND hWnd = GetConsoleWindow();
if( nullptr != hWnd )
{
ShowWindow( hWnd, SW_RESTORE );
SetForegroundWindow( hWnd );
return S_FALSE;
}
if( !AllocConsole() )
return getLastHr();
output.Close();
output.Attach( GetStdHandle( STD_OUTPUT_HANDLE ) );
if( !output )
return getLastHr();
constexpr UINT cp = CP_UTF8;
if( IsValidCodePage( cp ) )
SetConsoleOutputCP( cp );
// Enable ANSI color coding
DWORD mode = 0;
if( !GetConsoleMode( output, &mode ) )
return getLastHr();
if( 0 == ( mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING ) )
{
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if( !SetConsoleMode( output, mode ) )
return getLastHr();
}
SetConsoleTitle( L"Whisper Desktop Debug Console" );
SetConsoleCtrlHandler( &consoleHandlerRoutine, TRUE );
// Disable close command in the sys.menu of the new console, otherwise the whole process will quit: https://stackoverflow.com/a/12015131/126995
HWND hwnd = ::GetConsoleWindow();
if( hwnd != nullptr )
{
HMENU hMenu = ::GetSystemMenu( hwnd, FALSE );
if( hMenu != NULL )
DeleteMenu( hMenu, SC_CLOSE, MF_BYCOMMAND );
}
// Print old log entries
for( const auto& e : buffer )
CHECK( e.print( output, tempString ) );
const CStringA msg = "Press Control+C or Control+Break to close this window\r\n";
if( !SetConsoleTextAttribute( output, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY ) )
return getLastHr();
if( !WriteConsoleA( output, cstr( msg ), msg.GetLength(), nullptr, nullptr ) )
return getLastHr();
return S_OK;
}
HRESULT DebugConsole::hide()
{
LOCK();
if( !output )
return S_FALSE;
windowClosed();
return S_OK;
}
void DebugConsole::addCheckbox( CButton& cb )
{
checkboxes.emplace( &cb );
}
void DebugConsole::removeCheckbox( CButton& cb )
{
checkboxes.erase( &cb );
}
HRESULT ConsoleCheckbox::initialize( HWND dialog, int idc, AppState& state )
{
control = GetDlgItem( dialog, idc );
assert( control );
console = &state.console;
if( state.console.isVisible() )
control.SetCheck( BST_CHECKED );
state.console.addCheckbox( control );
return S_OK;
}
void ConsoleCheckbox::click()
{
const int state = control.GetCheck();
if( state == BST_CHECKED )
console->show();
else
console->hide();
}
void ConsoleCheckbox::ensureChecked()
{
const int state = control.GetCheck();
if( state == BST_CHECKED )
return;
control.SetCheck( BST_CHECKED );
console->show();
}
void DebugConsole::log( eLogLevel lvl, const char* pszFormat, va_list args )
{
LOCK();
// Add to the buffer
while( buffer.size() >= bufferSize )
buffer.pop_front();
tempStringA.FormatV( pszFormat, args );
buffer.emplace_back( Entry{ lvl, tempStringA } );
// If the console window is shown, print there, too.
if( output )
buffer.rbegin()->print( output, tempString );
}
void DebugConsole::logMessage( eLogLevel lvl, const char* pszFormat, va_list args )
{
if( nullptr == pGlobalInstance )
return;
pGlobalInstance->log( lvl, pszFormat, args );
}
void logMessage( Whisper::eLogLevel lvl, const char8_t* pczFormat, va_list args )
{
DebugConsole::logMessage( lvl, (const char*)pczFormat, args );
}
|