X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fthecheat;a=blobdiff_plain;f=ChazLog.m;fp=ChazLog.m;h=1637e56f3e484bc0b11d5091284ed723b50c9d20;hp=0000000000000000000000000000000000000000;hb=d27548f80fe411fda2ee69c74a24eab4292267e9;hpb=e8d51183acdd2410a38dcf8f0efbf7c30cd6c581 diff --git a/ChazLog.m b/ChazLog.m new file mode 100644 index 0000000..1637e56 --- /dev/null +++ b/ChazLog.m @@ -0,0 +1,155 @@ +// +// ChazLog.m +// Niobium +// +// Created by NB McGarvey on 9/6/04. +// Copyright 2004 NB McGarvey. All rights reserved. +// + +#include "ChazLog.h" + +#include "stdio.h" +#include "stdlib.h" + + +// PrivateAPI +void static _ChazPrint( FILE *output, NSString *format, va_list args ); + + +// Static Globals +BOOL static _gLogEnabled = NO; +FILE static *_gLogFile = stdout; +FILE static *_gDebugFile = NULL; + + +#pragma mark - +#pragma mark Setup +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + +void ChazLogEnable() +{ + _gLogEnabled = YES; +} + +void ChazLogDisable() +{ + if ( _gLogFile != _gDebugFile ) { + _gLogEnabled = NO; + } +} + + +void ChazDebugSetup() +{ + NSString *filepath = ChazDebugLogPath(); + FILE *file; + + // look for debug file + file = fopen( [filepath lossyCString], "r+" ); + + if ( !file ) { + // there is no debug file or we don't have permissions + return; + } + + fclose( file ); + + _gDebugFile = fopen( [filepath lossyCString], "w" ); + + ChazDebug( @"Debug log found (obviously). Entering debug mode." ); +} + +void ChazDebugCleanup() +{ + if ( _gDebugFile ) { + ChazDebug( @"Debug log cleaned up." ); + + fclose ( _gDebugFile ); + } +} + + +#pragma mark - +#pragma mark Logs +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + +void ChazLog( NSString *format, ... ) +{ + va_list args; + + if ( !_gLogEnabled ) { + return; + } + + va_start( args, format ); + // print log to standard i/o + _ChazPrint( _gLogFile, format, args ); + va_end( args ); +} + +void ChazDebug( NSString *format, ... ) +{ + va_list args; + + if ( !_gDebugFile ) { + return; + } + + va_start( args, format ); + // print log to the debug file + _ChazPrint( _gDebugFile, format, args ); + va_end( args ); +} + + +#pragma mark - +#pragma mark Miscellaneous +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + +BOOL ChazIsDebugging() +{ + return (_gDebugFile != NULL); +} + +NSString *ChazDebugLogPath() +{ + // get filepath + return [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"debug.txt"]; +} + + +void ChazMapLogToDebug() +{ + if ( _gDebugFile ) { + _gLogEnabled = YES; + _gLogFile = _gDebugFile; + } +} + + +void ChazOpenDebugLog() +{ + [[NSWorkspace sharedWorkspace] openFile:ChazDebugLogPath()]; +} + + +#pragma mark - +#pragma mark PrivateAPI +/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ + +void _ChazPrint( FILE *output, NSString *format, va_list args ) +{ + NSString *string; + + // get formatted string + string = [[NSString alloc] initWithFormat:format arguments:args]; + + fprintf( output, "[%s] %s\n", [[[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S.%F" + timeZone:nil + locale:nil] lossyCString], [string lossyCString] ); + fflush( output ); + + [string release]; +} + +