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