|
|
1.1 ! root 1: /*++ ! 2: ! 3: Copyright (c) 1993 Microsoft Corporation ! 4: ! 5: Module Name: ! 6: ! 7: log.c ! 8: ! 9: Abstract: ! 10: ! 11: This file implements the access to the postmortem log file. ! 12: ! 13: Author: ! 14: ! 15: Wesley Witt (wesw) 1-May-1993 ! 16: ! 17: Environment: ! 18: ! 19: User Mode ! 20: ! 21: --*/ ! 22: ! 23: #include <windows.h> ! 24: #include <stdlib.h> ! 25: #include <stdio.h> ! 26: #include <string.h> ! 27: #include <stdarg.h> ! 28: #include <commdlg.h> ! 29: #include <direct.h> ! 30: ! 31: #include "drwatson.h" ! 32: #include "proto.h" ! 33: #include "messages.h" ! 34: #include "resource.h" ! 35: ! 36: // ! 37: // global variables for this module ! 38: // ! 39: static HANDLE hFile = NULL; ! 40: static HANDLE hLogProtect = NULL; ! 41: static DWORD dwStartingPos = 0; ! 42: ! 43: ! 44: void ! 45: lprintf(DWORD dwFormatId, ...) ! 46: ! 47: /*++ ! 48: ! 49: Routine Description: ! 50: ! 51: This is function is a printf style function for printing messages ! 52: in a message file. ! 53: ! 54: Arguments: ! 55: ! 56: dwFormatId - format id in the message file ! 57: ... - var args ! 58: ! 59: Return Value: ! 60: ! 61: None. ! 62: ! 63: --*/ ! 64: ! 65: { ! 66: char buf[1024]; ! 67: DWORD dwCount; ! 68: va_list args; ! 69: ! 70: va_start( args, dwFormatId ); ! 71: ! 72: dwCount = FormatMessage( ! 73: FORMAT_MESSAGE_FROM_HMODULE, ! 74: NULL, ! 75: dwFormatId, ! 76: 0, // GetUserDefaultLangID(), ! 77: buf, ! 78: sizeof(buf), ! 79: &args ! 80: ); ! 81: ! 82: Assert( dwCount != 0 ); ! 83: ! 84: WriteFile( hFile, buf, dwCount, &dwCount, NULL ); ! 85: ! 86: return; ! 87: } ! 88: ! 89: void ! 90: lprintfs(char *format, ...) ! 91: ! 92: /*++ ! 93: ! 94: Routine Description: ! 95: ! 96: This is function is a printf replacement that writes the output to ! 97: the DrWatson log file. ! 98: ! 99: Arguments: ! 100: ! 101: format - print format ! 102: ... - var args ! 103: ! 104: Return Value: ! 105: ! 106: None. ! 107: ! 108: --*/ ! 109: ! 110: { ! 111: char buf[1024]; ! 112: DWORD cb; ! 113: ! 114: va_list arg_ptr; ! 115: va_start(arg_ptr, format); ! 116: cb = _vsnprintf(buf, sizeof(buf), format, arg_ptr); ! 117: Assert( hFile != NULL ); ! 118: WriteFile( hFile, buf, cb, &cb, NULL ); ! 119: return; ! 120: } ! 121: ! 122: void ! 123: OpenLogFile( char *szFileName, BOOL fAppend, BOOL fVisual ) ! 124: ! 125: /*++ ! 126: ! 127: Routine Description: ! 128: ! 129: Opens the DrWatson logfile for reading & writting. ! 130: ! 131: Arguments: ! 132: ! 133: szFileName - logfile name ! 134: fAppend - append the new data to the end of the file or ! 135: create a new file ! 136: fVisual - visual notification ! 137: ! 138: Return Value: ! 139: ! 140: None. ! 141: ! 142: --*/ ! 143: ! 144: { ! 145: char szName[1024]; ! 146: ! 147: GetAppName( szName, sizeof(szName) ); ! 148: strcat( szName, "LogProtect" ); ! 149: ! 150: hLogProtect = OpenSemaphore( SEMAPHORE_MODIFY_STATE | SYNCHRONIZE, FALSE, szName); ! 151: if (hLogProtect == NULL) { ! 152: hLogProtect = CreateSemaphore( NULL, 0, 1, szName ); ! 153: Assert( hLogProtect != NULL ); ! 154: } ! 155: else { ! 156: WaitForSingleObject( hLogProtect, INFINITE ); ! 157: } ! 158: ! 159: openagain: ! 160: hFile = CreateFile( szFileName, ! 161: GENERIC_WRITE | GENERIC_READ, ! 162: FILE_SHARE_READ, ! 163: NULL, ! 164: fAppend ? OPEN_EXISTING : CREATE_ALWAYS, ! 165: FILE_ATTRIBUTE_ATOMIC_WRITE, ! 166: NULL ! 167: ); ! 168: ! 169: if (fAppend) { ! 170: if (hFile == INVALID_HANDLE_VALUE) { ! 171: // ! 172: // file does not exist, so lets create a new file ! 173: // ! 174: hFile = CreateFile( szFileName, ! 175: GENERIC_WRITE | GENERIC_READ, ! 176: FILE_SHARE_READ, ! 177: NULL, ! 178: CREATE_NEW, ! 179: FILE_ATTRIBUTE_ATOMIC_WRITE, ! 180: NULL ! 181: ); ! 182: if (hFile == INVALID_HANDLE_VALUE) { ! 183: if (fVisual) { ! 184: NonFatalError( LoadRcString(IDS_INVALID_LOGFILE) ); ! 185: _getcwd( szFileName, MAX_PATH ); ! 186: if (!BrowseForDirectory( szFileName )) { ! 187: FatalError( LoadRcString(IDS_CANT_OPEN_LOGFILE) ); ! 188: } ! 189: MakeLogFileName( szFileName ); ! 190: goto openagain; ! 191: } ! 192: else { ! 193: ExitProcess( 1 ); ! 194: } ! 195: } ! 196: ! 197: // ! 198: // write the file banner ! 199: // ! 200: lprintfs( "\r\n" ); ! 201: lprintf( MSG_BANNER ); ! 202: lprintfs( "\r\n" ); ! 203: } ! 204: ! 205: SetFilePointer( hFile, 0, 0, FILE_END ); ! 206: } ! 207: else { ! 208: // ! 209: // write the file banner ! 210: // ! 211: lprintfs( "\r\n" ); ! 212: lprintf( MSG_BANNER ); ! 213: lprintfs( "\r\n" ); ! 214: } ! 215: ! 216: Assert( hFile != INVALID_HANDLE_VALUE ); ! 217: ! 218: dwStartingPos = SetFilePointer( hFile, 0, NULL, FILE_CURRENT ); ! 219: ! 220: return; ! 221: } ! 222: ! 223: void ! 224: CloseLogFile( void ) ! 225: ! 226: /*++ ! 227: ! 228: Routine Description: ! 229: ! 230: Closes the DrWatson logfile & releases the semaphore that ! 231: protects it. ! 232: ! 233: Arguments: ! 234: ! 235: None. ! 236: ! 237: Return Value: ! 238: ! 239: None. ! 240: ! 241: --*/ ! 242: ! 243: { ! 244: CloseHandle( hFile ); ! 245: ReleaseSemaphore( hLogProtect, 1, NULL ); ! 246: CloseHandle( hLogProtect ); ! 247: } ! 248: ! 249: char * ! 250: GetLogFileData( PDWORD pdwLogFileDataSize ) ! 251: ! 252: /*++ ! 253: ! 254: Routine Description: ! 255: ! 256: Reads in all of the logfile data that has been written since it was ! 257: opened. The data is placed into a buffer allocated by this function. ! 258: The caller is responsible for freeing the memory. ! 259: ! 260: Arguments: ! 261: ! 262: pdwLogFileDataSize - pointer to a dword that contains the size ! 263: in bytes of the data that is read. ! 264: ! 265: Return Value: ! 266: ! 267: Valid character pointer to the logfile data ! 268: ! 269: NULL - could not read the data. ! 270: ! 271: --*/ ! 272: ! 273: { ! 274: DWORD dwCurrPos; ! 275: char *p; ! 276: DWORD size; ! 277: ! 278: ! 279: dwCurrPos = SetFilePointer( hFile, 0, NULL, FILE_CURRENT ); ! 280: ! 281: *pdwLogFileDataSize = 0; ! 282: size = dwCurrPos - dwStartingPos; ! 283: ! 284: p = (char *) malloc( size ); ! 285: if (p == NULL) { ! 286: return NULL; ! 287: } ! 288: ! 289: SetFilePointer( hFile, dwStartingPos, NULL, FILE_BEGIN ); ! 290: ! 291: if (!ReadFile( hFile, p, size, &size, NULL )) { ! 292: free( p ); ! 293: p = NULL; ! 294: size = 0; ! 295: } ! 296: ! 297: SetFilePointer( hFile, dwCurrPos, NULL, FILE_BEGIN ); ! 298: ! 299: *pdwLogFileDataSize = size; ! 300: ! 301: return p; ! 302: } ! 303: ! 304: void ! 305: MakeLogFileName( char *szName ) ! 306: ! 307: /*++ ! 308: ! 309: Routine Description: ! 310: ! 311: Concatinates the base logfile name on to the string passed in. ! 312: ! 313: Arguments: ! 314: ! 315: szName - buffer for the logfile name. ! 316: ! 317: Return Value: ! 318: ! 319: None. ! 320: ! 321: --*/ ! 322: ! 323: { ! 324: strcat( szName, "\\drwtsn32.log" ); ! 325: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.