-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDebug.cpp
157 lines (121 loc) · 3.54 KB
/
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
/*
N-Rage`s Dinput8 Plugin
(C) 2002, 2006 Norbert Wladyka
Author`s Email: [email protected]
Website: http://go.to/nrage
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "commonIncludes.h"
#include <windows.h>
#include "FileAccess.h"
bool bDebug = true;
HANDLE hDebug = INVALID_HANDLE_VALUE;
void _DebugAnsiFileWrite( LPCSTR szRemark )
{
if( !bDebug )
return;
if (hDebug == INVALID_HANDLE_VALUE)
{
TCHAR szFile[] = _T("NRage-Debug.txt");
TCHAR szBuffer[MAX_PATH+1];
GetAbsoluteFileName( szBuffer, szFile, DIRECTORY_APPLICATION );
hDebug = CreateFile( szBuffer, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hDebug != INVALID_HANDLE_VALUE)
SetFilePointer(hDebug, 0, 0, FILE_END);
}
if( hDebug != INVALID_HANDLE_VALUE )
{
DWORD dwWritten;
LPCSTR szText = szRemark;
if( szText == NULL )
szText = "\r\n";
LPCSTR szCurrPos = szText;
while( ( szCurrPos = strchr( szCurrPos, '\n' )) != NULL )
{
DWORD length = szCurrPos - szText;
if( length > 0 && szCurrPos[-1] == '\r' )
--length;
if( length > 0 )
WriteFile( hDebug, (LPCVOID)szText, length, &dwWritten, NULL );
WriteFile( hDebug, "\r\n", 2, &dwWritten, NULL );
szText = ++szCurrPos;
}
DWORD length = lstrlenA( szText );
if( length > 0 )
WriteFile( hDebug, (LPCVOID)szText, length, &dwWritten, NULL );
}
return;
}
void _cdecl _DebugWriteA( LPCSTR szFormat, ... )
{
if( szFormat != NULL )
{
char szBuffer[4096];
va_list val;
va_start( val,szFormat );
wvsprintfA( szBuffer, szFormat, val );
va_end( val );
szBuffer[sizeof(szBuffer)-1] = '\0';
_DebugAnsiFileWrite( szBuffer );
}
else
_DebugAnsiFileWrite( NULL );
}
void _cdecl _DebugWriteW( const LPCWSTR szFormat, ... )
{
if( szFormat != NULL )
{
WCHAR szBuffer[4096];
va_list val;
va_start( val,szFormat );
wvsprintfW( szBuffer, szFormat, val );
va_end( val );
szBuffer[(sizeof(szBuffer) / sizeof(WCHAR))-1] = L'\0';
char szAnsi[sizeof(szBuffer) / sizeof(WCHAR)];
WideCharToMultiByte( CP_ACP, 0, szBuffer, -1, szAnsi, sizeof(szAnsi), NULL, NULL );
_DebugAnsiFileWrite( szAnsi );
}
else
_DebugAnsiFileWrite( NULL );
}
void _WriteDatasA( LPCSTR Header, const int Control, LPCBYTE Data, const HRESULT hr )
{
if( !bDebug || Data == NULL )
return;
_DebugWriteA( "%d:%s\n", Control, Header);
if( hr )
{
_DebugWriteA( "Failed! ErrorCode: %08X\n", hr );
}
int iEnd = Data[0] + Data[1] + 2;
for( int i = -1; i < iEnd; i += 8 )
{
_DebugWriteA( "%02X%02X%02X%02X %02X%02X%02X%02X\n", Data[i+0], Data[i+1], Data[i+2], Data[i+3], Data[i+4], Data[i+5], Data[i+6], Data[i+7] );
}
return;
}
void _CloseDebugFile()
{
if( hDebug != INVALID_HANDLE_VALUE )
{
_DebugWriteA("---DEBUG FILE CLOSED---\n");
CloseHandle( hDebug );
}
}
void _DebugFlush()
{
if( hDebug != INVALID_HANDLE_VALUE )
{
FlushFileBuffers(hDebug);
}
}