diff -r e55a17cdabd2 -r e5976864dfe9 Logging.m --- a/Logging.m Thu Mar 20 09:05:58 2008 -0700 +++ b/Logging.m Sat May 24 13:24:33 2008 -0700 @@ -7,9 +7,12 @@ // #import "Logging.h" -#import +#import "CollectionUtils.h" + +#include #include #include +#include NSString* LOC( NSString *key ) // Localized string lookup @@ -23,19 +26,35 @@ } +typedef enum { + kLoggingToOther, + kLoggingToFile, + kLoggingToTTY, + kLoggingToColorTTY +} MYLoggingTo; + + int _gShouldLog = -1; -static BOOL sConsole; +static MYLoggingTo sLoggingTo; static NSMutableSet *sEnabledDomains; -static BOOL isConsole( int fd ) +/** Does the file descriptor connect to console output, i.e. a terminal or Xcode? */ +static MYLoggingTo getLoggingMode( int fd ) { - if( isatty(fd) ) - return YES; - char path[MAXPATHLEN]; - if( fcntl(fd, F_GETPATH, path) == -1 ) - return NO; - return YES; + if( isatty(fd) ) { + const char *term = getenv("TERM"); + if( term && (strstr(term,"ANSI") || strstr(term,"ansi") || strstr(term,"color")) ) + return kLoggingToColorTTY; + else + return kLoggingToTTY; + } else { + char path[MAXPATHLEN]; + if( fcntl(fd, F_GETPATH, path) == 0 ) + return kLoggingToFile; + else + return kLoggingToOther; + } } @@ -57,9 +76,10 @@ [sEnabledDomains addObject: [key substringFromIndex: 3]]; } } - sConsole = isConsole(STDERR_FILENO); + sLoggingTo = getLoggingMode(STDERR_FILENO); - Log(@"Logging enabled in domains: {%@}", + Log(@"Logging mode %i enabled in domains: {%@}", + sLoggingTo, [[[sEnabledDomains allObjects] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)] componentsJoinedByString: @", "]); [pool drain]; @@ -95,9 +115,14 @@ } +#define kWarningPrefix @"\007WARNING*** " + +#define COLOR(STR) (sLoggingTo==kLoggingToColorTTY ?@"\033["#STR"m" :@"") + + static void _Logv( NSString *prefix, NSString *msg, va_list args ) { - if( sConsole ) { + if( sLoggingTo > kLoggingToOther ) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; static NSDateFormatter *sTimestampFormat; if( ! sTimestampFormat ) { @@ -109,14 +134,21 @@ [now release]; NSString *separator = prefix.length ?@": " :@""; msg = [[NSString alloc] initWithFormat: msg arguments: args]; - NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@| %@%@%@\n", - timestamp,prefix,separator,msg]; + NSString *prefixColor = (prefix==kWarningPrefix) ?COLOR(91) :COLOR(93); + NSString *msgColor = (prefix==kWarningPrefix) ?@"" :COLOR(0); + NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@%@| %@%@%@%@%@\n", + COLOR(30),timestamp, + prefixColor,prefix,separator, + msgColor,msg]; fputs([finalMsg UTF8String], stderr); [finalMsg release]; [msg release]; [pool drain]; - } else + } else { + if( prefix.length ) + msg = $sprintf(@"%@: %@", prefix,msg); NSLogv(msg,args); + } } @@ -159,8 +191,31 @@ { va_list args; va_start(args,msg); - _Logv(@"WARNING*** ",msg,args); + _Logv(kWarningPrefix,msg,args); va_end(args); } + + +/* + Copyright (c) 2008, Jens Alfke . All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, are permitted + provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */