Repository navigation
Expand file tree
/
Copy pathASLog.h
More file actions
322 lines (231 loc) · 10.9 KB
/
Copy pathASLog.h
File metadata and controls
322 lines (231 loc) · 10.9 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*!
ASLog.h
\file ASLog.h
\brief Interface for ASLog utility class
\author Copyright (c) 2008 - 2015 by Alan Staniforth
\date 2008-10-12
\version 1.1.0
This abstract class enhances the functionality of NSLog in several ways:
-# It creates debug logging commands as macros which only compile to functional code
in debugging builds. In release builds these macros are compiled out.
-# The basic NSLog status/origin information is enhanced by the addition of source
file name and line, (and optionally the function) from which the call comes.
-# It is possible to switch the logging from the default destination of stderr to a
user specified file.
This code is based on a number of sources and ideas.The general idea of using
an enhanced, NSLog, implemented as macros which could be compiled out came from:
[A Better NSLog()](http://borkware.com/rants/agentm/mlog/)
Adam Knight then presented the same idea, enhanced a little further and already
packaged up in a neat utility class:
[Better Logging](http://www.hopelessgeek.com/2005/11/18/better-logging)
(now [Better Logging - Internet Archive](https://web.archive.org/web/20110712220001/http://www.hopelessgeek.com/2005/11/18/better-logging))
I started with Adam's class and enhanced the macros as suggested on:
[Stupid C++ Tricks: Adventures in Assert](http://powerof2games.com/node/10)
(now [Stupid C++ Tricks: Adventures in Assert - Internet Archive](https://web.archive.org/web/20081101231431/http://www.powerof2games.com/node/10))
to be absolutely sure the macros were compiled out with no side effects. I added
a "Warning" variant, which would not compile out as suggested in:
[Uncle Jens’s Coding Tips](http://mooseyard.com/Jens/2007/05/uncle-jenss-coding-tips/)
(now [Uncle Jens’s Coding Tips - Internet Archive](https://web.archive.org/web/20090319202059/http://mooseyard.com/Jens/2007/05/uncle-jenss-coding-tips/))
and the idea of reassigning stderr came from:
[NSLogToFile](http://www.cocoadev.com/index.pl?NSLogToFile)
(now [NSLogToFile - Internet Archive](https://web.archive.org/web/20120303210003/http://www.cocoadev.com/index.pl?NSLogToFile))
I personally added the versions of the macros that do "ordinary" enhanced logging
and do not get compiled out.
License
=======
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
Changes
=======
2009-08-23 - Changed +switchLoggingToFile: to +switchLoggingToFile:fromAppDir:
to allow the use of the application directory as a base for the
logging file path.
2009-08-23 - Added #define ASLogVersion to track version
2015-03-30 - Added QuietLog() - a logging substitute for NSLog() that does
not generate so much noise.
2015-04-05 - Added ability to switch logging functions between NSLog() and
QuietLog()
2015-04-08 - Added simple call-through to main logging function without file
or line number info for the standard logging methods. Also
defined accompanying macro.
*/
#import <Foundation/NSDebug.h>
#import <Cocoa/Cocoa.h>
#pragma mark Macro defintions
/*! \def ASLogVersion
@brief String holding version number for the utilities
*/
#define ASLogVersion "1.0.1"
/*!
\name Debug Logging macros.
@relates ASLog
Convenience interface to ASLog Debug Logging methods
- Only compiled in when BUILD_WITH_DEBUG_LOGGING is defined.
- Only fire when either DEBUG_LOG_AUTO_ENABLE is defined or the environment
variable NSDebugEnabled exists and is set to YES
*/
//@{
/*! \def ASDLogOn
@brief Enables debug logging at runtime, compiled out in release builds
\def ASDLogOff
@brief Disables debug logging at runtime, compiled out in release builds
\def ASDQuietLogOn
@brief Enables quiet logging at runtime, compiled out in release builds
\def ASDQuietLogOff
@brief Disables quiet logging at runtime, compiled out in release builds
\def ASDNSLog
@brief No enhancements to NSLog, but will be compiled out in release builds
\def ASDLog
@brief NSLog + logs the sourcefile and line number
\def ASDFnLog
@brief NSLog + logs the sourcefile and line number and calling method
*/
#ifdef BUILD_WITH_DEBUG_LOGGING
// BUILD_WITH_DEBUG_LOGGING is defined, compile the macros in
#define ASDLogOn() do { [ASLog setLogOn:YES]; } while (0)
#define ASDLogOff() do { [ASLog setLogOn:NO]; } while (0)
#define ASDQuietLogOn() do { [ASLog setQuietOn:YES]; } while (0)
#define ASDQuietLogOff() do { [ASLog setQuietOn:NO]; } while (0)
#define ASDNSLog(s, ...) do { [ASLog debugLog:(s),##__VA_ARGS__]; } while (0)
#define ASDLog(s, ...) do { [ASLog debugLog:__FILE__ lineNumber:__LINE__ format:(s),##__VA_ARGS__]; } while (0)
#define ASDFnLog(s, ...) do { [ASLog debugLog:__FILE__ lineNumber:__LINE__ function:(char*)__FUNCTION__ format:(s),##__VA_ARGS__]; } while (0)
#else
// NOOP definitions of the debug logging macros
#define ASDLogOn() do { (void)sizeof(YES); } while (0)
#define ASDLogOff() do { (void)sizeof(YES); } while (0)
#define ASDQuietLogOn() do { (void)sizeof(YES); } while (0)
#define ASDQuietLogOff() do { (void)sizeof(YES); } while (0)
#define ASDNSLog(s, ...) do { (void)sizeof(s); } while (0)
#define ASDLog(s, ...) do { (void)sizeof(s); } while (0)
#define ASDFnLog(s, ...) do { (void)sizeof(s); } while (0)
#endif
//@} (Debug Logging macros)
/*!
\name Normal Logging macros.
@relates ASLog
Convenience interface to ASLog Normal Logging methods
- Still have NSLog enhancements.
- Not compiled out in release builds.
*/
//@{
/*! \def ASNSLog
@brief NSLog, unadorned
*/
#define ASNSLog(s, ...) do { [ASLog log:(s),##__VA_ARGS__]; } while (0)
/*! \def ASFlLog
@brief NSLog + logs the sourcefile and line number
*/
#define ASFlLog(s, ...) do { [ASLog log:__FILE__ lineNumber:__LINE__ format:(s),##__VA_ARGS__]; } while (0)
/*! \def ASFnLog
@brief NSLog + logs the sourcefile and line number and calling method
*/
#define ASFnLog(s, ...) do { [ASLog log:__FILE__ lineNumber:__LINE__ function:(char*)__FUNCTION__ format:(s),##__VA_ARGS__]; } while (0)
//@} (Normal Logging macros)
/*!
\name Warning Logging macros.
@relates ASLog
Convenience interface to ASLog Warning methods
- Still have NSLog enhancements.
- Not compiled out in release builds.
- Obvious in a busy log as every line contains "WARNING"
*/
//@{
/*! \def ASNSWarn
@brief NSLog + "WARNING"
*/
#define ASNSWarn(s, ...) do { [ASLog warn:(s),##__VA_ARGS__]; } while (0)
/*! \def ASWarn
@brief NSLog + "WARNING" + logs the sourcefile and line number
*/
#define ASWarn(s, ...) do { [ASLog warn:__FILE__ lineNumber:__LINE__ format:(s),##__VA_ARGS__]; } while (0)
/*! \def ASFnWarn
@brief NSLog + "WARNING" + logs the sourcefile and line number and calling method
*/
#define ASFnWarn(s, ...) do { [ASLog warn:__FILE__ lineNumber:__LINE__ function:(char*)__FUNCTION__ format:(s),##__VA_ARGS__]; } while (0)
//@} (Warning Logging macros)
#pragma mark Prototypes
/*! \fn QuietLog (NSString *format, ...)
@brief A quieter NSLog()
*/
extern void QuietLog (NSString *format, ...);
#pragma mark Class interface
/*!
\brief A better NSLog...
This abstract class enhances the functionality of NSLog in several ways:
-# It creates debug logging commands as macros which only compile to functional code
in debugging builds. In release builds these macros are compiled out.
-# The basic NSLog status/origin information is enhanced by the addition of source
file name and line, (and optionally the function) from which the call comes.
-# It is possible to switch the logging from the default destination of stderr to a
user specified file.
To enable logging, set the environment variable NSDebugEnabled to YES in the
"Variables to be set in the environment" section of the "Arguments" tab of the
"Edit Active Target..." window.
This is imported in Foundation/NSDebug.h as a flag for other debugging tools.
(The necessary header is imported in this class's header)
*/
@interface ASLog : NSObject {
}
/*!
\name Debug Logging methods.
- Only fire when either DEBUG_LOG_AUTO_ENABLE is defined or the environment
variable NSDebugEnabled exists and is set to YES
*/
//@{
//! @brief Plain NSLog, but will be compiled out in release builds
+ (void)debugLog:(NSString *)format, ...;
//! @brief NSLog, also logs source file and line number
+ (void)debugLog:(char *)sourceFile lineNumber:(int)lineNumber format:(NSString *)format, ...;
//! @brief NSLog, also logs source file, line number and calling method
+ (void)debugLog:(char *)sourceFile lineNumber:(int)lineNumber function:(char *)functionName format:(NSString *)format, ...;
//@} (Debug Logging methods)
/*!
\name Enhanced Normal Logging methods.
- Always fire
*/
//@{
//! @brief NSLog, nothing else.
+ (void)log:(NSString *)format, ...;
//! @brief NSLog, also logs source file and line number
+ (void)log:(char *)sourceFile lineNumber:(int)lineNumber format:(NSString *)format, ...;
//! @brief NSLog, also logs source file, line number and calling method
+ (void)log:(char *)sourceFile lineNumber:(int)lineNumber function:(char *)functionName format:(NSString *)format, ...;
//@} (Enhanced Normal Logging methods)
/*!
\name WARNING Logging methods.
- Always fire
- Always have 'WARNING' in the output so easier to spot in busy log
*/
//@{
//! @brief Plain NSLog, but adds "WARNING"
+ (void)warn:(NSString *)format, ...;
//! @brief NSLog, adds "WARNING" and also logs source file and line number
+ (void)warn:(char *)sourceFile lineNumber:(int)lineNumber format:(NSString *)format, ...;
//! @brief NSLog, adds "WARNING" and also logs source file, line number and calling method
+ (void)warn:(char *)sourceFile lineNumber:(int)lineNumber function:(char *)functionName format:(NSString *)format, ...;
//@} (WARNING Logging methods)
/*!
\name Control methods.
- Used to enable/disable logging for debugging methods and to redirect log output
*/
//@{
//! @brief Enables/Disables logging at runtime for the debug logging methods
+ (void)setLogOn: (BOOL) logOn;
//! @brief Switches logging methods between using NSLog() or QuietLog()
+ (void) setQuietOn: (BOOL) quietOn;
//! @brief Switches stderr to logging to a user specified file
+ (void)switchLoggingToFile:(NSString *)filePath fromAppDir:(BOOL)useAppDirAsBase;
//! @brief Switches stderr back to logging to default output stream
+ (void)restoreStdErr;
//@} (Control methods)
@end