/////////////////////////////////////////////////////////////////////////////// // Log.cpp // // // // Copyright (c) Jan Knepper, 1998. All Rights Reserved. // // Written by Jan Knepper // // // // Redistribution and use in source and binary forms, with or without // // modification, are permitted provided that the following conditions // // are met: // // 1. Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // // notice, this list of conditions and the following disclaimer in the // // documentation and/or other materials provided with the distribution. // // 3. The name of the author may not be used to endorse or promote products // // derived from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND // // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE // // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // // THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include //#define NO_LOG 1 //#define NO_ILOG 1 //#if defined(I_WIN_32) static int _enabled = 0; static int _enabled_internal = 0; static int _log = 0; static char _buffer [ 1024 ]; #if defined(I_WIN_32) static const char *_name = "C:\\TEMP\\JAK32.LOG"; #else static const char *_name = "C:\\TEMP\\JAK.LOG"; #endif int Log ( const char *source, int line, const char *format, ... ) { #if !defined(NO_LOG) va_list argptr; int result = 0; int i; int fd; int mode = O_WRONLY | O_CREAT | O_BINARY | O_APPEND; if ( ! _enabled ) return ( 0 ); if ( _log == 0 ) mode |= O_TRUNC; if ( ( fd = open ( _name, mode, S_IREAD | S_IWRITE ) ) != -1 ) { time_t t = time ( NULL ); memset ( _buffer, 0, sizeof ( _buffer ) ); sprintf ( _buffer, "%-24.24s %s %5d : ", ctime ( &t ), source, line ); write ( fd, _buffer, strlen ( _buffer ) ); memset ( _buffer, 0, sizeof ( _buffer ) ); va_start ( argptr, format ); result = vsprintf ( _buffer, format, argptr ); va_end ( argptr ); write ( fd, _buffer, strlen ( _buffer ) ); write ( fd, "\r\n", 2 ); close ( fd ); _log++; } return ( result ); #else return ( 0 ); #endif } // Copy of 'Log', but for internal usage. int iLog ( const char *source, int line, const char *format, ... ) { #if !defined(NO_ILOG) va_list argptr; int result = 0; int i; int fd; int mode = O_WRONLY | O_CREAT | O_BINARY | O_APPEND; if ( ! _enabled_internal ) return ( 0 ); if ( _log == 0 ) mode |= O_TRUNC; if ( ( fd = open ( _name, mode, S_IREAD | S_IWRITE ) ) != -1 ) { time_t t = time ( NULL ); memset ( _buffer, 0, sizeof ( _buffer ) ); sprintf ( _buffer, "%-24.24s %s %5d : ", ctime ( &t ), source, line ); write ( fd, _buffer, strlen ( _buffer ) ); memset ( _buffer, 0, sizeof ( _buffer ) ); va_start ( argptr, format ); result = vsprintf ( _buffer, format, argptr ); va_end ( argptr ); write ( fd, _buffer, strlen ( _buffer ) ); write ( fd, "\r\n", 2 ); close ( fd ); _log++; } return ( result ); #else return ( 0 ); #endif } void EnableLog ( int flag ) { if ( _enabled = flag ) if ( ! _enabled_internal ) unlink ( _name ); } void EnableInternalLog ( int flag ) { if ( _enabled_internal = flag ) if ( ! _enabled ) unlink ( _name ); } //#endif