Added some new utilities, taken from Murky.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "CollectionUtils.h"
14 #include <sys/param.h>
18 NSString* LOC( NSString *key ) // Localized string lookup
20 NSString *value = [[NSBundle mainBundle] localizedStringForKey:key value:nil table:nil];
22 Warn(@"No localized string for '%@' in Localizable.strings!",key);
23 value = [key uppercaseString];
38 static MYLoggingTo sLoggingTo;
39 static NSMutableSet *sEnabledDomains;
42 /** Does the file descriptor connect to console output, i.e. a terminal or Xcode? */
43 static MYLoggingTo getLoggingMode( int fd )
46 const char *term = getenv("TERM");
47 if( term && (strstr(term,"ANSI") || strstr(term,"ansi") || strstr(term,"color")) )
48 return kLoggingToColorTTY;
52 char path[MAXPATHLEN];
53 if( fcntl(fd, F_GETPATH, path) == 0 )
54 return kLoggingToFile;
56 return kLoggingToOther;
61 static void InitLogging()
63 if( _gShouldLog != -1 )
66 NSAutoreleasePool *pool = [NSAutoreleasePool new];
68 sEnabledDomains = [[NSMutableSet alloc] init];
69 NSDictionary *dflts = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
70 for( NSString *key in dflts ) {
71 if( [key hasPrefix: @"Log"] ) {
72 BOOL value = [[NSUserDefaults standardUserDefaults] boolForKey: key];
76 [sEnabledDomains addObject: [key substringFromIndex: 3]];
79 sLoggingTo = getLoggingMode(STDERR_FILENO);
81 Log(@"Logging mode %i enabled in domains: {%@}",
83 [[[sEnabledDomains allObjects] sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)]
84 componentsJoinedByString: @", "]);
89 BOOL EnableLog( BOOL enable )
91 if( _gShouldLog == -1 )
93 BOOL old = _gShouldLog;
98 BOOL _WillLogTo( NSString *domain )
100 if( _gShouldLog == -1 )
102 return _gShouldLog && [sEnabledDomains containsObject: domain];
105 BOOL _EnableLogTo( NSString *domain, BOOL enable )
107 if( _gShouldLog == -1 )
109 BOOL old = [sEnabledDomains containsObject: domain];
111 [sEnabledDomains addObject: domain];
113 [sEnabledDomains removeObject: domain];
118 #define kWarningPrefix @"\007WARNING*** "
120 #define COLOR(STR) (sLoggingTo==kLoggingToColorTTY ?@"\033["#STR"m" :@"")
123 static void _Logv( NSString *prefix, NSString *msg, va_list args )
125 if( sLoggingTo > kLoggingToOther ) {
126 NSAutoreleasePool *pool = [NSAutoreleasePool new];
127 static NSDateFormatter *sTimestampFormat;
128 if( ! sTimestampFormat ) {
129 sTimestampFormat = [[NSDateFormatter alloc] init];
130 sTimestampFormat.dateFormat = @"HH:mm:ss.SSS";
132 NSDate *now = [[NSDate alloc] init];
133 NSString *timestamp = [sTimestampFormat stringFromDate: now];
135 NSString *separator = prefix.length ?@": " :@"";
136 msg = [[NSString alloc] initWithFormat: msg arguments: args];
137 NSString *prefixColor = (prefix==kWarningPrefix) ?COLOR(91) :COLOR(93);
138 NSString *msgColor = (prefix==kWarningPrefix) ?@"" :COLOR(0);
139 NSString *finalMsg = [[NSString alloc] initWithFormat: @"%@%@| %@%@%@%@%@\n",
141 prefixColor,prefix,separator,
143 fputs([finalMsg UTF8String], stderr);
149 msg = $sprintf(@"%@: %@", prefix,msg);
155 void AlwaysLog( NSString *msg, ... )
164 void _Log( NSString *msg, ... )
166 if( _gShouldLog == -1 )
177 void _LogTo( NSString *domain, NSString *msg, ... )
179 if( _gShouldLog == -1 )
181 if( _gShouldLog && [sEnabledDomains containsObject: domain] ) {
184 _Logv(domain, msg, args);
190 void Warn( NSString *msg, ... )
194 _Logv(kWarningPrefix,msg,args);
202 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
204 Redistribution and use in source and binary forms, with or without modification, are permitted
205 provided that the following conditions are met:
207 * Redistributions of source code must retain the above copyright notice, this list of conditions
208 and the following disclaimer.
209 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
210 and the following disclaimer in the documentation and/or other materials provided with the
213 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
214 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
215 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
216 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
217 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
218 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
219 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
220 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.