| 1 | #include <stdio.h> | 
|---|
| 2 | #include <stdarg.h> | 
|---|
| 3 | #include <errno.h> | 
|---|
| 4 | #include <string.h> | 
|---|
| 5 | #include <stdlib.h> | 
|---|
| 6 |  | 
|---|
| 7 | #define INCL_DOSMISC | 
|---|
| 8 | #include <os2.h> | 
|---|
| 9 |  | 
|---|
| 10 | #include "Utility.h" | 
|---|
| 11 |  | 
|---|
| 12 | #define LOG_DIR_VAR "HELPMGR_LOG_DIR" | 
|---|
| 13 | #define LOG_FILENAME "helpmgr.log" | 
|---|
| 14 |  | 
|---|
| 15 | int g_CheckedEnvironment = 0; | 
|---|
| 16 | char* g_LogFilePath = NULL; | 
|---|
| 17 |  | 
|---|
| 18 | void stoplog() | 
|---|
| 19 | { | 
|---|
| 20 | g_LogFilePath = NULL; | 
|---|
| 21 | } | 
|---|
| 22 |  | 
|---|
| 23 | void LogEvent( char* format, ... ) | 
|---|
| 24 | { | 
|---|
| 25 | char* LogDir; | 
|---|
| 26 | va_list ap; | 
|---|
| 27 | FILE* f; | 
|---|
| 28 |  | 
|---|
| 29 | if ( g_CheckedEnvironment ) | 
|---|
| 30 | { | 
|---|
| 31 | if ( g_LogFilePath == NULL ) | 
|---|
| 32 | /* logging is not enabled */ | 
|---|
| 33 | return; | 
|---|
| 34 | } | 
|---|
| 35 | else | 
|---|
| 36 | { | 
|---|
| 37 | /* check the environment var to see if logging is enabled */ | 
|---|
| 38 | g_CheckedEnvironment = 1; | 
|---|
| 39 | LogDir = getenv( LOG_DIR_VAR ); | 
|---|
| 40 | if ( LogDir == NULL ) | 
|---|
| 41 | /* not defined */ | 
|---|
| 42 | return; | 
|---|
| 43 | if ( strlen( LogDir ) == 0 ) | 
|---|
| 44 | /* defined as blank? */ | 
|---|
| 45 | return; | 
|---|
| 46 |  | 
|---|
| 47 | /* allocate space for full path, plus slash, plus terminator */ | 
|---|
| 48 | g_LogFilePath = malloc( strlen( LogDir ) + strlen( LOG_FILENAME ) + 2 ); | 
|---|
| 49 | if ( g_LogFilePath == NULL ) | 
|---|
| 50 | /* out of memory */ | 
|---|
| 51 | return; | 
|---|
| 52 |  | 
|---|
| 53 | /* copy path */ | 
|---|
| 54 | strcpy( g_LogFilePath, LogDir ); | 
|---|
| 55 |  | 
|---|
| 56 | /* add ending slash if needed */ | 
|---|
| 57 | if ( g_LogFilePath[ strlen( g_LogFilePath ) - 1 ] != '\\' ) | 
|---|
| 58 | strcat( g_LogFilePath, "\\" ); | 
|---|
| 59 |  | 
|---|
| 60 | /* add filename */ | 
|---|
| 61 | strcat( g_LogFilePath, LOG_FILENAME ); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | f = fopen( g_LogFilePath, "a" ); | 
|---|
| 65 | if ( f == NULL ) | 
|---|
| 66 | { | 
|---|
| 67 | DosBeep( 2000, 50 ); | 
|---|
| 68 | printf( "fopen failed, errno %d: %s", errno, strerror( errno ) ); | 
|---|
| 69 | return; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | va_start( ap, format ); | 
|---|
| 73 | vfprintf( f, format, ap ); | 
|---|
| 74 | fprintf( f, "\n" ); | 
|---|
| 75 | fclose( f ); | 
|---|
| 76 | va_end( ap ); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void LogData( char* data, int length ) | 
|---|
| 80 | { | 
|---|
| 81 | char* p; | 
|---|
| 82 | char buffer[ 64 ]; | 
|---|
| 83 | char hex[ 4 ]; | 
|---|
| 84 | int i; | 
|---|
| 85 |  | 
|---|
| 86 | i = 0; | 
|---|
| 87 | p = data; | 
|---|
| 88 |  | 
|---|
| 89 | while ( p < data + length ) | 
|---|
| 90 | { | 
|---|
| 91 | if ( i == 0 ) | 
|---|
| 92 | { | 
|---|
| 93 | strcpy( buffer, "" ); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | sprintf( hex, "%02x ", (int) *p ); | 
|---|
| 97 | strcat( buffer, hex ); | 
|---|
| 98 |  | 
|---|
| 99 | p ++; | 
|---|
| 100 |  | 
|---|
| 101 | i ++; | 
|---|
| 102 | if ( i == 16 ) | 
|---|
| 103 | { | 
|---|
| 104 | i = 0; | 
|---|
| 105 | LogEvent( buffer ); | 
|---|
| 106 | } | 
|---|
| 107 | } | 
|---|
| 108 | if ( i > 0 ) | 
|---|
| 109 | { | 
|---|
| 110 | LogEvent( buffer ); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|