Create a gist now

Instantly share code, notes, and snippets.

@markd2 /log.m
Created Jun 25, 2012

QuietLog, and a little demonstration
#import <Foundation/Foundation.h>
// clang -fobjc-arc -Wall -Wformat -Weverything -Wno-format-nonliteral -framework Foundation -o log log.m
// Compiler likes explicit function prototypes prior to first use.
// Add an attribute to get additional checking from the compiler
extern void QuietLog (NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
// !!! The attribute above isn't warning like it should, but NSLog's _is_ working.
// This is NSLog's attribute. I'm currently baffled.
// extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
void QuietLog (NSString *format, ...) {
va_list argList;
va_start (argList, format);
NSString *message = [[NSString alloc] initWithFormat: format
arguments: argList]; // autorelease if not ARC
va_end (argList);
fprintf (stderr, "%s\n", [message UTF8String]);
} // QuietLog
int main (void) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects: @"Greeble", @"Bork", @"Fnord", nil];
NSLog (@"The array is %@", array);
QuietLog (@"The array is %@", array);
// Force some warnings. These actually don't warn
NSLog (@"The array is %@", 3.1415);
QuietLog (@"The array is %@", 3.1415);
// NSLog gets a warning, but not QuietLog
NSLog (@"The array is %s", 12);
QuietLog (@"The array is %s", 12);
}
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment