/* Copyright 2006, Marcus Low of Malaysia. Use at your own risk, using all related sources in this project signifies that you agree that you will be using these sources without any warranty from the author for whatsoever event/damage/reason. Work provided here is as is. */ #include "MyTrace.h" //-------------------------------------------------------------------------// BOOL g_Debug = FALSE; BOOL g_DebugToFile = FALSE; ofstream g_DebugFileObj; //-------------------------------------------------------------------------// class InitMyTrace { public: InitMyTrace(); }; //-------------------------------------------------------------------------// InitMyTrace::InitMyTrace() { ifstream fin(DEBUG_FILE_FLAG); if (fin.fail() == FALSE) { g_Debug = TRUE; } else { return; } ifstream finlog(DEBUG_FILE_LOG); if (finlog.fail() == FALSE) { g_DebugToFile = TRUE; g_DebugFileObj.open(DEBUG_FILE_LOG, ios::app); } } //-------------------------------------------------------------------------// InitMyTrace g_InitMyTrace; //-------------------------------------------------------------------------// void MyTrace(LPCSTR lpszFormat, ...) { if (g_Debug == FALSE) { return; } DWORD dw = GetLastError(); va_list args; va_start(args, lpszFormat); int nBuf; char szBuffer[512]; if (_tcslen(lpszFormat) > sizeof(szBuffer)) { return; } nBuf = _vsnprintf_s(szBuffer, sizeof(szBuffer), sizeof(szBuffer)-1, lpszFormat, args); if (g_DebugToFile != FALSE) { SYSTEMTIME sysTime; ZeroMemory(&sysTime, sizeof(sysTime)); GetLocalTime(&sysTime); TCHAR TimePrint[256]; ZeroMemory(TimePrint, sizeof(TimePrint)); wsprintf(TimePrint, "%d:%d:%d, %02d:%02d:%d", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond); g_DebugFileObj << TimePrint << endl; g_DebugFileObj << szBuffer << endl; } OutputDebugStringA(szBuffer); va_end(args); SetLastError(dw); } //-------------------------------------------------------------------------// void LogDump (DWORD dw, PVOID start, ULONG offset) { ULONG i, j, m, n; PCHAR tmpstr; tmpstr = (PCHAR)start; if(start == 0 || start == (PVOID)0xFFFFFFFF) return; m = offset / 16; n = offset % 16; Log("Context : %X\n", dw); for(i = 0; i < m + (n == 0 ? 0 : 1); i++) { Log("%04X ", i * 16); for (j = 0; j < (i < m ? 16 : n); j ++) { Log("%02X ", tmpstr[(i * 16) + j] & 0x00FF); } for(; j < 16; j++) Log((".. ")); for (j = 0; j < (i < m ? 16 : n); j ++) { if(tmpstr[(i * 16) + j] >= 32) Log("%c", tmpstr[(i * 16) + j]); else Log("."); } for(; j < 16; j++) Log(".. "); Log("\n"); } Log("\n"); }