From 26b0832e1290e9581e7305ec6dd7123a2aa8dc8d Mon Sep 17 00:00:00 2001 From: helge Date: Fri, 19 Nov 2004 02:59:04 +0000 Subject: [PATCH] fixed bugs in logger, improved performance git-svn-id: http://svn.opengroupware.org/SOPE/trunk@400 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-core/NGExtensions/ChangeLog | 10 +++ .../FdExt.subproj/NSObject+Logs.m | 8 ++- .../NGExtensions/NGExtensions/NGLogger.h | 1 + .../NGExtensions/NGLogging.subproj/ChangeLog | 9 +++ .../NGLogging.subproj/NGLogAppender.m | 2 +- .../NGLogging.subproj/NGLogConsoleAppender.m | 62 ++++++++++++++++++- .../NGExtensions/NGLogging.subproj/NGLogger.m | 42 +++++++++---- .../NGLogging.subproj/NGLoggerManager.m | 15 ++--- sope-core/NGExtensions/Version | 2 +- 9 files changed, 125 insertions(+), 26 deletions(-) diff --git a/sope-core/NGExtensions/ChangeLog b/sope-core/NGExtensions/ChangeLog index 5ebd4ecc..79692e2a 100644 --- a/sope-core/NGExtensions/ChangeLog +++ b/sope-core/NGExtensions/ChangeLog @@ -1,3 +1,13 @@ +2004-11-19 Helge Hess + + * v4.5.135 + + * FdExt.subproj/NSObject+Logs.m: fixed a bug in default logger creation + (incorrect static variable) + + * NGLogging: fixed bug in console appender, increased speed, avoid + different logger objects for each class + 2004-11-18 Marcus Mueller * v4.5.134 diff --git a/sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m b/sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m index f1091470..a64f4eec 100644 --- a/sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m +++ b/sope-core/NGExtensions/FdExt.subproj/NSObject+Logs.m @@ -47,9 +47,13 @@ static inline Class NSStringClass(void) { if (logger == nil) { NGLoggerManager *lm; - // TODO: broken. If [self class] is passed in, 'logger' can't be a static - lm = [NGLoggerManager defaultLoggerManager]; + lm = [NGLoggerManager defaultLoggerManager]; + +#if 0 // broken: if [self class] is passed in, 'logger' can't be a static logger = [lm loggerForClass:[self class]]; +#else + logger = [lm loggerForClass:[NSObject class]]; +#endif } return logger; } diff --git a/sope-core/NGExtensions/NGExtensions/NGLogger.h b/sope-core/NGExtensions/NGExtensions/NGLogger.h index da894ebb..9ddb2de6 100644 --- a/sope-core/NGExtensions/NGExtensions/NGLogger.h +++ b/sope-core/NGExtensions/NGExtensions/NGLogger.h @@ -48,6 +48,7 @@ id _appender; // going away as soon as we have a config } ++ (id)defaultLogger; - (id)initWithLogLevel:(NGLogLevel)_level; /* accessors */ diff --git a/sope-core/NGExtensions/NGLogging.subproj/ChangeLog b/sope-core/NGExtensions/NGLogging.subproj/ChangeLog index 469c509b..5a13bd65 100644 --- a/sope-core/NGExtensions/NGLogging.subproj/ChangeLog +++ b/sope-core/NGExtensions/NGLogging.subproj/ChangeLog @@ -1,3 +1,12 @@ +2004-11-19 Helge Hess + + * NGLoggerManager.m: use default logger if none is registered + + * NGLogger.m: cleaned up -init, added default logger + + * NGLogConsoleAppender.m: removed the bug with using a message in + place of a format, rewrote logger to be sufficiently fast + 2004-11-18 Marcus Mueller * NGLogLevel.h: new header bearing the log levels diff --git a/sope-core/NGExtensions/NGLogging.subproj/NGLogAppender.m b/sope-core/NGExtensions/NGLogging.subproj/NGLogAppender.m index 4ae5e3a7..324315ea 100644 --- a/sope-core/NGExtensions/NGLogging.subproj/NGLogAppender.m +++ b/sope-core/NGExtensions/NGLogging.subproj/NGLogAppender.m @@ -38,7 +38,7 @@ - (NSString *)formattedEvent:(NGLogEvent *)_event { NSMutableString *fe; NSString *lvl; - + lvl = [self localizedNameOfLogLevel:[_event level]]; fe = [NSMutableString stringWithCapacity:128]; if (lvl != nil) { diff --git a/sope-core/NGExtensions/NGLogging.subproj/NGLogConsoleAppender.m b/sope-core/NGExtensions/NGLogging.subproj/NGLogConsoleAppender.m index ad624a25..645dea4c 100644 --- a/sope-core/NGExtensions/NGLogging.subproj/NGLogConsoleAppender.m +++ b/sope-core/NGExtensions/NGLogging.subproj/NGLogConsoleAppender.m @@ -21,12 +21,70 @@ #include "NGLogConsoleAppender.h" #include "NGLogEvent.h" +#include "common.h" +#include +#include @implementation NGLogConsoleAppender +static Class NSCalendarDateClass = Nil; +static Class NSProcessInfoClass = Nil; +static unsigned char *processName = NULL; + +static char *monthNames[14] = { + "Dec", + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + "Jan" +}; + ++ (void)initialize { + if (NSCalendarDateClass == Nil) + NSCalendarDateClass = [NSCalendarDate class]; + if (NSProcessInfoClass == Nil) + NSProcessInfoClass = [NSProcessInfo class]; + + if (processName == NULL) { + /* process name can't change, right? */ + unsigned len; + NSString *pn; + + pn = [[NSProcessInfoClass processInfo] processName]; + len = [pn cStringLength]; + + processName = malloc(len + 4); + [pn getCString:processName]; + } +} + +static __inline__ unsigned char * levelPrefixForEvent(NGLogEvent *_event) { + switch ([_event level]) { + case NGLogLevelWarn: return "[WARN]"; + case NGLogLevelError: return "[ERROR]"; + case NGLogLevelFatal: return "[FATAL]"; + default: return ""; + } +} + - (void)appendLogEvent:(NGLogEvent *)_event { - // TODO: broken, NSLog takes a format string - NSLog([self formattedEvent:_event]); + /* Note: pid can change after a fork() */ + NSCalendarDate *date; + + // TODO: get time using libc function, cheaper + // TODO: set to GMT? + date = [[NSCalendarDateClass alloc] init]; + + // TODO: cString for message is expensive + fprintf(stderr, + "%s %02i %02i:%02i:%02i %s [%d]: %s\n", + monthNames[[date monthOfYear]], + [date dayOfMonth], + [date hourOfDay], [date minuteOfHour], [date secondOfMinute], + processName, + getpid(), + [[_event message] cString]); + + [date release]; } @end /* NGLogConsoleAppender */ diff --git a/sope-core/NGExtensions/NGLogging.subproj/NGLogger.m b/sope-core/NGExtensions/NGLogging.subproj/NGLogger.m index 23105a94..fd746406 100644 --- a/sope-core/NGExtensions/NGLogging.subproj/NGLogger.m +++ b/sope-core/NGExtensions/NGLogging.subproj/NGLogger.m @@ -41,28 +41,44 @@ static Class NSStringClass = Nil; NSStringClass = [NSString class]; } ++ (id)defaultLogger { + static NGLogger *logger = nil; // THREAD + + if (logger == nil) + logger = [[self alloc] init]; + return logger; +} + - (id)init { return [self initWithLogLevel:NGLogLevelAll]; } -- (id)initWithLogLevel:(NGLogLevel)_level { - self = [super init]; - if (self) { - NSUserDefaults *ud; - NSString *appenderClassName; - +- (id)initWithLogLevel:(NGLogLevel)_level appender:(id)_appenderParam { + if ((self = [super init]) != nil) { [self setLogLevel:_level]; - - // TODO: remove this as soon as we have a config - ud = [NSUserDefaults standardUserDefaults]; - appenderClassName = [ud stringForKey:@"NGLogDefaultAppenderClass"]; - if (appenderClassName == nil) - appenderClassName = @"NGLogConsoleAppender"; - self->_appender = [[NSClassFromString(appenderClassName) alloc] init]; + self->_appender = [_appenderParam retain]; // TODO: fix ivar name } return self; } +- (id)initWithLogLevel:(NGLogLevel)_level { + NSUserDefaults *ud; + NSString *appenderClassName; + id appender; + + // TODO: remove this as soon as we have a config + ud = [NSUserDefaults standardUserDefaults]; + appenderClassName = [ud stringForKey:@"NGLogDefaultAppenderClass"]; + if (appenderClassName == nil) + appenderClassName = @"NGLogConsoleAppender"; + + appender = [[NSClassFromString(appenderClassName) alloc] init]; + + self = [self initWithLogLevel:_level appender:appender]; + [appender release]; + return self; +} + - (void)dealloc { [self->_appender release]; [super dealloc]; diff --git a/sope-core/NGExtensions/NGLogging.subproj/NGLoggerManager.m b/sope-core/NGExtensions/NGLogging.subproj/NGLoggerManager.m index e05f97c7..550ed3df 100644 --- a/sope-core/NGExtensions/NGLogging.subproj/NGLoggerManager.m +++ b/sope-core/NGExtensions/NGLogging.subproj/NGLoggerManager.m @@ -87,19 +87,20 @@ static NSNull *sharedNull; - (NGLogger *)loggerForFacilityNamed:(NSString *)_name { id logger; - logger = [self->loggerMap objectForKey:_name]; - if (!logger) { - logger = [[NGLogger alloc] init]; - [self->loggerMap setObject:logger forKey:_name]; - [logger release]; - } + // TODO: expensive, use a faster map (at least NSMapTable) + if ((logger = [self->loggerMap objectForKey:_name]) != nil) + return logger; + + // TODO: is a registration really faster? I guess not? + logger = [NGLogger defaultLogger]; /* fallback to shared logger */ + [self->loggerMap setObject:logger forKey:_name]; return logger; } - (NGLogger *)loggerForClass:(Class)_clazz { NSString *name; - name = NSStringFromClass(_clazz); + name = _clazz != Nil ? NSStringFromClass(_clazz) : nil; return [self loggerForFacilityNamed:name]; } diff --git a/sope-core/NGExtensions/Version b/sope-core/NGExtensions/Version index 93b5fd83..38af7b35 100644 --- a/sope-core/NGExtensions/Version +++ b/sope-core/NGExtensions/Version @@ -1,6 +1,6 @@ # version -SUBMINOR_VERSION:=134 +SUBMINOR_VERSION:=135 # v4.3.115 requires libFoundation v1.0.59 # v4.2.72 requires libEOControl v4.2.39 -- 2.39.5