Oolite
Loading...
Searching...
No Matches
OOLogging.m
Go to the documentation of this file.
1/*
2
3OOLogging.m
4
5
6Copyright (C) 2007-2013 Jens Ayton and contributors
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#define OOLOG_POISON_NSLOG 0
29
33#import "OOLogHeader.h"
35#import "OOLoggingExtended.h"
36#import "OOPListParsing.h"
37#import "ResourceManager.h"
38
39#undef NSLog // We need to be able to call the real NSLog.
40
41#define PER_THREAD_INDENTATION 1
42
43#if PER_THREAD_INDENTATION
44#if OOLITE_USE_TLS // Define to use __thread keyword where supported
45#define USE_INDENT_GLOBALS 1
46#define THREAD_LOCAL __thread
47#else
48#define USE_INDENT_GLOBALS 0
49static NSString* const kIndentLevelKey = @"org.aegidian.oolite.oolog.indentLevel";
50static NSString* const kIndentStackKey = @"org.aegidian.oolite.oolog.indentStack";
51#endif
52#else
53#define USE_INDENT_GLOBALS 1
54#define THREAD_LOCAL
55#endif
56
57// Control flags for OOLogInternal() - like message classes, but less cool.
58#define OOLOG_NOT_INITED 1
59#define OOLOG_SETTING_SET 0
60#define OOLOG_SETTING_RETRIEVE 0
61#define OOLOG_UNDEFINED_METACLASS 1
62#define OOLOG_BAD_SETTING 1
63#define OOLOG_BAD_DEFAULT_SETTING 1
64#define OOLOG_BAD_POP_INDENT 1
65#define OOLOG_EXCEPTION_IN_LOG 1
66
67// Used to track OOLogPushIndent()/OOLogPopIndent() state.
73
74// We could probably use less state variables.
75static BOOL sInited = NO;
76static NSLock* sLock = nil;
77static NSMutableDictionary* sExplicitSettings = nil;
78static NSMutableDictionary* sDerivedSettingsCache = nil;
79#if USE_INDENT_GLOBALS
80static THREAD_LOCAL unsigned sIndentLevel = 0;
81static THREAD_LOCAL OOLogIndentStackElement* sIndentStack = NULL;
82#endif
83static BOOL sShowFunction = NO;
84static BOOL sShowFileAndLine = NO;
85static BOOL sShowTime = NO; // This is defaulted to YES when loading settings, but after printing log header.
86static BOOL sShowClass = NO; // Ditto.
87static BOOL sDefaultDisplay = YES;
88static BOOL sOverrideInEffect = NO;
89static BOOL sOverrideValue = NO;
90
91// These specific values are used for true, false and inherit in the cache and explicitSettings dictionaries so we can use pointer comparison.
92static NSString* const kTrueToken = @"on";
93static NSString* const kFalseToken = @"off";
94static NSString* const kInheritToken = @"inherit";
95
96// To avoid recursion/self-dependencies, OOLog gets its own logging function.
97#define OOLogInternal(cond, format, ...) \
98 do { \
99 if ((cond)) { \
100 OOLogInternal_(OOLOG_FUNCTION_NAME, format, ##__VA_ARGS__); \
101 } \
102 } while (0)
103static void OOLogInternal_(const char* inFunction, NSString* inFormat, ...);
104
105// Functions used internally
106static void LoadExplicitSettings(void);
107static void LoadExplicitSettingsFromDictionary(NSDictionary* inDict);
108static id ResolveDisplaySetting(NSString* inMessageClass);
109static id ResolveMetaClassReference(NSString* inMetaClass, NSMutableSet* ioSeenMetaClasses);
110
111OOINLINE unsigned GetIndentLevel(void) PURE_FUNC;
112OOINLINE void SetIndentLevel(unsigned level);
113
114#ifndef OOLOG_NO_FILE_NAME
115static NSMapTable* sFileNamesCache = NULL;
116#endif
117
118// Given a boolean, return the appropriate value for the cache dictionary.
119OOINLINE id CacheValue(BOOL inValue) PURE_FUNC;
120OOINLINE id CacheValue(BOOL inValue)
121{
122 return inValue ? kTrueToken : kFalseToken;
123}
124
125/* Inited()
126 Test wether OOLoggingInit() has been called.
127*/
128OOINLINE BOOL Inited(void)
129{
130 if (EXPECT(sInited))
131 return YES;
132 OOLogInternal(OOLOG_NOT_INITED, @"***** ERROR: OOLoggingInit() has not been called.");
133 return NO;
134}
135
136BOOL OOLogWillDisplayMessagesInClass(NSString* inMessageClass)
137{
138 id value = nil;
139
140 if (!Inited())
141 return NO;
142
144 return sOverrideValue;
145
146 [sLock lock];
147
148 // Look for cached value
149 value = [sDerivedSettingsCache objectForKey:inMessageClass];
150 if (EXPECT_NOT(value == nil)) {
151 @try {
152 // No cached value.
153 value = ResolveDisplaySetting(inMessageClass);
154
155 if (value != nil) {
157 sDerivedSettingsCache = [[NSMutableDictionary alloc] init];
158 [sDerivedSettingsCache setObject:value forKey:inMessageClass];
159 }
160 }
161 @catch (id exception) {
162 [sLock unlock];
163 @throw exception;
164 }
165 }
166 [sLock unlock];
167
168 OOLogInternal(OOLOG_SETTING_RETRIEVE, @"%@ is %s", inMessageClass, (value == kTrueToken) ? "on" : "off");
169 return value == kTrueToken;
170}
171
172void OOLogSetDisplayMessagesInClass(NSString* inClass, BOOL inFlag)
173{
174 id value = nil;
175
176 if (!Inited())
177 return;
178
179 [sLock lock];
180 value = [sExplicitSettings objectForKey:inClass];
181 if (value == nil || value != CacheValue(inFlag)) {
182 OOLogInternal(OOLOG_SETTING_SET, @"Setting %@ to %s", inClass, inFlag ? "ON" : "OFF");
183
184 [sExplicitSettings setObject:CacheValue(inFlag) forKey:inClass];
185
186 // Clear cache and let it be rebuilt as needed.
188 } else {
189 OOLogInternal(OOLOG_SETTING_SET, @"Keeping %@ %s", inClass, inFlag ? "ON" : "OFF");
190 }
191 [sLock unlock];
192}
193
194NSString* OOLogGetParentMessageClass(NSString* inClass)
195{
196 NSRange range;
197
198 if (inClass == nil)
199 return nil;
200
201 range = [inClass rangeOfString:@"." options:NSCaseInsensitiveSearch | NSLiteralSearch | NSBackwardsSearch]; // Only NSBackwardsSearch is important, others are optimizations
202 if (range.location == NSNotFound)
203 return nil;
204
205 return [inClass substringToIndex:range.location];
206}
207
208#if !OOLOG_SHORT_CIRCUIT
209
210void OOLogIndentIf(NSString* inMessageClass)
211{
212 if (OOLogWillDisplayMessagesInClass(inMessageClass))
213 OOLogIndent();
214}
215
216void OOLogOutdentIf(NSString* inMessageClass)
217{
218 if (OOLogWillDisplayMessagesInClass(inMessageClass))
219 OOLogOutdent();
220}
221
222#endif
223
224#if USE_INDENT_GLOBALS
225
226#if OOLITE_USE_TLS
227#define INDENT_LOCK() \
228 do { \
229 } while (0)
230#define INDENT_UNLOCK() \
231 do { \
232 } while (0)
233#else
234#define INDENT_LOCK() [sLock lock]
235#define INDENT_UNLOCK() [sLock unlock]
236#endif
237
238OOINLINE unsigned GetIndentLevel(void)
239{
240 return sIndentLevel;
241}
242
243OOINLINE void SetIndentLevel(unsigned value)
244{
245 sIndentLevel = value;
246}
247
248void OOLogPushIndent(void)
249{
250 OOLogIndentStackElement* elem = NULL;
251
252 elem = malloc(sizeof *elem);
253 if (elem != NULL) {
254 INDENT_LOCK();
255
256 elem->indent = sIndentLevel;
257 elem->link = sIndentStack;
258 sIndentStack = elem;
259
261 }
262}
263
264void OOLogPopIndent(void)
265{
266 INDENT_LOCK();
267
268 OOLogIndentStackElement* elem = sIndentStack;
269
270 if (elem != NULL) {
271 sIndentStack = elem->link;
272 sIndentLevel = elem->indent;
273 free(elem);
274 } else {
275 OOLogInternal(OOLOG_BAD_POP_INDENT, @"OOLogPopIndent(): state stack underflow.");
276 }
278}
279
280#else // !USE_INDENT_GLOBALS
281
282#define INDENT_LOCK() \
283 do { \
284 } while (0)
285#define INDENT_UNLOCK() \
286 do { \
287 } while (0)
288
290{
291 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
292 return [[threadDict objectForKey:kIndentLevelKey] unsignedIntValue];
293}
294
295OOINLINE void SetIndentLevel(unsigned value)
296{
297 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
298 [threadDict setObject:[NSNumber numberWithUnsignedInt:value] forKey:kIndentLevelKey];
299}
300
302{
303 OOLogIndentStackElement* elem = NULL;
304 NSMutableDictionary* threadDict = nil;
305 NSValue* val = nil;
306
307 elem = malloc(sizeof *elem);
308 if (elem != NULL) {
309 threadDict = [[NSThread currentThread] threadDictionary];
310 val = [threadDict objectForKey:kIndentStackKey];
311
312 elem->indent = [[threadDict objectForKey:kIndentLevelKey] intValue];
313 elem->link = [val pointerValue];
314
315 /*
316 Clang static analyzer reports elem not released here. It is in fact
317 released in OOLogPopIndent().
318 */
319 [threadDict setObject:[NSValue valueWithPointer:elem] forKey:kIndentStackKey];
320 }
321}
322
324{
325 OOLogIndentStackElement* elem = NULL;
326 NSMutableDictionary* threadDict = nil;
327 NSValue* val = nil;
328
329 threadDict = [[NSThread currentThread] threadDictionary];
330 val = [threadDict objectForKey:kIndentStackKey];
331
332 elem = [val pointerValue];
333
334 if (elem != NULL) {
335 [threadDict setObject:[NSNumber numberWithUnsignedInt:elem->indent] forKey:kIndentLevelKey];
336 [threadDict setObject:[NSValue valueWithPointer:elem->link] forKey:kIndentStackKey];
337 free(elem);
338 } else {
339 OOLogInternal(OOLOG_BAD_POP_INDENT, @"OOLogPopIndent(): state stack underflow.");
340 }
341}
342
343#endif // USE_INDENT_GLOBALS
344
345void OOLogIndent(void)
346{
347 INDENT_LOCK();
348
350
352}
353
354void OOLogOutdent(void)
355{
356 INDENT_LOCK();
357
358 unsigned indentLevel = GetIndentLevel();
359 if (indentLevel != 0)
360 SetIndentLevel(indentLevel - 1);
361
363}
364
365void OOLogWithPrefix(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inPrefix, NSString* inFormat, ...)
366{
367 if (!OOLogWillDisplayMessagesInClass(inMessageClass))
368 return;
369 va_list args;
370 va_start(args, inFormat);
371 OOLogWithFunctionFileAndLineAndArguments(inMessageClass, inFunction, inFile, inLine, [inPrefix stringByAppendingString:inFormat], args);
372 va_end(args);
373}
374
375void OOLogWithFunctionFileAndLine(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inFormat, ...)
376{
377 va_list args;
378
379 va_start(args, inFormat);
380 OOLogWithFunctionFileAndLineAndArguments(inMessageClass, inFunction, inFile, inLine, inFormat, args);
381 va_end(args);
382}
383
384void OOLogWithFunctionFileAndLineAndArguments(NSString* inMessageClass, const char* inFunction, const char* inFile, unsigned long inLine, NSString* inFormat, va_list inArguments)
385{
386 NSAutoreleasePool* pool = nil;
387 NSString* formattedMessage = nil;
388 unsigned indentLevel;
389
390 if (inFormat == nil)
391 return;
392
393#if !OOLOG_SHORT_CIRCUIT
394 if (!OOLogWillDisplayMessagesInClass(inMessageClass))
395 return;
396#endif
397
398 pool = [[NSAutoreleasePool alloc] init];
399 @try {
400 // Do argument substitution
401 formattedMessage = [[[NSString alloc] initWithFormat:inFormat arguments:inArguments] autorelease];
402
403 // Apply various prefix options
404#ifndef OOLOG_NO_FILE_NAME
405 if (sShowFileAndLine && inFile != NULL) {
406 if (sShowFunction) {
407 formattedMessage = [NSString stringWithFormat:@"%s (%@:%lu): %@", inFunction, OOLogAbbreviatedFileName(inFile), inLine, formattedMessage];
408 } else {
409 formattedMessage = [NSString stringWithFormat:@"%@:%lu: %@", OOLogAbbreviatedFileName(inFile), inLine, formattedMessage];
410 }
411 } else
412#endif
413 {
414 if (sShowFunction) {
415 formattedMessage = [NSString stringWithFormat:@"%s: %@", inFunction, formattedMessage];
416 }
417 }
418
419 if (sShowClass) {
421 formattedMessage = [NSString stringWithFormat:@"[%@] %@", inMessageClass, formattedMessage];
422 } else {
423 formattedMessage = [NSString stringWithFormat:@"[%@]: %@", inMessageClass, formattedMessage];
424 }
425 }
426
427 if (sShowTime) {
428 formattedMessage = [NSString stringWithFormat:@"%@ %@", [[NSDate date] descriptionWithCalendarFormat:@"%H:%M:%S.%F" timeZone:nil locale:nil], formattedMessage];
429 }
430
431 // Apply indentation
432 indentLevel = GetIndentLevel();
433 if (indentLevel != 0) {
434#define INDENT_FACTOR 2 /* Spaces per indent level */
435#define MAX_INDENT 64 /* Maximum number of indentation _spaces_ */
436
437 unsigned indent;
438 // String of 64 spaces (null-terminated)
439 const char spaces[MAX_INDENT + 1] = " ";
440 const char* indentString;
441
442 indent = INDENT_FACTOR * indentLevel;
443 if (MAX_INDENT < indent)
444 indent = MAX_INDENT;
445 indentString = &spaces[MAX_INDENT - indent];
446
447 formattedMessage = [NSString stringWithFormat:@"%s%@", indentString, formattedMessage];
448 }
449
450 OOLogOutputHandlerPrint(formattedMessage);
451 }
452 @catch (NSException* exception) {
453 OOLogInternal(OOLOG_EXCEPTION_IN_LOG, @"***** Exception thrown during logging: %@ : %@", [exception name], [exception reason]);
454 }
455
456 [pool release];
457}
458
459void OOLogGenericParameterErrorForFunction(const char* inFunction)
460{
461 OOLog(kOOLogParameterError, @"***** %s: bad parameters. (This is an internal programming error, please report it.)", inFunction);
462}
463
465{
466 OOLog(kOOLogSubclassResponsibility, @"***** %s is a subclass responsibility. (This is an internal programming error, please report it.)", inFunction);
467}
468
470{
471 return sShowFunction;
472}
473
474void OOLogSetShowFunction(BOOL flag)
475{
476 flag = !!flag; // YES or NO, not 42.
477
478 if (flag != sShowFunction) {
479 sShowFunction = flag;
480 [[NSUserDefaults standardUserDefaults] setBool:flag forKey:@"logging-show-function"];
481 }
482}
483
485{
486 return sShowFileAndLine;
487}
488
490{
491 flag = !!flag; // YES or NO, not 42.
492
493 if (flag != sShowFileAndLine) {
494 sShowFileAndLine = flag;
495 [[NSUserDefaults standardUserDefaults] setBool:flag forKey:@"logging-show-file-and-line"];
496 }
497}
498
500{
501 return sShowTime;
502}
503
504void OOLogSetShowTime(BOOL flag)
505{
506 flag = !!flag; // YES or NO, not 42.
507
508 if (flag != sShowTime) {
509 sShowTime = flag;
510 [[NSUserDefaults standardUserDefaults] setBool:flag forKey:@"logging-show-time"];
511 }
512}
513
515{
516 return sShowClass;
517}
518
520{
521 flag = !!flag; // YES or NO, not 42.
522
523 if (flag != sShowClass) {
524 sShowClass = flag;
525 [[NSUserDefaults standardUserDefaults] setBool:flag forKey:@"logging-show-class"];
526 }
527}
528
530{
531 sShowClass = !!flag; // YES or NO, not 42.
532}
533
535{
536 NSAutoreleasePool* pool = nil;
537
538 if (sInited)
539 return;
540
541 pool = [[NSAutoreleasePool alloc] init];
542
543 sLock = [[NSLock alloc] init];
544 [sLock setName:@"OOLogging lock"];
545 if (sLock == nil)
546 exit(EXIT_FAILURE);
547
548#ifndef OOLOG_NO_FILE_NAME
549 sFileNamesCache = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSObjectMapValueCallBacks, 100);
550#endif
551
552 sInited = YES; // Must be before OOLogOutputHandlerInit().
554
556
558
559 [pool release];
560}
561
563{
564 if (!sInited)
565 return;
566
568
569 /* We do not set sInited to NO. Instead, the output handler is required
570 to be able to handle working even after being closed. Under OS X, this
571 is done by writing to stderr in this case; on other platforms, NSLog()
572 is used and OOLogOutputHandlerClose() is a no-op.
573 */
574}
575
580
582{
583 static unsigned lastMarkerID = 0;
584 unsigned thisMarkerID;
585 NSString* marker = nil;
586
587 [sLock lock];
588 thisMarkerID = ++lastMarkerID;
589 [sLock unlock];
590
591 marker = [NSString stringWithFormat:@"\n\n========== [Marker %u] ==========", thisMarkerID];
593}
594
595NSString* const kOOLogSubclassResponsibility = @"general.error.subclassResponsibility";
596NSString* const kOOLogParameterError = @"general.error.parameterError";
597NSString* const kOOLogDeprecatedMethod = @"general.error.deprecatedMethod";
598NSString* const kOOLogAllocationFailure = @"general.error.allocationFailure";
599NSString* const kOOLogInconsistentState = @"general.error.inconsistentState";
600NSString* const kOOLogException = @"exception";
601NSString* const kOOLogFileNotFound = @"files.notFound";
602NSString* const kOOLogFileNotLoaded = @"files.notLoaded";
603NSString* const kOOLogOpenGLError = @"rendering.opengl.error";
604NSString* const kOOLogUnconvertedNSLog = @"unclassified";
605
606/* OOLogInternal_()
607 Implementation of OOLogInternal(), private logging function used by
608 OOLogging so it doesn’t depend on itself (and risk recursiveness).
609*/
610static void OOLogInternal_(const char* inFunction, NSString* inFormat, ...)
611{
612 va_list args;
613 NSString* formattedMessage = nil;
614 NSAutoreleasePool* pool = nil;
615
616 pool = [[NSAutoreleasePool alloc] init];
617
618 @try {
619 va_start(args, inFormat);
620 formattedMessage = [[[NSString alloc] initWithFormat:inFormat arguments:args] autorelease];
621 va_end(args);
622
623 formattedMessage = [NSString stringWithFormat:@"OOLogging internal - %s: %@", inFunction, formattedMessage];
624
625 OOLogOutputHandlerPrint(formattedMessage);
626 }
627 @catch (NSException* exception) {
628 fprintf(stderr, "***** Exception in OOLogInternal_(): %s : %s", [[exception name] UTF8String], [[exception reason] UTF8String]);
629 }
630
631 [pool release];
632}
633
634/* LoadExplicitSettings()
635 Read settings from logcontrol.plists, merge in settings from preferences.
636
637 Log settings are loaded from the following locations, from lowest to
638 highest priority:
639 * logcontrol.plist inside OXPs, but only in hierarchies not defined
640 by the built-in plist.
641 * Built-in logcontrol.plist.
642 * Loose logcontrol.plist files in AddOns folders.
643 * Preferences (settable through the debug console).
644
645 Because the settings are loaded very early (OOLoggingInit() time), before
646 the state of strict mode has been set, OXP settings are always loaded.
647 Since these generally shouldn't have any effect in strict mode, that's
648 fine.
649*/
650static void LoadExplicitSettings(void)
651{
652 // Load display settings.
653 NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults];
654 sShowFunction = [prefs oo_boolForKey:@"logging-show-function" defaultValue:NO];
655 sShowFileAndLine = [prefs oo_boolForKey:@"logging-show-file-and-line" defaultValue:NO];
656 sShowTime = [prefs oo_boolForKey:@"logging-show-time" defaultValue:YES];
657 sShowClass = [prefs oo_boolForKey:@"logging-show-class" defaultValue:YES];
658
659 NSDictionary* oldSettings = sExplicitSettings;
660
661 /*
662 HACK: we look up search paths while loading settings, which touches
663 the cache, which logs stuff. To avoid spam like dataCache.retrieve.success,
664 we first load the built-in logcontrol.plist only and use it while
665 loading the full settings.
666 */
667 NSString* path = [[[ResourceManager builtInPath] stringByAppendingPathComponent:@"Config"]
668 stringByAppendingPathComponent:@"logcontrol.plist"];
669 sExplicitSettings = [NSMutableDictionary dictionary];
671
672 // Release previous sExplicitSettings.
673 [oldSettings release];
674
675 // Load new settings.
676 NSDictionary* settings = [ResourceManager logControlDictionary];
677
678 // Replace the old dictionary.
679 sExplicitSettings = [[NSMutableDictionary alloc] init];
680
681 // Populate.
683
684 // Get _default and _override values.
685 id value = [sExplicitSettings objectForKey:@"_default"];
686 if (value != nil && [value respondsToSelector:@selector(boolValue)]) {
687 if (value == kTrueToken)
688 sDefaultDisplay = YES;
689 else if (value == kFalseToken)
690 sDefaultDisplay = NO;
691 else
692 OOLogInternal(OOLOG_BAD_DEFAULT_SETTING, @"_default may not be set to a metaclass, ignoring.");
693
694 [sExplicitSettings removeObjectForKey:@"_default"];
695 }
696 value = [sExplicitSettings objectForKey:@"_override"];
697 if (value != nil && [value respondsToSelector:@selector(boolValue)]) {
698 if (value == kTrueToken) {
699 sOverrideInEffect = YES;
700 sOverrideValue = YES;
701 } else if (value == kFalseToken) {
702 sOverrideInEffect = YES;
703 sOverrideValue = NO;
704 } else
705 OOLogInternal(OOLOG_BAD_DEFAULT_SETTING, @"_override may not be set to a metaclass, ignoring.");
706
707 [sExplicitSettings removeObjectForKey:@"_override"];
708 }
709
710 // Invalidate cache.
712
714}
715
716/* LoadExplicitSettingsFromDictionary()
717 Helper for LoadExplicitSettings().
718*/
719static void LoadExplicitSettingsFromDictionary(NSDictionary* inDict)
720{
721 id key = nil;
722 foreachkey(key, inDict)
723 {
724 id value = [inDict objectForKey:key];
725
726 /* Supported values:
727 "yes", "true" or "on" -> kTrueToken
728 "no", "false" or "off" -> kFalseToken
729 "inherit" or "inherited" -> nil
730 NSNumber -> kTrueToken or kFalseToken
731 "$metaclass" -> "$metaclass"
732 */
733 if ([value isKindOfClass:[NSString class]]) {
734 if (NSOrderedSame == [value caseInsensitiveCompare:@"yes"] || NSOrderedSame == [value caseInsensitiveCompare:@"true"] || NSOrderedSame == [value caseInsensitiveCompare:@"on"]) {
735 value = kTrueToken;
736 } else if (NSOrderedSame == [value caseInsensitiveCompare:@"no"] || NSOrderedSame == [value caseInsensitiveCompare:@"false"] || NSOrderedSame == [value caseInsensitiveCompare:@"off"]) {
737 value = kFalseToken;
738 } else if (NSOrderedSame == [value caseInsensitiveCompare:@"inherit"] || NSOrderedSame == [value caseInsensitiveCompare:@"inherited"]) {
739 value = nil;
740 [sExplicitSettings removeObjectForKey:key];
741 } else if (![value hasPrefix:@"$"]) {
742 OOLogInternal(OOLOG_BAD_SETTING, @"Bad setting value \"%@\" (expected yes, no, inherit or $metaclass).", value);
743 value = nil;
744 }
745 } else if ([value respondsToSelector:@selector(boolValue)]) {
746 value = CacheValue([value boolValue]);
747 } else {
748 OOLogInternal(OOLOG_BAD_SETTING, @"Bad setting value \"%@\" (expected yes, no, inherit or $metaclass).", value);
749 value = nil;
750 }
751
752 if (value != nil) {
753 [sExplicitSettings setObject:value forKey:key];
754 }
755 }
756}
757
758/* OOLogAbbreviatedFileName()
759 Map full file paths provided by __FILE__ to more mananagable file names,
760 with caching.
761*/
762NSString* OOLogAbbreviatedFileName(const char* inName)
763{
764 NSString* name = nil;
765
766 if (EXPECT_NOT(inName == NULL))
767 return @"unspecified file";
768
769 [sLock lock];
770 name = NSMapGet(sFileNamesCache, inName);
771 if (EXPECT_NOT(name == nil)) {
772 name = [[NSString stringWithUTF8String:inName] lastPathComponent];
773 NSMapInsertKnownAbsent(sFileNamesCache, inName, name);
774 }
775 [sLock unlock];
776
777 return name;
778}
779
780/* Look up setting for a message class in explicit settings, resolving
781 inheritance and metaclasses.
782*/
783static id ResolveDisplaySetting(NSString* inMessageClass)
784{
785 id value = nil;
786 NSMutableSet* seenMetaClasses = nil;
787
788 if (inMessageClass == nil)
790
791 value = [sExplicitSettings objectForKey:inMessageClass];
792
793 // Simple case: explicit setting for this value
794 if (value == kTrueToken || value == kFalseToken)
795 return value;
796
797 // Simplish case: use inherited value
798 if (value == nil || value == kInheritToken)
800
801 // Less simple case: should be a metaclass.
802 seenMetaClasses = [NSMutableSet set];
803 return ResolveMetaClassReference(value, seenMetaClasses);
804}
805
806/* Resolve a metaclass reference, recursively if necessary. The
807 ioSeenMetaClasses dictionary is used to avoid loops.
808*/
809static id ResolveMetaClassReference(NSString* inMetaClass, NSMutableSet* ioSeenMetaClasses)
810{
811 id value = nil;
812
813 // All values should have been checked at load time, but what the hey.
814 if (![inMetaClass isKindOfClass:[NSString class]] || ![inMetaClass hasPrefix:@"$"]) {
815 OOLogInternal(OOLOG_BAD_SETTING, @"Bad setting value \"%@\" (expected yes, no, inherit or $metaclass). Falling back to _default.", inMetaClass);
817 }
818
819 [ioSeenMetaClasses addObject:inMetaClass];
820
821 value = [sExplicitSettings objectForKey:inMetaClass];
822
823 if (value == kTrueToken || value == kFalseToken)
824 return value;
825 if (value == nil) {
826 OOLogInternal(OOLOG_UNDEFINED_METACLASS, @"Reference to undefined metaclass %@, falling back to _default.", inMetaClass);
828 }
829
830 // If we get here, it should be a metaclass reference.
831 return ResolveMetaClassReference(value, ioSeenMetaClasses);
832}
#define kOOLogUnconvertedNSLog
static BOOL sInited
Definition OOCPUInfo.m:56
#define DESTROY(x)
#define foreachkey(VAR, DICT)
#define EXPECT_NOT(x)
#define OOINLINE
#define EXPECT(x)
#define PURE_FUNC
void OOPrintLogHeader(void)
Definition OOLogHeader.m:57
void OOLogOutputHandlerPrint(NSString *string)
void OOLogOutputHandlerClose(void)
void OOLogOutputHandlerInit(void)
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
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
NSString *const kOOLogFileNotLoaded
Definition OOLogging.m:602
#define OOLog(class, format,...)
Definition OOLogging.h:84
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
static BOOL sShowFunction
Definition OOLogging.m:83
void OOLogPushIndent(void)
Definition OOLogging.m:301
static BOOL sShowClass
Definition OOLogging.m:86
static NSString *const kFalseToken
Definition OOLogging.m:93
void OOLoggingTerminate(void)
Definition OOLogging.m:562
static NSMutableDictionary * sExplicitSettings
Definition OOLogging.m:77
OOINLINE unsigned GetIndentLevel(void) PURE_FUNC
Definition OOLogging.m:289
OOINLINE void SetIndentLevel(unsigned level)
Definition OOLogging.m:295
void OOLogPopIndent(void)
Definition OOLogging.m:323
static NSString *const kTrueToken
Definition OOLogging.m:92
static void LoadExplicitSettings(void)
Definition OOLogging.m:650
void OOLogWithPrefix(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inPrefix, NSString *inFormat,...)
Definition OOLogging.m:365
BOOL OOLogWillDisplayMessagesInClass(NSString *inMessageClass)
Definition OOLogging.m:136
#define OOLOG_BAD_DEFAULT_SETTING
Definition OOLogging.m:63
void OOLogWithFunctionFileAndLine(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat,...)
Definition OOLogging.m:375
static BOOL sShowTime
Definition OOLogging.m:85
void OOLogSetShowFileAndLine(BOOL flag)
Definition OOLogging.m:489
#define OOLOG_SETTING_RETRIEVE
Definition OOLogging.m:60
void OOLogSetShowTime(BOOL flag)
Definition OOLogging.m:504
void OOLogSetShowFunction(BOOL flag)
Definition OOLogging.m:474
void OOLogWithFunctionFileAndLineAndArguments(NSString *inMessageClass, const char *inFunction, const char *inFile, unsigned long inLine, NSString *inFormat, va_list inArguments)
Definition OOLogging.m:384
static BOOL sOverrideValue
Definition OOLogging.m:89
BOOL OOLogShowFunction(void)
Definition OOLogging.m:469
void OOLogOutdent(void)
Definition OOLogging.m:354
OOINLINE BOOL Inited(void)
Definition OOLogging.m:128
static NSString *const kIndentStackKey
Definition OOLogging.m:50
static BOOL sShowFileAndLine
Definition OOLogging.m:84
void OOLoggingReloadSettings(void)
Definition OOLogging.m:576
void OOLogGenericSubclassResponsibilityForFunction(const char *inFunction)
Definition OOLogging.m:464
#define OOLOG_EXCEPTION_IN_LOG
Definition OOLogging.m:65
static NSLock * sLock
Definition OOLogging.m:76
#define OOLOG_SETTING_SET
Definition OOLogging.m:59
void OOLoggingInit(void)
Definition OOLogging.m:534
BOOL OOLogShowMessageClass(void)
Definition OOLogging.m:514
BOOL OOLogShowTime(void)
Definition OOLogging.m:499
NSString * OOLogAbbreviatedFileName(const char *inName)
Definition OOLogging.m:762
#define OOLogInternal(cond, format,...)
Definition OOLogging.m:97
static BOOL sOverrideInEffect
Definition OOLogging.m:88
static NSString *const kIndentLevelKey
Definition OOLogging.m:49
void OOLogSetDisplayMessagesInClass(NSString *inClass, BOOL inFlag)
Definition OOLogging.m:172
OOINLINE id CacheValue(BOOL inValue) PURE_FUNC
Definition OOLogging.m:120
#define MAX_INDENT
static void LoadExplicitSettingsFromDictionary(NSDictionary *inDict)
Definition OOLogging.m:719
#define OOLOG_UNDEFINED_METACLASS
Definition OOLogging.m:61
static id ResolveDisplaySetting(NSString *inMessageClass)
Definition OOLogging.m:783
#define OOLOG_BAD_POP_INDENT
Definition OOLogging.m:64
static void OOLogInternal_(const char *inFunction, NSString *inFormat,...)
Definition OOLogging.m:610
#define OOLOG_BAD_SETTING
Definition OOLogging.m:62
void OOLogInsertMarker(void)
Definition OOLogging.m:581
NSString * OOLogGetParentMessageClass(NSString *inClass)
Definition OOLogging.m:194
static NSString *const kInheritToken
Definition OOLogging.m:94
void OOLogSetShowMessageClass(BOOL flag)
Definition OOLogging.m:519
static NSMapTable * sFileNamesCache
Definition OOLogging.m:115
static BOOL sDefaultDisplay
Definition OOLogging.m:87
static NSMutableDictionary * sDerivedSettingsCache
Definition OOLogging.m:78
#define INDENT_LOCK()
Definition OOLogging.m:282
static id ResolveMetaClassReference(NSString *inMetaClass, NSMutableSet *ioSeenMetaClasses)
Definition OOLogging.m:809
#define INDENT_UNLOCK()
Definition OOLogging.m:285
void OOLogSetShowMessageClassTemporary(BOOL flag)
Definition OOLogging.m:529
#define OOLOG_NOT_INITED
Definition OOLogging.m:58
#define INDENT_FACTOR
BOOL OOLogShowFileAndLine(void)
Definition OOLogging.m:484
void OOLogGenericParameterErrorForFunction(const char *inFunction)
Definition OOLogging.m:459
void OOLogIndent(void)
Definition OOLogging.m:345
NSDictionary * OODictionaryFromFile(NSString *path)
return nil
NSString * builtInPath()
NSDictionary * logControlDictionary()
OOLogIndentStackElement * link
Definition OOLogging.m:70