/
SILogging.h
executable file
·282 lines (257 loc) · 11.8 KB
/
SILogging.h
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
/*
* SILogging.h
*
* $Version: Logging 1.0 (1e09f90c5fec) on 2010-07-22 $
* Author: Bill Hollings
* Copyright (c) 2010 The Brenwill Workshop Ltd.
* http://www.brenwill.com
*
* Additionals: Matias Pequeno on 2012-09-13
* Copyright (c) 2012 Silicon Illusions, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* http://en.wikipedia.org/wiki/MIT_License
*
* Thanks to Nick Dalton for providing the underlying ideas for using variadic macros as
* well as for outputting the code location as part of the log entry. For his ideas, see:
* http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
*/
/**
* For Objective-C code, this library adds flexible, non-intrusive logging capabilities
* that can be efficiently enabled or disabled via compile switches.
*
* There are four levels of logging: Trace, Info, Error and Debug, and each can be enabled
* independently via the LOGGING_LEVEL_TRACE, LOGGING_LEVEL_INFO, LOGGING_LEVEL_ERROR and
* LOGGING_LEVEL_DEBUG switches, respectively.
*
* In addition, ALL logging can be enabled or disabled via the LOGGING_ENABLED switch.
*
* Logging functions are implemented here via macros. Disabling logging, either entirely, or
* at a specific level, completely removes the corresponding log invocations from the compiled
* code, thus eliminating both the memory and CPU overhead that the logging calls would add.
* You might choose, for example, to completely remove all logging from production release code,
* by setting LOGGING_ENABLED off in your production builds settings. Or, as another example,
* you might choose to include Error logging in your production builds by turning only
* LOGGING_ENABLED and LOGGING_LEVEL_ERROR on, and turning the others off.
*
* To perform logging, use any of the following function calls in your code:
*
* LogTrace(fmt, ...) - recommended for detailed tracing of program flow
* - will print if LOGGING_LEVEL_TRACE is set on.
*
* LogInfo(fmt, ...) - recommended for general, infrequent, information messages
* - will print if LOGGING_LEVEL_INFO is set on.
*
* LogError(fmt, ...) - recommended for use only when there is an error to be logged
* - will print if LOGGING_LEVEL_ERROR is set on.
*
* LogDebug(fmt, ...) - recommended for temporary use during debugging
* - will print if LOGGING_LEVEL_DEBUG is set on.
*
* In each case, the functions follow the general NSLog/printf template, where the first argument
* "fmt" is an NSString that optionally includes embedded Format Specifiers, and subsequent optional
* arguments indicate data to be formatted and inserted into the string. As with NSLog, the number
* of optional arguments must match the number of embedded Format Specifiers. For more info, see the
* core documentation for NSLog and String Format Specifiers.
*
* You can choose to have each logging entry automatically include class, method and line information
* by enabling the LOGGING_INCLUDE_CODE_LOCATION switch.
*
* Although you can directly edit this file to turn on or off the switches below, the preferred
* technique is to set these switches via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS
* in your build configuration.
*/
#import <Foundation/Foundation.h>
#if defined(LOGGING_FORWARD_TO_TEST_FLIGHT) && LOGGING_FORWARD_TO_TEST_FLIGHT
# import "TestFlight.h"
#endif
#if defined(LOGGING_FORWARD_TO_CRASHLYTICS) && LOGGING_FORWARD_TO_CRASHLYTICS
# import <Crashlytics/Crashlytics.h>
#endif
/**
* Set this switch to enable or disable logging capabilities.
* This can be set either here or via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS
* in your build configuration. Using the compiler build setting is preferred for this to
* ensure that logging is not accidentally left enabled by accident in release builds.
*/
#ifndef LOGGING_ENABLED
# define LOGGING_ENABLED 0
#endif
/**
* Set any or all of these switches to enable or disable logging at specific levels.
* These can be set either here or as a compiler build settings.
* For these settings to be effective, LOGGING_ENABLED must also be defined and non-zero.
*/
#ifndef LOGGING_LEVEL_TRACE
# define LOGGING_LEVEL_TRACE 0
#endif
#ifndef LOGGING_LEVEL_INFO
# define LOGGING_LEVEL_INFO 1
#endif
#ifndef LOGGING_LEVEL_WARNING
# define LOGGING_LEVEL_WARNING 1
#endif
#ifndef LOGGING_LEVEL_ERROR
# define LOGGING_LEVEL_ERROR 1
#endif
#ifndef LOGGING_LEVEL_DEBUG
# define LOGGING_LEVEL_DEBUG 1
#endif
/**
* Set this switch to indicate whether or not to include class, method and line information
* in the log entries. This can be set either here or as a compiler build setting.
*/
#ifndef LOGGING_INCLUDE_CODE_LOCATION
# define LOGGING_INCLUDE_CODE_LOCATION 0
#endif
/**
* Set this switch to remove the default output of Date, Time, Process Name
* and ID from logging. This can be set either here or as a compiler build setting.
* Warning: This will use printf() instead of the default NSLog(). Use only on
* debug mode to avoid any performance impact.
*/
#ifndef LOGGING_USE_COMPACT_LOG
# define LOGGING_USE_COMPACT_LOG 0
#endif
/**
* Set this switch if your application was integrated with TestFlight and you
* want to forward logs to it.
*/
#ifndef LOGGING_FORWARD_TO_TEST_FLIGHT
# define LOGGING_FORWARD_TO_TEST_FLIGHT 0
#endif
/**
* Set this switch if your application was integrated with Crashlytics and you
* want to forward logs to it.
*/
#ifndef LOGGING_FORWARD_TO_CRASHLYTICS
# define LOGGING_FORWARD_TO_CRASHLYTICS 0
#endif
/**
* Set this switch if your application is forwarding output to Crashlytics or
* TestFlight and you don't want any output going to the console or system.
* Specially useful for Release and Ad Hoc builds.
*/
#ifndef LOGGING_DISABLE_LOCAL_OUTPUT
# define LOGGING_DISABLE_LOCAL_OUTPUT 0
#endif
// *********** END OF USER SETTINGS - Do not change anything below this line ***********
// Removes Date, Time, Process Name and ID from logging.
extern void compactLog(NSString *format, ...);
#define LogQuiet(fmt, ...) compactLog(fmt, ##__VA_ARGS__)
// Removes Date, Time, Process Name and ID from logging and displays function with an alternate formatting
void compactLogAltFormat(NSString *namespace, int indentLevel, NSString *functionName, NSString *format, ...);
// Disable Logging entirely if LOGGING_ENABLED is 0
#if !(defined(LOGGING_ENABLED) && LOGGING_ENABLED)
# undef LOGGING_LEVEL_TRACE
# undef LOGGING_LEVEL_INFO
# undef LOGGING_LEVEL_ERROR
# undef LOGGING_LEVEL_WARNING
# undef LOGGING_LEVEL_DEBUG
#endif
// Disable NSLog if we don't want local output of any kind
#if defined(LOGGING_DISABLE_LOCAL_OUTPUT) && LOGGING_DISABLE_LOCAL_OUTPUT
# undef NSLog
# undef NSLogv
# define NSLog(...)
# define NSLogv(...)
#endif
// Logging format
#if defined(LOGGING_USE_COMPACT_LOG) && LOGGING_USE_COMPACT_LOG
# define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) compactLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
# define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) compactLog((@"%s:%d [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
#else
# if defined(LOGGING_FORWARD_TO_TEST_FLIGHT) && LOGGING_FORWARD_TO_TEST_FLIGHT
# if defined(LOGGING_FORWARD_TO_CRASHLYTICS) && LOGGING_FORWARD_TO_CRASHLYTICS
# define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) \
NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__); \
TFLog((@"[%@] " fmt), lvl, ##__VA_ARGS__); \
CLSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
# define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) \
NSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__); \
TFLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__); \
CLSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
# else
# define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) \
NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__); \
TFLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
# define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) \
NSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__); \
TFLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
# endif
# elif defined(LOGGING_FORWARD_TO_CRASHLYTICS) && LOGGING_FORWARD_TO_CRASHLYTICS
# define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) \
NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__); \
CLSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
# define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) \
NSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__); \
CLSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
# else
# define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
# define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) NSLog((@"%s [Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
# endif
#endif
// Code location
#if defined(LOGGING_INCLUDE_CODE_LOCATION) && LOGGING_INCLUDE_CODE_LOCATION
# define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_WITH_LOCATION(fmt, lvl, ##__VA_ARGS__)
#else
# define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_NO_LOCATION(fmt, lvl, ##__VA_ARGS__)
#endif
// Trace logging - for detailed tracing
#if defined(LOGGING_LEVEL_TRACE) && LOGGING_LEVEL_TRACE
# define LogTrace(fmt, ...) LOG_FORMAT(fmt, @"trace", ##__VA_ARGS__)
#else
# define LogTrace(...)
#endif
// Info logging - for general, non-performance affecting information messages
#if defined(LOGGING_LEVEL_INFO) && LOGGING_LEVEL_INFO
# define LogInfo(fmt, ...) LOG_FORMAT(fmt, @"info", ##__VA_ARGS__)
#else
# define LogInfo(...)
#endif
// Warning logging - only when something potentially harmful happened
#if defined(LOGGING_LEVEL_WARNING) && LOGGING_LEVEL_WARNING
# define LogWarning(fmt, ...) LOG_FORMAT(fmt, @"***WARNING***", ##__VA_ARGS__)
#else
# define LogWarning(...)
#endif
// Error logging - only when there is an error to be logged
#if defined(LOGGING_LEVEL_ERROR) && LOGGING_LEVEL_ERROR
# define LogError(fmt, ...) LOG_FORMAT(fmt, @"***ERROR***", ##__VA_ARGS__)
#else
# define LogError(...)
#endif
// Debug logging - use only temporarily for highlighting and tracking down problems
#if defined(LOGGING_LEVEL_DEBUG) && LOGGING_LEVEL_DEBUG
# define LogDebug(fmt, ...) LOG_FORMAT(fmt, @"DEBUG", ##__VA_ARGS__)
#else
# define LogDebug(...)
#endif
#pragma mark
// SIKit library Logging
#if defined(LOGGING_ENABLED) && LOGGING_ENABLED
# define LOGGING_GRID_VIEW 1
#endif
// SIGridView logging
#if defined(LOGGING_GRID_VIEW) && LOGGING_GRID_VIEW
# define LogGridView(fmt, ...) LOG_FORMAT(fmt, @"GRID VIEW", ##__VA_ARGS__)
#else
# define LogGridView(...)
#endif