Logging.m
changeset 1 e55a17cdabd2
parent 0 d84d25d6cdbb
child 11 e5976864dfe9
     1.1 --- a/Logging.m	Sat Mar 08 21:04:41 2008 -0800
     1.2 +++ b/Logging.m	Thu Mar 20 09:05:58 2008 -0700
     1.3 @@ -23,7 +23,9 @@
     1.4  }
     1.5  
     1.6  
     1.7 -int gShouldLog = -1;
     1.8 +int _gShouldLog = -1;
     1.9 +static BOOL sConsole;
    1.10 +static NSMutableSet *sEnabledDomains;
    1.11  
    1.12  
    1.13  static BOOL isConsole( int fd )
    1.14 @@ -37,9 +39,65 @@
    1.15  }
    1.16  
    1.17  
    1.18 -static void _Logv( NSString *msg, va_list args )
    1.19 +static void InitLogging()
    1.20  {
    1.21 -    if( isConsole(STDERR_FILENO) ) {
    1.22 +    if( _gShouldLog != -1 )
    1.23 +        return;
    1.24 +
    1.25 +    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    1.26 +    _gShouldLog = NO;
    1.27 +    sEnabledDomains = [[NSMutableSet alloc] init];
    1.28 +    NSDictionary *dflts = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    1.29 +    for( NSString *key in dflts ) {
    1.30 +        if( [key hasPrefix: @"Log"] ) {
    1.31 +            BOOL value = [[NSUserDefaults standardUserDefaults] boolForKey: key];
    1.32 +            if( key.length==3 )
    1.33 +                _gShouldLog = value;
    1.34 +            else if( value )
    1.35 +                [sEnabledDomains addObject: [key substringFromIndex: 3]];
    1.36 +        }
    1.37 +    }
    1.38 +    sConsole = isConsole(STDERR_FILENO);
    1.39 +    
    1.40 +    Log(@"Logging enabled in domains: {%@}", 
    1.41 +        [[[sEnabledDomains allObjects] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)] 
    1.42 +                componentsJoinedByString: @", "]);
    1.43 +    [pool drain];
    1.44 +}
    1.45 +
    1.46 +
    1.47 +BOOL EnableLog( BOOL enable )
    1.48 +{
    1.49 +    if( _gShouldLog == -1 )
    1.50 +        InitLogging();
    1.51 +    BOOL old = _gShouldLog;
    1.52 +    _gShouldLog = enable;
    1.53 +    return old;
    1.54 +}
    1.55 +
    1.56 +BOOL _WillLogTo( NSString *domain )
    1.57 +{
    1.58 +    if( _gShouldLog == -1 )
    1.59 +        InitLogging();
    1.60 +    return _gShouldLog && [sEnabledDomains containsObject: domain];
    1.61 +}
    1.62 +
    1.63 +BOOL _EnableLogTo( NSString *domain, BOOL enable )
    1.64 +{
    1.65 +    if( _gShouldLog == -1 )
    1.66 +        InitLogging();
    1.67 +    BOOL old = [sEnabledDomains containsObject: domain];
    1.68 +    if( enable )
    1.69 +        [sEnabledDomains addObject: domain];
    1.70 +    else
    1.71 +        [sEnabledDomains removeObject: domain];
    1.72 +    return old;
    1.73 +}
    1.74 +
    1.75 +
    1.76 +static void _Logv( NSString *prefix, NSString *msg, va_list args )
    1.77 +{
    1.78 +    if( sConsole ) {
    1.79          NSAutoreleasePool *pool = [NSAutoreleasePool new];
    1.80          static NSDateFormatter *sTimestampFormat;
    1.81          if( ! sTimestampFormat ) {
    1.82 @@ -49,8 +107,10 @@
    1.83          NSDate *now = [[NSDate alloc] init];
    1.84          NSString *timestamp = [sTimestampFormat stringFromDate: now];
    1.85          [now release];
    1.86 +        NSString *separator = prefix.length ?@": " :@"";
    1.87          msg = [[NSString alloc] initWithFormat: msg arguments: args];
    1.88 -        NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@| %@\n", timestamp,msg];
    1.89 +        NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@| %@%@%@\n", 
    1.90 +                              timestamp,prefix,separator,msg];
    1.91          fputs([finalMsg UTF8String], stderr);
    1.92          [finalMsg release];
    1.93          [msg release];
    1.94 @@ -64,32 +124,42 @@
    1.95  {
    1.96      va_list args;
    1.97      va_start(args,msg);
    1.98 -    _Logv(msg,args);
    1.99 +    _Logv(@"",msg,args);
   1.100      va_end(args);
   1.101  }
   1.102  
   1.103  
   1.104  void _Log( NSString *msg, ... )
   1.105  {
   1.106 -    if( gShouldLog == -1 )
   1.107 -        gShouldLog = [[NSUserDefaults standardUserDefaults] boolForKey: @"Log"];
   1.108 -    
   1.109 -    if( gShouldLog ) {
   1.110 +    if( _gShouldLog == -1 )
   1.111 +        InitLogging();
   1.112 +    if( _gShouldLog ) {
   1.113          va_list args;
   1.114          va_start(args,msg);
   1.115 -        _Logv(msg,args);
   1.116 +        _Logv(@"",msg,args);
   1.117          va_end(args);
   1.118      }
   1.119  }
   1.120  
   1.121  
   1.122 +void _LogTo( NSString *domain, NSString *msg, ... )
   1.123 +{
   1.124 +    if( _gShouldLog == -1 )
   1.125 +        InitLogging();
   1.126 +    if( _gShouldLog && [sEnabledDomains containsObject: domain] ) {
   1.127 +        va_list args;
   1.128 +        va_start(args,msg);
   1.129 +        _Logv(domain, msg, args);
   1.130 +        va_end(args);
   1.131 +    }
   1.132 +}
   1.133 +
   1.134 +
   1.135  void Warn( NSString *msg, ... )
   1.136  {
   1.137 -    msg = [@"WARNING*** " stringByAppendingString: msg];
   1.138 -    
   1.139      va_list args;
   1.140      va_start(args,msg);
   1.141 -    _Logv(msg,args);
   1.142 +    _Logv(@"WARNING*** ",msg,args);
   1.143      va_end(args);
   1.144  }
   1.145