Oolite
Loading...
Searching...
No Matches
OOLogging.h
Go to the documentation of this file.
1/*
2
3OOLogging.h
4By Jens Ayton
5
6More flexible alternative to NSLog().
7
8
9Copyright (C) 2007-2013 Jens Ayton and contributors
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28
29*/
30
31#import "OOFoundation.h"
33#include <stdarg.h>
34
35#ifndef OOLOG_POISON_NSLOG
36#define OOLOG_POISON_NSLOG 0
37#endif
38
39#ifndef OOLOG_FUNCTION_NAME
40#if defined(__GNUC__) && __GNUC__ >= 2
41#define OOLOG_FUNCTION_NAME __FUNCTION__
42#elif 199901L <= __STDC_VERSION__
43#define OOLOG_FUNCTION_NAME __func__
44#else
45#define OOLOG_FUNCTION_NAME NULL
46#endif
47#endif
48
49#ifndef OOLOG_FILE_NAME
50#ifdef OOLOG_NO_FILE_NAME
51#define OOLOG_FILE_NAME NULL
52#else
53#define OOLOG_FILE_NAME __FILE__
54#endif
55#endif
56
57/* OOLOG_SHORT_CIRCUIT:
58 If nonzero, the test of whether to display a message before evaluating the
59 other parameters of the call. This saves time, but could cause weird bugs
60 if the parameters involve calls with side effects.
61*/
62#ifndef OOLOG_SHORT_CIRCUIT
63#define OOLOG_SHORT_CIRCUIT 1
64#endif
65
66/* General usage:
67 OOLog(messageClass, format, parameters);
68 is conceptually equivalent to:
69 NSLog(format, parameters);
70 except that it will do nothing if logging is disabled for messageClass.
71
72 A message class is a hierarchical string, such as:
73 @"all.script.debug"
74
75 To determine whether scripting is enabled for this class, a setting for
76 @"all.script.debug" is looked up in a settings table. If it is not found,
77 @"all.script" is tried, followed by @"all".
78
79 Message class display settings can be manipulated with
80 OOLogSetDisplayMessagesInClass() and tested with
81 OOLogWillDisplayMessagesInClass().
82*/
83#if OOLOG_SHORT_CIRCUIT
84#define OOLog(class, format, ...) \
85 do { \
86 if (OOLogWillDisplayMessagesInClass(class)) { \
87 OOLogWithFunctionFileAndLine(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, format, ##__VA_ARGS__); \
88 } \
89 } while (0)
90#define OOLogWithArguments(class, format, args) \
91 do { \
92 if (OOLogWillDisplayMessagesInClass(class)) { \
93 OOLogWithFunctionFileAndLineAndArguments(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, format, args); \
94 } \
95 } while (0)
96#else
97#define OOLog(class, format, ...) OOLogWithFunctionFileAndLine(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, format, ##__VA_ARGS__)
98#define OOLogWithArguments(class, format, args) OOLogWithFunctionFileAndLineAndArguments(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, format, args)
99#endif
100
101BOOL OOLogWillDisplayMessagesInClass(NSString* inMessageClass);
102
103void OOLogIndent(void);
104void OOLogOutdent(void);
105
106#if OOLOG_SHORT_CIRCUIT
107#define OOLogIndentIf(class) \
108 do { \
109 if (OOLogWillDisplayMessagesInClass(class)) \
110 OOLogIndent(); \
111 } while (0)
112#define OOLogOutdentIf(class) \
113 do { \
114 if (OOLogWillDisplayMessagesInClass(class)) \
115 OOLogOutdent(); \
116 } while (0)
117#else
118void OOLogIndentIf(NSString* inMessageClass);
119void OOLogOutdentIf(NSString* inMessageClass);
120#endif
121
122#define OOLOG_ERROR_PREFIX @"***** ERROR: "
123#define OOLOG_WARNING_PREFIX @"----- WARNING: "
124
125#define OOLogERR(class, format, ...) OOLogWithPrefix(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, OOLOG_ERROR_PREFIX, format, ##__VA_ARGS__)
126#define OOLogWARN(class, format, ...) OOLogWithPrefix(class, OOLOG_FUNCTION_NAME, OOLOG_FILE_NAME, __LINE__, OOLOG_WARNING_PREFIX, format, ##__VA_ARGS__)
127
128// Remember/restore indent levels, for cases where an exception may occur while indented.
129void OOLogPushIndent(void);
130void OOLogPopIndent(void);
131
132void OOLogWithPrefix(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inPrefix, NSString* inFormat, ...) OO_TAKES_FORMAT_STRING(6, 7);
133void OOLogWithFunctionFileAndLine(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inFormat, ...) OO_TAKES_FORMAT_STRING(5, 6);
134void OOLogWithFunctionFileAndLineAndArguments(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inFormat, va_list inArguments) OO_TAKES_FORMAT_STRING(5, 0);
135
136// OOLogGenericParameterError(): general parameter error message, "***** $function_name: bad parameters. (This is an internal programming error, please report it.)"
137#define OOLogGenericParameterError() OOLogGenericParameterErrorForFunction(OOLOG_FUNCTION_NAME)
138void OOLogGenericParameterErrorForFunction(const char* inFunction);
139
140// OOLogGenericSubclassResponsibility(): general subclass responsibility message, "***** $function_name is a subclass responsibility. (This is an internal programming error, please report it.)"
141#define OOLogGenericSubclassResponsibility() OOLogGenericSubclassResponsibilityForFunction(OOLOG_FUNCTION_NAME)
142void OOLogGenericSubclassResponsibilityForFunction(const char* inFunction);
143
144#if OOLOG_POISON_NSLOG
145#pragma GCC poison NSLog // Use OOLog instead
146#elif !OOLOG_NO_HIJACK_NSLOG
147// Hijack NSLog. Buahahahaha.
148#define NSLog(format, ...) OOLog(kOOLogUnconvertedNSLog, format, ##__VA_ARGS__)
149#define NSLogv(format, args) OOLogWithArguments(kOOLogUnconvertedNSLog, format, args)
150#endif
151
152// OODebugLog() is only included in debug builds.
153#if OO_DEBUG
154#define OODebugLog OOLog
155#else
156#define OODebugLog(class, format, ...) \
157 do { \
158 (void)class; \
159 if (0) \
160 (void)format; \
161 } while (0)
162#endif
163
164// OOExtraLog() is included in debug and test-release builds, but not deployment builds.
165#ifndef NDEBUG
166#define OOExtraLog OOLog
167#else
168#define OOExtraLog(class, format, ...) \
169 do { \
170 (void)class; \
171 if (0) \
172 (void)format; \
173 } while (0)
174#endif
175
176// *** Predefined message classes.
177/* These are general coding error types. Generally a subclass should be used
178 for each instance -- for instance, -[Entity warnAboutHostiles] uses
179 @"general.error.subclassResponsibility.Entity-warnAboutHostiles".
180*/
181
182extern NSString* const kOOLogSubclassResponsibility; // @"general.error.subclassResponsibility"
183extern NSString* const kOOLogParameterError; // @"general.error.parameterError"
184extern NSString* const kOOLogDeprecatedMethod; // @"general.error.deprecatedMethod"
185extern NSString* const kOOLogAllocationFailure; // @"general.error.allocationFailure"
186extern NSString* const kOOLogInconsistentState; // @"general.error.inconsistentState"
187extern NSString* const kOOLogException; // @"exception"
188
189extern NSString* const kOOLogFileNotFound; // @"files.notFound"
190extern NSString* const kOOLogFileNotLoaded; // @"files.notLoaded"
191
192extern NSString* const kOOLogOpenGLError; // @"rendering.opengl.error"
193
194// Don't use. However, #defining it as @"unclassified.module" can be used as a stepping stone to OOLog support.
195extern NSString* const kOOLogUnconvertedNSLog; // @"unclassified"
#define kOOLogUnconvertedNSLog
#define OO_TAKES_FORMAT_STRING(stringIndex, firstToCheck)
void OOLogPushIndent(void)
Definition OOLogging.m:301
NSString *const kOOLogException
Definition OOLogging.m:600
NSString *const kOOLogOpenGLError
Definition OOLogging.m:603
NSString *const kOOLogInconsistentState
Definition OOLogging.m:599
NSString *const kOOLogDeprecatedMethod
Definition OOLogging.m:597
void OOLogPopIndent(void)
Definition OOLogging.m:323
void void void OOLogWithFunctionFileAndLineAndArguments(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat, va_list inArguments) OO_TAKES_FORMAT_STRING(5
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
NSString *const kOOLogFileNotLoaded
Definition OOLogging.m:602
void OOLogOutdent(void)
Definition OOLogging.m:354
void OOLogGenericSubclassResponsibilityForFunction(const char *inFunction)
Definition OOLogging.m:464
void OOLogWithPrefix(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inPrefix, NSString *inFormat,...) OO_TAKES_FORMAT_STRING(6
NSString *const kOOLogAllocationFailure
Definition OOLogging.m:598
NSString *const kOOLogParameterError
Definition OOLogging.m:596
NSString *const kOOLogFileNotFound
Definition OOLogging.m:601
#define OOLogIndentIf(class)
Definition OOLogging.h:107
NSString *const kOOLogSubclassResponsibility
Definition OOLogging.m:595
void void OOLogWithFunctionFileAndLine(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat,...) OO_TAKES_FORMAT_STRING(5
void OOLogGenericParameterErrorForFunction(const char *inFunction)
Definition OOLogging.m:459
void OOLogIndent(void)
Definition OOLogging.m:345