2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGLoggerManager.h"
23 #include "NGLogLevel.h"
25 #include "NSNull+misc.h"
28 @interface NGLoggerManager (PrivateAPI)
29 - (NGLogger *)_getConfiguredLoggerNamed:(NSString *)_name;
32 @implementation NGLoggerManager
34 static NGLoggerManager *sharedInstance = nil;
35 static NSNull *sharedNull = nil;
36 static BOOL debugAll = NO;
39 static BOOL didInit = NO;
45 sharedInstance = [[self alloc] init];
46 sharedNull = [[NSNull null] retain];
47 ud = [NSUserDefaults standardUserDefaults];
48 debugAll = [ud boolForKey:@"NGLogDebugAllEnabled"];
51 + (id)defaultLoggerManager {
52 return sharedInstance;
54 + (id)defaultManager {
55 NSLog(@"WARNING(%s): called deprecated method.", __PRETTY_FUNCTION__);
56 return [self defaultLoggerManager];
62 self->loggerMap = [[NSMutableDictionary alloc] initWithCapacity:50];
68 [self->loggerMap release];
74 - (NGLogger *)loggerForDefaultKey:(NSString *)_defaultKey {
77 logger = [self->loggerMap objectForKey:_defaultKey];
81 ud = [NSUserDefaults standardUserDefaults];
82 if (!debugAll && ![ud boolForKey:_defaultKey]) {
83 [self->loggerMap setObject:sharedNull forKey:_defaultKey];
87 logger = [self _getConfiguredLoggerNamed:_defaultKey];
88 [self->loggerMap setObject:logger forKey:_defaultKey];
91 return (logger != sharedNull) ? logger : nil;
94 - (NGLogger *)loggerForFacilityNamed:(NSString *)_name {
97 // TODO: expensive, use a faster map (at least NSMapTable)
98 if ((logger = [self->loggerMap objectForKey:_name]) != nil)
101 logger = [self _getConfiguredLoggerNamed:_name];
102 [self->loggerMap setObject:logger forKey:_name];
106 - (NGLogger *)loggerForClass:(Class)_clazz {
109 name = _clazz != Nil ? NSStringFromClass(_clazz) : (NSString *)nil;
110 return [self loggerForFacilityNamed:name];
115 - (NGLogger *)_getConfiguredLoggerNamed:(NSString *)_name {
118 configKey = [NSString stringWithFormat:@"%@LoggerConfig", _name];
119 return [NGLogger loggerWithConfigFromUserDefaults:configKey];
124 - (NSString *)description {
125 return [NSString stringWithFormat:@"<%@[0x%p] debugAll=%@ #loggers=%d>",
126 NSStringFromClass([self class]), self,
127 debugAll ? @"YES" : @"NO", [self->loggerMap count]];
130 @end /* NGLoggerManager */