forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_Debug.Cpp
190 lines (150 loc) · 5.77 KB
/
NSWFL_Debug.Cpp
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_DEBUG_CPP_
#define _NSWFL_DEBUG_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#include <io.h>
#include <fcntl.h>
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace Debug {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FILE *hRedirectedOutputHandle = NULL;
FILE *hRedirectedErrorHandle = NULL;
FILE *hRedirectedInputHandle = NULL;
bool bIsConsoleOpen = false;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
AllocConsole();
RedirectIO(stdin, GetStdHandle(STD_INPUT_HANDLE));
RedirectIO(stdout, GetStdHandle(STD_OUTPUT_HANDLE));
RedirectIO(stderr, GetStdHandle(STD_OUTPUT_HANDLE));
*/
void RedirectIO(FILE *hFrom, HANDLE hTo)
{
int fd = _open_osfhandle((intptr_t)hTo, _O_WRONLY | _O_TEXT);
_dup2(fd, _fileno(hFrom));
setvbuf(hFrom, NULL, _IONBF, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DWORD WINAPI DebugConsoleThread(LPVOID pVoid)
{
_DEBUG_THREAD_DATA *data = (_DEBUG_THREAD_DATA *)pVoid;
DebugConsoleHandler handler = (DebugConsoleHandler)data->Handler;
SetEvent(data->hEvent);
char sConsolBuffer[5120];
while (true)
{
printf("\n>");
gets_s(sConsolBuffer, sizeof(sConsolBuffer));
int iLength = (int)strlen(sConsolBuffer);
if (iLength > 0)
{
if (!handler(sConsolBuffer, iLength))
{
break;
}
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DebugRedirectOutput(void)
{
freopen_s(&hRedirectedOutputHandle, "CONOUT$", "w", stdout);
freopen_s(&hRedirectedErrorHandle, "CONOUT$", "w", stderr);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DebugRedirectInput(void)
{
freopen_s(&hRedirectedInputHandle, "CONIN$", "r", stdin);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Open read/write console with a console input handler. If the handler returns false, the console is closed.
bool OpenDebugConsole(const char *sTitle, DebugConsoleHandler pHandler)
{
return OpenDebugConsole(sTitle, pHandler, NULL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Open read/write console with a console input handler. If the handler returns false, the console is closed.
bool OpenDebugConsole(const char *sTitle, DebugConsoleHandler pHandler, char *sInitialText)
{
bool bIsAreadyOpen = (GetConsoleWindow() != NULL);
if (bIsAreadyOpen || AllocConsole())
{
bIsConsoleOpen = true;
SetConsoleTitle(sTitle);
if (bIsAreadyOpen == false)
{
DebugRedirectOutput();
}
if (sInitialText)
{
printf("%s", sInitialText);
}
if (bIsAreadyOpen == false)
{
DebugRedirectInput();
}
if (pHandler)
{
StartDebugConsoleHandler(pHandler);
}
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Allows for interaction with the console.
void StartDebugConsoleHandler(DebugConsoleHandler pHandler)
{
_DEBUG_THREAD_DATA data;
memset(&data, 0, sizeof(data));
data.Handler = pHandler;
char sEventName[255];
sprintf_s(sEventName, sizeof(sEventName), "DCT_%d", GetTickCount());
data.hEvent = CreateEvent(NULL, FALSE, FALSE, sEventName);
CreateThread(0, NULL, DebugConsoleThread, &data, 0, NULL);
WaitForSingleObject(data.hEvent, INFINITE);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Open write-only console.
bool OpenDebugConsole(const char *sTitle)
{
return OpenDebugConsole(sTitle, NULL);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CloseDebugConsole(void)
{
if (hRedirectedOutputHandle != NULL)
{
fclose(hRedirectedOutputHandle);
}
if (hRedirectedErrorHandle != NULL)
{
fclose(hRedirectedErrorHandle);
}
if (hRedirectedInputHandle != NULL)
{
fclose(hRedirectedInputHandle);
}
if (bIsConsoleOpen)
{
FreeConsole();
bIsConsoleOpen = false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::Debug
} //namespace::NSWFL
#endif