diff -r d84d25d6cdbb -r e55a17cdabd2 Logging.m --- a/Logging.m Sat Mar 08 21:04:41 2008 -0800 +++ b/Logging.m Thu Mar 20 09:05:58 2008 -0700 @@ -23,7 +23,9 @@ } -int gShouldLog = -1; +int _gShouldLog = -1; +static BOOL sConsole; +static NSMutableSet *sEnabledDomains; static BOOL isConsole( int fd ) @@ -37,9 +39,65 @@ } -static void _Logv( NSString *msg, va_list args ) +static void InitLogging() { - if( isConsole(STDERR_FILENO) ) { + if( _gShouldLog != -1 ) + return; + + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + _gShouldLog = NO; + sEnabledDomains = [[NSMutableSet alloc] init]; + NSDictionary *dflts = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; + for( NSString *key in dflts ) { + if( [key hasPrefix: @"Log"] ) { + BOOL value = [[NSUserDefaults standardUserDefaults] boolForKey: key]; + if( key.length==3 ) + _gShouldLog = value; + else if( value ) + [sEnabledDomains addObject: [key substringFromIndex: 3]]; + } + } + sConsole = isConsole(STDERR_FILENO); + + Log(@"Logging enabled in domains: {%@}", + [[[sEnabledDomains allObjects] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)] + componentsJoinedByString: @", "]); + [pool drain]; +} + + +BOOL EnableLog( BOOL enable ) +{ + if( _gShouldLog == -1 ) + InitLogging(); + BOOL old = _gShouldLog; + _gShouldLog = enable; + return old; +} + +BOOL _WillLogTo( NSString *domain ) +{ + if( _gShouldLog == -1 ) + InitLogging(); + return _gShouldLog && [sEnabledDomains containsObject: domain]; +} + +BOOL _EnableLogTo( NSString *domain, BOOL enable ) +{ + if( _gShouldLog == -1 ) + InitLogging(); + BOOL old = [sEnabledDomains containsObject: domain]; + if( enable ) + [sEnabledDomains addObject: domain]; + else + [sEnabledDomains removeObject: domain]; + return old; +} + + +static void _Logv( NSString *prefix, NSString *msg, va_list args ) +{ + if( sConsole ) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; static NSDateFormatter *sTimestampFormat; if( ! sTimestampFormat ) { @@ -49,8 +107,10 @@ NSDate *now = [[NSDate alloc] init]; NSString *timestamp = [sTimestampFormat stringFromDate: now]; [now release]; + NSString *separator = prefix.length ?@": " :@""; msg = [[NSString alloc] initWithFormat: msg arguments: args]; - NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@| %@\n", timestamp,msg]; + NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@| %@%@%@\n", + timestamp,prefix,separator,msg]; fputs([finalMsg UTF8String], stderr); [finalMsg release]; [msg release]; @@ -64,32 +124,42 @@ { va_list args; va_start(args,msg); - _Logv(msg,args); + _Logv(@"",msg,args); va_end(args); } void _Log( NSString *msg, ... ) { - if( gShouldLog == -1 ) - gShouldLog = [[NSUserDefaults standardUserDefaults] boolForKey: @"Log"]; - - if( gShouldLog ) { + if( _gShouldLog == -1 ) + InitLogging(); + if( _gShouldLog ) { va_list args; va_start(args,msg); - _Logv(msg,args); + _Logv(@"",msg,args); va_end(args); } } +void _LogTo( NSString *domain, NSString *msg, ... ) +{ + if( _gShouldLog == -1 ) + InitLogging(); + if( _gShouldLog && [sEnabledDomains containsObject: domain] ) { + va_list args; + va_start(args,msg); + _Logv(domain, msg, args); + va_end(args); + } +} + + void Warn( NSString *msg, ... ) { - msg = [@"WARNING*** " stringByAppendingString: msg]; - va_list args; va_start(args,msg); - _Logv(msg,args); + _Logv(@"WARNING*** ",msg,args); va_end(args); }