/* * Copyright (C) 1995-1997 Christopher D. Granz * * This header may not be removed. * * Refer to the file "License" included in this package for further * information and before using any of the following. */ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <errno.h> #include "sapphire.h" /* * Functions */ void log_init( void ) { char cBuf[256]; int iIndex = 1000; sprintf( cBuf, "%s%d.log", pLogDir, iIndex ); pLogFile = fopen( cBuf, "r" ); for ( iIndex++; pLogFile != NULL; iIndex++ ) { fclose( pLogFile ); sprintf( cBuf, "%s%d.log", pLogDir, iIndex ); pLogFile = fopen( cBuf, "r" ); } if ( ( pLogFile = fopen( cBuf, "w" ) ) == NULL ) sap_fatal( "Open file: %s: %s.", cBuf, strerror( errno ) ); } /* * Write to the log, using printf formatting. */ void lprintf( char *pFormat, ... ) { va_list vlArgs; char cStr[MAX_STRING]; char *pBuf; VA_START( vlArgs, pFormat ); vsnprintf( cStr, MAX_STRING, pFormat, vlArgs ); pBuf = ctime( &tCurrentTime ); pBuf[24] = '\0'; /* Remove the trailing newline. */ fprintf( pLogFile, "[%s] :: %s\n", pBuf, cStr ); fflush( pLogFile ); VA_END( vlArgs ); } void sap_warning( char *pFormat, ... ) { va_list vlArgs; char cStr[MAX_STRING]; int i; VA_START( vlArgs, pFormat ); vsnprintf( cStr, MAX_STRING, pFormat, vlArgs ); if ( bWarningsFound == FALSE ) { fprintf( stderr, "There are warning(s).\n" ); bWarningsFound = TRUE; } for ( i = 0; cStr[i] != '\0'; i++ ) { if ( i == 75 ) { while ( cStr[i] != ' ' ) { if ( i == 0 ) { /* fprintf( stderr, "\nNo spaces in log string.\n" ); */ break; } i--; } cStr[i] = '\n'; break; } } if ( pCOFilename != NULL && pCOFilename[0] != '\0' ) lprintf( "**** [WARNING]:\n FILE: %s\n LINE: %ld\n %s", pCOFilename, lCurrentLine, cStr ); else lprintf( "**** [WARNING]:\n %s", cStr ); VA_END( vlArgs ); } void sap_error( char *pFormat, ... ) { va_list vlArgs; char cStr[MAX_STRING]; int i; VA_START( vlArgs, pFormat ); vsnprintf( cStr, MAX_STRING, pFormat, vlArgs ); if ( bErrorsFound == FALSE ) { fprintf( stderr, "There are error(s).\n" ); bErrorsFound = TRUE; } for ( i = 0; cStr[i] != '\0'; i++ ) { if ( i == 75 ) { while ( cStr[i] != ' ' ) { if ( i == 0 ) { /* fprintf( stderr, "\nNo spaces in log string.\n" ); */ break; } i--; } cStr[i] = '\n'; break; } } if ( pCOFilename != NULL && pCOFilename[0] != '\0' ) lprintf( "**** [ERROR]:\n FILE: %s\n LINE: %ld\n %s", pCOFilename, lCurrentLine, cStr ); else lprintf( "**** [ERROR]:\n %s", cStr ); VA_END( vlArgs ); } void sap_fatal( char *pFormat, ... ) { va_list vlArgs; char cStr[MAX_STRING]; int i; VA_START( vlArgs, pFormat ); vsnprintf( cStr, MAX_STRING, pFormat, vlArgs ); fprintf( stderr, "Fatal error encountered.\n" ); for ( i = 0; cStr[i] != '\0'; i++ ) { if ( i == 75 ) { while ( cStr[i] != ' ' ) { if ( i == 0 ) { /* fprintf( stderr, "\nNo spaces in log string.\n" ); */ break; } i--; } cStr[i] = '\n'; break; } } if ( pCOFilename != NULL && pCOFilename[0] != '\0' ) lprintf( "**** [FATAL]:\n FILE: %s\n LINE: %ld\n %s", pCOFilename, lCurrentLine, cStr ); else lprintf( "**** [FATAL]:\n %s", cStr ); exit( 1 ); VA_END( vlArgs ); } #ifdef DEBUG void sap_debug( char *pFormat, ... ) { va_list vlArgs; char cStr[MAX_STRING]; int i; VA_START( vlArgs, pFormat ); vsnprintf( cStr, MAX_STRING, pFormat, vlArgs ); fprintf( stderr, "There has been an internal error.\n" ); for ( i = 0; cStr[i] != '\0'; i++ ) { if ( i == 75 ) { while ( cStr[i] != ' ' ) { if ( i == 0 ) { /* fprintf( stderr, "\nNo spaces in log string.\n" ); */ break; } i--; } cStr[i] = '\n'; break; } } lprintf( "**** [INTERNAL ERROR]:\n %s", cStr ); VA_END( vlArgs ); } #endif /* * End of log.c */