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