Skip to content

Commit d5e31dd

Browse files
author
Apps4u
committed
Create logging.h
copy of Brenwill Workshop logging.h
1 parent 1467806 commit d5e31dd

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

logging.h

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Logging.h
3+
*
4+
* $Version: Logging 1.0 (1e09f90c5fec) on 2010-07-22 $
5+
* Author: Bill Hollings
6+
* Copyright (c) 2010 The Brenwill Workshop Ltd.
7+
* http://www.brenwill.com
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*
27+
* http://en.wikipedia.org/wiki/MIT_License
28+
*
29+
* Thanks to Nick Dalton for providing the underlying ideas for using variadic macros as
30+
* well as for outputting the code location as part of the log entry. For his ideas, see:
31+
* http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
32+
*/
33+
34+
/**
35+
* For Objective-C code, this library adds flexible, non-intrusive logging capabilities
36+
* that can be efficiently enabled or disabled via compile switches.
37+
*
38+
* There are four levels of logging: Trace, Info, Error and Debug, and each can be enabled
39+
* independently via the LOGGING_LEVEL_TRACE, LOGGING_LEVEL_INFO, LOGGING_LEVEL_ERROR and
40+
* LOGGING_LEVEL_DEBUG switches, respectively.
41+
*
42+
* In addition, ALL logging can be enabled or disabled via the LOGGING_ENABLED switch.
43+
*
44+
* Logging functions are implemented here via macros. Disabling logging, either entirely, or
45+
* at a specific level, completely removes the corresponding log invocations from the compiled
46+
* code, thus eliminating both the memory and CPU overhead that the logging calls would add.
47+
* You might choose, for example, to completely remove all logging from production release code,
48+
* by setting LOGGING_ENABLED off in your production builds settings. Or, as another example,
49+
* you might choose to include Error logging in your production builds by turning only
50+
* LOGGING_ENABLED and LOGGING_LEVEL_ERROR on, and turning the others off.
51+
*
52+
* To perform logging, use any of the following function calls in your code:
53+
*
54+
* LogTrace(fmt, ...) - recommended for detailed tracing of program flow
55+
* - will print if LOGGING_LEVEL_TRACE is set on.
56+
*
57+
* LogInfo(fmt, ...) - recommended for general, infrequent, information messages
58+
* - will print if LOGGING_LEVEL_INFO is set on.
59+
*
60+
* LogError(fmt, ...) - recommended for use only when there is an error to be logged
61+
* - will print if LOGGING_LEVEL_ERROR is set on.
62+
*
63+
* LogDebug(fmt, ...) - recommended for temporary use during debugging
64+
* - will print if LOGGING_LEVEL_DEBUG is set on.
65+
*
66+
* In each case, the functions follow the general NSLog/printf template, where the first argument
67+
* "fmt" is an NSString that optionally includes embedded Format Specifiers, and subsequent optional
68+
* arguments indicate data to be formatted and inserted into the string. As with NSLog, the number
69+
* of optional arguments must match the number of embedded Format Specifiers. For more info, see the
70+
* core documentation for NSLog and String Format Specifiers.
71+
*
72+
* You can choose to have each logging entry automatically include class, method and line information
73+
* by enabling the LOGGING_INCLUDE_CODE_LOCATION switch.
74+
*
75+
* Although you can directly edit this file to turn on or off the switches below, the preferred
76+
* technique is to set these switches via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS
77+
* in your build configuration.
78+
*/
79+
80+
/**
81+
* Set this switch to enable or disable logging capabilities.
82+
* This can be set either here or via the compiler build setting GCC_PREPROCESSOR_DEFINITIONS
83+
* in your build configuration. Using the compiler build setting is preferred for this to
84+
* ensure that logging is not accidentally left enabled by accident in release builds.
85+
*/
86+
#ifndef LOGGING_ENABLED
87+
# define LOGGING_ENABLED 0
88+
#endif
89+
90+
/**
91+
* Set any or all of these switches to enable or disable logging at specific levels.
92+
* These can be set either here or as a compiler build settings.
93+
* For these settings to be effective, LOGGING_ENABLED must also be defined and non-zero.
94+
*/
95+
#ifndef LOGGING_LEVEL_TRACE
96+
# define LOGGING_LEVEL_TRACE 0
97+
#endif
98+
#ifndef LOGGING_LEVEL_INFO
99+
# define LOGGING_LEVEL_INFO 1
100+
#endif
101+
#ifndef LOGGING_LEVEL_ERROR
102+
# define LOGGING_LEVEL_ERROR 1
103+
#endif
104+
#ifndef LOGGING_LEVEL_DEBUG
105+
# define LOGGING_LEVEL_DEBUG 1
106+
#endif
107+
108+
/**
109+
* Set this switch to indicate whether or not to include class, method and line information
110+
* in the log entries. This can be set either here or as a compiler build setting.
111+
*/
112+
#ifndef LOGGING_INCLUDE_CODE_LOCATION
113+
#define LOGGING_INCLUDE_CODE_LOCATION 0
114+
#endif
115+
116+
// *********** END OF USER SETTINGS - Do not change anything below this line ***********
117+
118+
119+
#if !(defined(LOGGING_ENABLED) && LOGGING_ENABLED)
120+
#undef LOGGING_LEVEL_TRACE
121+
#undef LOGGING_LEVEL_INFO
122+
#undef LOGGING_LEVEL_ERROR
123+
#undef LOGGING_LEVEL_DEBUG
124+
#endif
125+
126+
// Logging format
127+
#define LOG_FORMAT_NO_LOCATION(fmt, lvl, ...) NSLog((@"[%@] " fmt), lvl, ##__VA_ARGS__)
128+
#define LOG_FORMAT_WITH_LOCATION(fmt, lvl, ...) NSLog((@"%s[Line %d] [%@] " fmt), __PRETTY_FUNCTION__, __LINE__, lvl, ##__VA_ARGS__)
129+
130+
#if defined(LOGGING_INCLUDE_CODE_LOCATION) && LOGGING_INCLUDE_CODE_LOCATION
131+
#define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_WITH_LOCATION(fmt, lvl, ##__VA_ARGS__)
132+
#else
133+
#define LOG_FORMAT(fmt, lvl, ...) LOG_FORMAT_NO_LOCATION(fmt, lvl, ##__VA_ARGS__)
134+
#endif
135+
136+
// Trace logging - for detailed tracing
137+
#if defined(LOGGING_LEVEL_TRACE) && LOGGING_LEVEL_TRACE
138+
#define LogTrace(fmt, ...) LOG_FORMAT(fmt, @"trace", ##__VA_ARGS__)
139+
#else
140+
#define LogTrace(...)
141+
#endif
142+
143+
// Info logging - for general, non-performance affecting information messages
144+
#if defined(LOGGING_LEVEL_INFO) && LOGGING_LEVEL_INFO
145+
#define LogInfo(fmt, ...) LOG_FORMAT(fmt, @"info", ##__VA_ARGS__)
146+
#else
147+
#define LogInfo(...)
148+
#endif
149+
150+
// Error logging - only when there is an error to be logged
151+
#if defined(LOGGING_LEVEL_ERROR) && LOGGING_LEVEL_ERROR
152+
#define LogError(fmt, ...) LOG_FORMAT(fmt, @"***ERROR***", ##__VA_ARGS__)
153+
#else
154+
#define LogError(...)
155+
#endif
156+
157+
// Debug logging - use only temporarily for highlighting and tracking down problems
158+
#if defined(LOGGING_LEVEL_DEBUG) && LOGGING_LEVEL_DEBUG
159+
#define LogDebug(fmt, ...) LOG_FORMAT(fmt, @"DEBUG", ##__VA_ARGS__)
160+
#else
161+
#define LogDebug(...)
162+
#endif
163+

0 commit comments

Comments
 (0)