/*!
@header ECLogging
@abstract Module of Encore
@availability OS X, GNUstep
@copyright 2004, 2005, 2006 Oliver Langer
Author: Oliver Langer
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------
Modification history
16.01.06 ola initial version
22.08.06 ola license changed
-------------------------------------------------------------------------
*/
#include
#include
#include
#include
#include
#include
#include
static ECLogging *globalLogging = nil;
static NSLock *_lock;
@implementation ECLogging
+ initialize {
[super initialize];
_lock = [[NSLock alloc] init];
if( nil == globalLogging ) {
globalLogging = [[ECLogging alloc] init];
}
return self;
}
+ (ECLogger *) loggerForContext: (NSString *) aContext {
return [[ECLogging instance] loggerForContext: aContext];
}
+ (ECLogging *) instance {
[_lock lock];
if( nil == globalLogging ) {
globalLogging = [[ECLogging alloc] init];
}
[_lock unlock];
return globalLogging;
}
- init {
if( nil != globalLogging ) {
return globalLogging;
}
self = [super init];
self->contextToLogger = [[NSMutableDictionary alloc] init];
self->rootLogger = nil;
return self;
}
- (void) dealloc {
[self->contextToLogger release];
[self->rootLogger release];
[_lock lock];
globalLogging = nil;
[_lock unlock];
[super dealloc];
}
- addRootLoggingConfiguration: (ECLoggingConfiguration *) aConfiguration {
return [self addLoggingConfiguration: aConfiguration forContext: nil];
}
- addLoggingConfiguration: (ECLoggingConfiguration *) aConfiguration
forContext: (NSString *) aContext {
NSArray *allContexts;
ECLogger *logger;
int i;
if( nil == aContext ) {
self->rootLogger = [[aConfiguration logger] retain];
return self;
}
EC_AUTORELEASEPOOL_BEGIN
logger = [aConfiguration logger];
allContexts = [self createAllSupercontextsOf: aContext];
for( i = 0; i < [allContexts count]; i++ ) {
[self->contextToLogger
setObject: logger
forKey: [allContexts objectAtIndex: i]];
}
[self->contextToLogger
setObject: logger
forKey: aContext];
EC_AUTORELEASEPOOL_END
return self;
}
- (NSArray *) createAllSupercontextsOf: (NSString *) aContextStr {
NSArray *toReturn = [[[NSMutableArray alloc] init] autorelease];
NSArray *substrings;
NSMutableString *context = nil;
int i;
substrings = [aContextStr splitStringUsingDelimiter: @"."];
for( i = 0; i < (int) ([substrings count]-2); i++) {
if( nil == context ) {
context = [[[NSMutableString alloc] init] autorelease];
} else {
[context appendString: @"."];
}
[context appendString: [substrings objectAtIndex: i]];
}
return toReturn;
}
- (ECLogger *) loggerForContext: (NSString *) aContext {
ECLogger *logger = [self->contextToLogger objectForKey: aContext];
return (nil == logger) ? rootLogger : logger;
}
@end