2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 void static _ChazPrint( FILE *output
, NSString
*format
, va_list args
);
33 BOOL static _gLogEnabled
= NO
;
34 FILE static *_gLogFile
= stdout
;
35 FILE static *_gDebugFile
= NULL
;
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 if ( _gLogFile
!= _gDebugFile
) {
57 NSString
*filepath
= ChazDebugLogPath();
60 // look for debug file
61 file
= fopen( [filepath lossyCString
], "r+" );
64 // there is no debug file or we don't have permissions
70 _gDebugFile
= fopen( [filepath lossyCString
], "w" );
72 ChazDebug( @
"Debug log found (obviously). Entering debug mode." );
75 void ChazDebugCleanup()
78 ChazDebug( @
"Debug log cleaned up." );
80 fclose ( _gDebugFile
);
87 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89 void ChazLog( NSString
*format
, ...
)
93 if ( !_gLogEnabled
) {
97 va_start( args
, format
);
98 // print log to standard i/o
99 _ChazPrint( _gLogFile
, format
, args
);
103 void ChazDebug( NSString
*format
, ...
)
107 if ( !_gDebugFile
) {
111 va_start( args
, format
);
112 // print log to the debug file
113 _ChazPrint( _gDebugFile
, format
, args
);
119 #pragma mark Miscellaneous
120 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
122 BOOL ChazIsDebugging()
124 return (_gDebugFile
!= NULL
);
127 NSString
*ChazDebugLogPath()
130 return [[[[NSBundle mainBundle
] bundlePath
] stringByDeletingLastPathComponent
] stringByAppendingPathComponent
:@
"debug.txt"];
134 void ChazMapLogToDebug()
138 _gLogFile
= _gDebugFile
;
143 void ChazOpenDebugLog()
145 [[NSWorkspace sharedWorkspace
] openFile
:ChazDebugLogPath()];
150 #pragma mark PrivateAPI
151 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
153 void _ChazPrint( FILE *output
, NSString
*format
, va_list args
)
157 // get formatted string
158 string
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
160 fprintf( output
, "[%s] %s\n", [[[NSDate date
] descriptionWithCalendarFormat
:@
"%Y-%m-%d %H:%M:%S.%F"
162 locale
:nil] lossyCString
], [string lossyCString
] );