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