iPhone Code Schnipsel: Erweitertes NSLog
Gegeben
Diverse Aufrufe von NSLog innerhalb eines iPhone-Projektes, z.B.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSLog(@"App did finish launching.");
// ...
Gesucht
In der Debug-Konfiguration soll zusätzlich Dateiname und Zeilennummer ausgegeben werden, in der Release-Konfiguration soll hingegen die Ausgabe deaktiviert werden.
Lösung
Inspiriert durch John Muchows Blogpost Filename and Line Number with NSLog: Part II und wie folgt abgewandelt:
LogHelper.h:#if DEBUG
#define CMLog(format, ...) [LogHelper logWithPath:__FILE__ line:__LINE__ string:(format), ## __VA_ARGS__]
#else
#define CMLog(format, ...)
#endif
@interface LogHelper : NSObject {
}
+ (void)logWithPath:(char *)path line:(NSUInteger)line string:(NSString *)format, ...;
LogHelper.m:#import "LogHelper.h"
@implementation LogHelper
+ (void)logWithPath:(char *)path line:(NSUInteger)line string:(NSString *)format, ... {
NSString *pathString = [[NSString alloc] initWithBytes:path
length:strlen(path)
encoding:NSUTF8StringEncoding];
va_list argList;
va_start(argList, format);
NSString *formattedString = [[NSString alloc] initWithFormat:format
arguments:argList];
va_end(argList);
NSLog([NSString stringWithFormat:@"%@ (%d): %@",
[pathString lastPathComponent],
line,
formattedString]);
[formattedString release];
}
@end
Unter Target > AppName > Get Info ist dann im Reiter Build für die Konfiguration Debug die Einstellung Other C Flags mit "-DDEBUG" zu ergänzen und im Code NSLog durch CMLog zu ersetzen.
Schlüsselwörter: iphone, programmierung, schnipsel
Schlüsselwörter
- berlin (2)
- blog (5)
- browser (2)
- cocoaheads (5)
- dropbox (1)
- git (7)
- idisk (1)
- iphone (28)
- javascript (2)
- kurztip (4)
- linktips (17)
- mac (9)
- macruby (1)
- objective-c (8)
- ortung (1)
- programmierung (22)
- rails (1)
- railsconf (7)
- ruby (6)
- ruby on rails (7)
- schnipsel (14)
- server (2)
- spiele (1)
- statistiken (3)
- stuttgart (3)
- testen (4)
- tidy (1)
- versionskontrolle (5)
- wwdc (1)
- xcode (9)
- xml (1)
Kommentare