OpenBarnyard
 
Loading...
Searching...
No Matches
TDebugConsole_Win.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TDebugConsole_Win.h"
3#include "Toshi/TString8.h"
4
5#include <conio.h>
6#include <windows.h>
7
8//-----------------------------------------------------------------------------
9// Enables memory debugging.
10// Note: Should be the last include!
11//-----------------------------------------------------------------------------
12#include "Core/TMemoryDebugOn.h"
13
14static constexpr const WORD kTypeColours[]{
15 FOREGROUND_GREEN,
16 FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
17 FOREGROUND_INTENSITY | FOREGROUND_RED,
18 BACKGROUND_RED | FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN
19};
20
21TSTATICASSERT( Toshi::TUtil::LogType_NUMOF == TARRAYSIZE( kTypeColours ) );
22
24
26{
27 m_HWND = FindConsole();
28 m_Created = m_HWND != TNULL;
29
30 m_LogListener.Connect(
32 this,
33 TDebugConsole::OnLog,
34 1
35 );
36}
37
39{
40 if ( IsCreated() )
41 {
42 if ( IsVisible() )
43 {
44 TWARN( "Press any key to exit...\n" );
45 _getch();
46 }
47 }
48}
49
51{
52 TBOOL oldState = m_Visible;
53 m_Visible = state;
54
55 ShowWindow( m_HWND, state );
56 UpdateWindow( m_HWND );
57
58 return oldState;
59}
60
62{
63 Show( !m_Visible );
64 return m_Visible;
65}
66
68{
70
71 // Generate title for the console so we can find it's HWND
72 LARGE_INTEGER time;
73 QueryPerformanceCounter( &time );
74 TCHAR timeHex[ sizeof( time.QuadPart ) + 1 ] = { 0 };
75 _ultoa_s( (unsigned long)time.QuadPart, timeHex, 0x10 );
76 str += timeHex;
77
78 // Save the current title
79 TCHAR lpConsoleTitle[ 256 ];
80 DWORD titleLength = GetConsoleTitleA( lpConsoleTitle, 256 );
81 if ( titleLength == 0 ) { return TNULL; }
82
83 // Set the genereated title and wait for 50ms so it's applied
84 SetConsoleTitleA( str );
85 ThreadSleep( 50 );
86
87 // Find a window with the generated title and restore it
88 HWND consoleHWND = FindWindowA( TNULL, str );
89 SetConsoleTitleA( lpConsoleTitle );
90
91 return consoleHWND;
92}
93
94static TBOOL s_bIsSimpleMode = TFALSE;
95
96TBOOL __stdcall TDebugConsole::OnLog( TDebugConsole* a_pCaller, TUtil* a_pOwner, TUtil::LogEvent* pLogEvent )
97{
98 TASSERT( pLogEvent );
99
100 HANDLE hStd;
101 WORD wOldColorAttrs;
102
103 hStd = GetStdHandle( STD_OUTPUT_HANDLE );
104
105 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
106 GetConsoleScreenBufferInfo( hStd, &csbiInfo );
107 wOldColorAttrs = csbiInfo.wAttributes;
108
109 SetConsoleTextAttribute( hStd, kTypeColours[ pLogEvent->m_eType ] );
110
111 if ( s_bIsSimpleMode )
112 {
114 "[%s]: %s",
115 TUtil::LogTypeToString( pLogEvent->m_eType ),
116 pLogEvent->m_szString
117 );
118 }
119 else
120 {
121 auto& appParams = TUtil::GetToshiParameters();
122
123 TCHAR strTime[ 9 ];
124 _strtime( strTime );
125
127 "[%s] [%s/%s] [%s]: %s",
128 strTime,
129 appParams.szLogAppName,
130 appParams.szLogAppDirName != TNULL ? appParams.szLogAppDirName : "",
131 TUtil::LogTypeToString( pLogEvent->m_eType ),
132 pLogEvent->m_szString
133 );
134 }
135
136 SetConsoleTextAttribute( hStd, wOldColorAttrs );
137
138 return true;
139}
140
TOSHI_NAMESPACE_START constexpr const TCHAR * TDebugConsoleUUID
void TDebug_FinalPrintf(const TCHAR *a_szFormat,...)
#define TASSERT(X,...)
Definition Defines.h:138
#define TWARN(...)
Definition Defines.h:152
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
#define TSTATICASSERT(...)
Definition Defines.h:67
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
void ThreadSleep(TUINT dwMilliseconds)
Char string implementation for the Toshi engine.
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
bool TBOOL
Definition Typedefs.h:6
TBOOL Show(TBOOL state)
TBOOL IsVisible() const
TBOOL IsCreated() const
HWND FindConsole() const
Definition TUtil.h:14
static constexpr const TCHAR * LogTypeToString(LogType a_eType)
Definition TUtil.h:36
static const TOSHIParams & GetToshiParameters()
Definition TUtil.h:129
static TEmitter< TUtil, TUtil::LogEvent > & GetLogEmitter()
Definition TUtil.h:117
const TCHAR * m_szString
Definition TUtil.h:53
LogType m_eType
Definition TUtil.h:52