project64/Source/nragev20/Debug.cpp

158 lines
3.9 KiB
C++
Raw Normal View History

2020-02-09 11:36:49 +00:00
/*
N-Rage`s Dinput8 Plugin
(C) 2002, 2006 Norbert Wladyka
2016-01-27 09:11:59 +00:00
Author`s Email: norbert.wladyka@chello.at
Website: http://go.to/nrage
2016-01-27 09:11:59 +00:00
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.
2016-01-27 09:11:59 +00:00
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.
2016-01-27 09:11:59 +00:00
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
2016-01-27 09:11:59 +00:00
*/
#include <windows.h>
#include "commonIncludes.h"
2016-01-27 09:11:59 +00:00
#include "FileAccess.h"
bool bDebug = true;
HANDLE hDebug = INVALID_HANDLE_VALUE;
void _DebugAnsiFileWrite( LPCSTR szRemark )
{
2020-02-09 11:36:49 +00:00
if( !bDebug )
return;
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
if (hDebug == INVALID_HANDLE_VALUE)
{
TCHAR szFile[] = _T("NRage-Debug.txt");
TCHAR szBuffer[MAX_PATH+1];
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
GetAbsoluteFileName( szBuffer, szFile, DIRECTORY_LOG );
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);
}
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
if( hDebug != INVALID_HANDLE_VALUE )
{
DWORD dwWritten;
LPCSTR szText = szRemark;
if( szText == NULL )
szText = "\r\n";
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
LPCSTR szCurrPos = szText;
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
while( ( szCurrPos = strchr( szCurrPos, '\n' )) != NULL )
{
DWORD length = szCurrPos - szText;
if( length > 0 && szCurrPos[-1] == '\r' )
--length;
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
if( length > 0 )
WriteFile( hDebug, (LPCVOID)szText, length, &dwWritten, NULL );
WriteFile( hDebug, "\r\n", 2, &dwWritten, NULL );
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
szText = ++szCurrPos;
}
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
DWORD length = lstrlenA( szText );
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
if( length > 0 )
2016-01-27 09:11:59 +00:00
WriteFile( hDebug, (LPCVOID)szText, length, &dwWritten, NULL );
2020-02-09 11:36:49 +00:00
}
return;
2016-01-27 09:11:59 +00:00
}
void _cdecl _DebugWriteA( LPCSTR szFormat, ... )
{
2020-02-09 11:36:49 +00:00
if( szFormat != NULL )
{
char szBuffer[4096];
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
va_list val;
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
va_start( val,szFormat );
wvsprintfA( szBuffer, szFormat, val );
va_end( val );
szBuffer[sizeof(szBuffer)-1] = '\0';
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
_DebugAnsiFileWrite( szBuffer );
}
else
2016-01-27 09:11:59 +00:00
_DebugAnsiFileWrite( NULL );
}
void _cdecl _DebugWriteW( const LPCWSTR szFormat, ... )
{
2020-02-09 11:36:49 +00:00
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 );
2016-01-27 09:11:59 +00:00
}
void _WriteDatasA( LPCSTR Header, const int Control, const unsigned char * Data, const HRESULT hr )
{
2020-02-09 11:36:49 +00:00
if( !bDebug || Data == NULL )
return;
_DebugWriteA( "%d:%s\n", Control, Header);
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
if( hr )
{
_DebugWriteA( "Failed! ErrorCode: %08X\n", hr );
}
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
int iEnd = Data[0] + Data[1] + 2;
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
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] );
}
2016-01-27 09:11:59 +00:00
2020-02-09 11:36:49 +00:00
return;
2016-01-27 09:11:59 +00:00
}
void _CloseDebugFile()
{
2020-02-09 11:36:49 +00:00
if( hDebug != INVALID_HANDLE_VALUE )
{
_DebugWriteA("---DEBUG FILE CLOSED---\n");
CloseHandle( hDebug );
}
2016-01-27 09:11:59 +00:00
}
void _DebugFlush()
{
2020-02-09 11:36:49 +00:00
if( hDebug != INVALID_HANDLE_VALUE )
{
FlushFileBuffers(hDebug);
}
2016-01-27 09:11:59 +00:00
}