5 // Created by NB McGarvey on 9/6/04.
6 // Copyright 2004 NB McGarvey. All rights reserved.
16 void static _ChazPrint( FILE *output
, NSString
*format
, va_list args
);
20 BOOL static _gLogEnabled
= NO
;
21 FILE static *_gLogFile
= stdout
;
22 FILE static *_gDebugFile
= NULL
;
27 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36 if ( _gLogFile
!= _gDebugFile
) {
44 NSString
*filepath
= ChazDebugLogPath();
47 // look for debug file
48 file
= fopen( [filepath lossyCString
], "r+" );
51 // there is no debug file or we don't have permissions
57 _gDebugFile
= fopen( [filepath lossyCString
], "w" );
59 ChazDebug( @
"Debug log found (obviously). Entering debug mode." );
62 void ChazDebugCleanup()
65 ChazDebug( @
"Debug log cleaned up." );
67 fclose ( _gDebugFile
);
74 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 void ChazLog( NSString
*format
, ...
)
80 if ( !_gLogEnabled
) {
84 va_start( args
, format
);
85 // print log to standard i/o
86 _ChazPrint( _gLogFile
, format
, args
);
90 void ChazDebug( NSString
*format
, ...
)
98 va_start( args
, format
);
99 // print log to the debug file
100 _ChazPrint( _gDebugFile
, format
, args
);
106 #pragma mark Miscellaneous
107 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
109 BOOL ChazIsDebugging()
111 return (_gDebugFile
!= NULL
);
114 NSString
*ChazDebugLogPath()
117 return [[[[NSBundle mainBundle
] bundlePath
] stringByDeletingLastPathComponent
] stringByAppendingPathComponent
:@
"debug.txt"];
121 void ChazMapLogToDebug()
125 _gLogFile
= _gDebugFile
;
130 void ChazOpenDebugLog()
132 [[NSWorkspace sharedWorkspace
] openFile
:ChazDebugLogPath()];
137 #pragma mark PrivateAPI
138 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
140 void _ChazPrint( FILE *output
, NSString
*format
, va_list args
)
144 // get formatted string
145 string
= [[NSString alloc
] initWithFormat
:format arguments
:args
];
147 fprintf( output
, "[%s] %s\n", [[[NSDate date
] descriptionWithCalendarFormat
:@
"%Y-%m-%d %H:%M:%S.%F"
149 locale
:nil] lossyCString
], [string lossyCString
] );