// stdafx.cpp : source file that includes just the standard includes // Ooganizer-Hook.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" #include "stdio.h" #include "stdlib.h" ////////////////////////////////////////////////////////////////////// // Func: SetLoggingDetail() ////////////////////////////////////////////////////////////////////// void SetLoggingDetail(LoggingDetail detail, wchar_t* logPath) { #ifdef _DEBUG g_LoggingDetail = LOGGING_DEBUG; #else g_LoggingDetail = detail; #endif // If the log path is set externally set it here internally // else set the log path to %USERPROFILE%\AppData\Local\Temp if(logPath && logPath[0] != 0) { lstrcpyn(g_LogPath, logPath, (MAX_PATH - lstrlen(g_LOG_FILE_NAME))); } else { size_t len = 0; errno_t err = 0; wchar_t* pValue = NULL; if(err = _wdupenv_s( &pValue, &len, L"TMP")) if(err = _wdupenv_s( &pValue, &len, L"TEMP")) return; if(pValue != NULL) lstrcpyn(g_LogPath, pValue, (MAX_PATH - lstrlen(g_LOG_FILE_NAME))); } // Make sure Path ends with '\' if(g_LogPath[lstrlen(g_LogPath) - 1] != L'\\') lstrcat(g_LogPath, L"\\"); // Add the Log File name to the path variable (now we have everything) // ~we are ready to do some serious logging lstrcat(g_LogPath,g_LOG_FILE_NAME); } ////////////////////////////////////////////////////////////////////// // Func: GetLoggingDetail() ////////////////////////////////////////////////////////////////////// _LoggingDetail GetLoggingDetail() { return g_LoggingDetail; } ////////////////////////////////////////////////////////////////////// // Func: log() // Desc: Logs to Ooganizer.log in defined LogPath or temporary folder // Prms: Variable # of params // // Usge: Log("%s %d %d %d", "test", 1, 2, 3) ////////////////////////////////////////////////////////////////////// //void log( LoggingDetail detail, char * pFmt, ... ) //{ // if(g_LoggingDetail >= detail) // { // FILE* pFile = NULL; // errno_t err = 0; // // if ( !( err = fopen_s( &pFile, g_LogPath, "at" ) ) ) // { // fprintf( pFile, "ThreadId %i -", GetCurrentThreadId()); // va_list arg; // va_start( arg, pFmt ); // vfprintf( pFile, pFmt, arg ); // va_end( arg ); // // if ( pFmt[strlen( pFmt ) - 1] != '\n' ) // fprintf( pFile, "\n" ); // fclose( pFile ); // } // } //} ////////////////////////////////////////////////////////////////////// // Func: log() // Desc: Logs to Ooganizer.log in defined LogPath or temporary folder using Wide Char // Prms: Variable # of params // // Usge: Log("%s %d %d %d", "test", 1, 2, 3) ////////////////////////////////////////////////////////////////////// void log( LoggingDetail detail, wchar_t * pFmt, ... ) { if(g_LoggingDetail >= detail) { FILE* pFile = NULL; errno_t err = 0; if ( !( err = _wfopen_s( &pFile, g_LogPath, L"at" ) ) ) { if(detail == LOGGING_LOW) fwprintf( pFile, L"ThreadId %i - [Imp] - ", GetCurrentThreadId()); // low == very important messages else if(detail == LOGGING_MEDIUM) fwprintf( pFile, L"ThreadId %i - {P/D} - ", GetCurrentThreadId()); // medium == program details else if(detail == LOGGING_HIGH) fwprintf( pFile, L"ThreadId %i - {X/D} - ", GetCurrentThreadId()); // high == more program details (extra} else if(detail == LOGGING_DEBUG) fwprintf( pFile, L"ThreadId %i - Debug - ", GetCurrentThreadId()); // debug == only show up in debug builds (Debug Only) else fwprintf( pFile, L"ThreadId %i - - ", GetCurrentThreadId()); // should never happen va_list arg; va_start( arg, pFmt ); vfwprintf( pFile, pFmt, arg ); va_end( arg ); if ( pFmt[lstrlen( pFmt ) - 1] != '\n' ) fwprintf( pFile, L"\n" ); fclose( pFile ); } } } ////////////////////////////////////////////////////////////////////// // Func: log() // Desc: Logs the window title text to the Log File // Prms: HWND, handle to a window // str, custom string to log // // Note: GetWindowText doesn't always return TRUE, if this function // gets called during the WndProc it will return False ////////////////////////////////////////////////////////////////////// void log(LoggingDetail detail, HWND hWnd, wchar_t* str) { if(g_LoggingDetail >= detail) { wchar_t Title[MAX_PATH + 1] = {0}; if (GetWindowText(hWnd,Title,MAX_PATH)) log(detail, L"Window - %s - %s", Title, str); } } ////////////////////////////////////////////////////////////////////// // Func: clearLog() // Desc: clears the log file (used at startup) - Only Clears if Logging is enabled ////////////////////////////////////////////////////////////////////// void clearLog() { if(g_LoggingDetail != LOGGING_NONE) { FILE* pFile = NULL; errno_t err = 0; if( !( err = _wfopen_s( &pFile, g_LogPath, L"w" ) ) ) fclose( pFile ); } }