]> Dogcows Code - chaz/thecheat/blob - ChazLog.m
The Cheat 1.2.1
[chaz/thecheat] / ChazLog.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
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)
9 // any later version.
10 //
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.
15 //
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.
19 //
20
21
22 #include "ChazLog.h"
23
24 #include "stdio.h"
25 #include "stdlib.h"
26
27
28 // PrivateAPI
29 void static _ChazPrint( FILE *output, NSString *format, va_list args );
30
31
32 // Static Globals
33 BOOL static _gLogEnabled = NO;
34 FILE static *_gLogFile = stdout;
35 FILE static *_gDebugFile = NULL;
36
37
38 #pragma mark -
39 #pragma mark Setup
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 void ChazLogEnable()
43 {
44 _gLogEnabled = YES;
45 }
46
47 void ChazLogDisable()
48 {
49 if ( _gLogFile != _gDebugFile ) {
50 _gLogEnabled = NO;
51 }
52 }
53
54
55 void ChazDebugSetup()
56 {
57 NSString *filepath = ChazDebugLogPath();
58 FILE *file;
59
60 // look for debug file
61 file = fopen( [filepath lossyCString], "r+" );
62
63 if ( !file ) {
64 // there is no debug file or we don't have permissions
65 return;
66 }
67
68 fclose( file );
69
70 _gDebugFile = fopen( [filepath lossyCString], "w" );
71
72 ChazDebug( @"Debug log found (obviously). Entering debug mode." );
73 }
74
75 void ChazDebugCleanup()
76 {
77 if ( _gDebugFile ) {
78 ChazDebug( @"Debug log cleaned up." );
79
80 fclose ( _gDebugFile );
81 }
82 }
83
84
85 #pragma mark -
86 #pragma mark Logs
87 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
88
89 void ChazLog( NSString *format, ... )
90 {
91 va_list args;
92
93 if ( !_gLogEnabled ) {
94 return;
95 }
96
97 va_start( args, format );
98 // print log to standard i/o
99 _ChazPrint( _gLogFile, format, args );
100 va_end( args );
101 }
102
103 void ChazDebug( NSString *format, ... )
104 {
105 va_list args;
106
107 if ( !_gDebugFile ) {
108 return;
109 }
110
111 va_start( args, format );
112 // print log to the debug file
113 _ChazPrint( _gDebugFile, format, args );
114 va_end( args );
115 }
116
117
118 #pragma mark -
119 #pragma mark Miscellaneous
120 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
121
122 BOOL ChazIsDebugging()
123 {
124 return (_gDebugFile != NULL);
125 }
126
127 NSString *ChazDebugLogPath()
128 {
129 // get filepath
130 return [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"debug.txt"];
131 }
132
133
134 void ChazMapLogToDebug()
135 {
136 if ( _gDebugFile ) {
137 _gLogEnabled = YES;
138 _gLogFile = _gDebugFile;
139 }
140 }
141
142
143 void ChazOpenDebugLog()
144 {
145 [[NSWorkspace sharedWorkspace] openFile:ChazDebugLogPath()];
146 }
147
148
149 #pragma mark -
150 #pragma mark PrivateAPI
151 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
152
153 void _ChazPrint( FILE *output, NSString *format, va_list args )
154 {
155 NSString *string;
156
157 // get formatted string
158 string = [[NSString alloc] initWithFormat:format arguments:args];
159
160 fprintf( output, "[%s] %s\n", [[[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S.%F"
161 timeZone:nil
162 locale:nil] lossyCString], [string lossyCString] );
163 fflush( output );
164
165 [string release];
166 }
167
168
This page took 0.035644 seconds and 4 git commands to generate.