3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
20 void static _ChazPrint( FILE *output
, NSString
*format
, va_list args
);
24 BOOL static _gLogEnabled
= YES
;
25 FILE static *_gLogFile
= stdout
;
26 FILE static *_gDebugFile
= NULL
;
31 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 if ( _gLogFile
!= _gDebugFile
) {
48 NSString
*filepath
= ChazDebugLogPath();
51 // look for debug file
52 file
= fopen( [filepath lossyCString
], "r+" );
55 // there is no debug file or we don't have permissions
61 _gDebugFile
= fopen( [filepath lossyCString
], "w" );
63 ChazDebug( @
"Debug log found (obviously). Entering debug mode." );
66 void ChazDebugCleanup()
69 ChazDebug( @
"Debug log cleaned up." );
71 fclose ( _gDebugFile
);
78 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
80 void ChazLog( NSString
*format
, ...
)
84 if ( !_gLogEnabled
) {
88 va_start( args
, format
);
89 // print log to standard i/o
90 _ChazPrint( _gLogFile
, format
, args
);
94 void ChazDebug( NSString
*format
, ...
)
102 va_start( args
, format
);
103 // print log to the debug file
104 _ChazPrint( _gDebugFile
, format
, args
);
110 #pragma mark Miscellaneous
111 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113 BOOL ChazIsDebugging()
115 return (_gDebugFile
!= NULL
);
118 NSString
*ChazDebugLogPath()
121 return [[[[NSBundle mainBundle
] bundlePath
] stringByDeletingLastPathComponent
] stringByAppendingPathComponent
:@
"debug.txt"];
125 void ChazMapLogToDebug()
129 _gLogFile
= _gDebugFile
;
134 void ChazOpenDebugLog()
136 [[NSWorkspace sharedWorkspace
] openFile
:ChazDebugLogPath()];
141 #pragma mark PrivateAPI
142 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
144 void _ChazPrint( FILE *output
, NSString
*format
, va_list args
)
148 // get formatted string
149 string
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
151 fprintf( output
, "[%s] %s\n", [[[NSDate date
] descriptionWithCalendarFormat
:@
"%Y-%m-%d %H:%M:%S.%F"
153 locale
:nil] lossyCString
], [string lossyCString
] );