]> err.no Git - sope/blob - sope-core/NGExtensions/NGLogging.subproj/NGLoggerManager.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGLogging.subproj / NGLoggerManager.m
1 /*
2   Copyright (C) 2004-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "NGLoggerManager.h"
23 #include "NGLogLevel.h"
24 #include "NGLogger.h"
25 #include "NSNull+misc.h"
26 #include "common.h"
27
28 @interface NGLoggerManager (PrivateAPI)
29 - (NGLogger *)_getConfiguredLoggerNamed:(NSString *)_name;
30 @end
31
32 @implementation NGLoggerManager
33
34 static NGLoggerManager *sharedInstance = nil;
35 static NSNull          *sharedNull     = nil;
36 static BOOL            debugAll        = NO;
37
38 + (void)initialize {
39   static BOOL    didInit = NO;
40   NSUserDefaults *ud;
41
42   if (didInit) return;
43   
44   didInit         = YES;
45   sharedInstance  = [[self alloc] init];
46   sharedNull      = [[NSNull null] retain];
47   ud              = [NSUserDefaults standardUserDefaults];
48   debugAll        = [ud boolForKey:@"NGLogDebugAllEnabled"];
49 }
50
51 + (id)defaultLoggerManager {
52   return sharedInstance;
53 }
54 + (id)defaultManager {
55   NSLog(@"WARNING(%s): called deprecated method.", __PRETTY_FUNCTION__);
56   return [self defaultLoggerManager];
57 }
58
59 - (id)init {
60   self = [super init];
61   if (self) {
62     self->loggerMap = [[NSMutableDictionary alloc] initWithCapacity:50];
63   }
64   return self;
65 }
66
67 - (void)dealloc {
68   [self->loggerMap release];
69   [super dealloc];
70 }
71
72 /* operations */
73
74 - (NGLogger *)loggerForDefaultKey:(NSString *)_defaultKey {
75   id logger;
76
77   logger = [self->loggerMap objectForKey:_defaultKey];
78   if (!logger) {
79     NSUserDefaults *ud;
80
81     ud = [NSUserDefaults standardUserDefaults];
82     if (!debugAll && ![ud boolForKey:_defaultKey]) {
83       [self->loggerMap setObject:sharedNull forKey:_defaultKey];
84       logger = sharedNull;
85     }
86     else {
87       logger = [self _getConfiguredLoggerNamed:_defaultKey];
88       [self->loggerMap setObject:logger forKey:_defaultKey];
89     }
90   }
91   return (logger != sharedNull) ? logger : nil;
92 }
93
94 - (NGLogger *)loggerForFacilityNamed:(NSString *)_name {
95   NGLogger *logger;
96   
97   // TODO: expensive, use a faster map (at least NSMapTable)
98   if ((logger = [self->loggerMap objectForKey:_name]) != nil)
99     return logger;
100
101   logger = [self _getConfiguredLoggerNamed:_name];
102   [self->loggerMap setObject:logger forKey:_name];
103   return logger;
104 }
105
106 - (NGLogger *)loggerForClass:(Class)_clazz {
107   NSString *name;
108   
109   name = _clazz != Nil ? NSStringFromClass(_clazz) : (NSString *)nil;
110   return [self loggerForFacilityNamed:name];
111 }
112
113 /* Private */
114
115 - (NGLogger *)_getConfiguredLoggerNamed:(NSString *)_name {
116   NSString *configKey;
117   
118   configKey = [NSString stringWithFormat:@"%@LoggerConfig", _name];
119   return [NGLogger loggerWithConfigFromUserDefaults:configKey];
120 }
121
122 /* description */
123
124 - (NSString *)description {
125   return [NSString stringWithFormat:@"<%@[0x%p] debugAll=%@ #loggers=%d>",
126                      NSStringFromClass([self class]), self,
127                      debugAll ? @"YES" : @"NO", [self->loggerMap count]];
128 }
129
130 @end /* NGLoggerManager */