.h
enum LOGTYPE
{
LOGTYPE_INFO = 0,
LOGTYPE_ERROR,
};
@interface Log : NSObject
+(void)log:(int)logtype file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format,...;
@end
.mm
@implementation Log
+(void)log:(int)logtype file:(char*)sourceFile function:(char*)functionName lineNumber:(int)lineNumber format:(NSString*)format,...
{
va_list ap;
NSString *print, *file, *function;
va_start(ap,format);
file = [[NSString alloc] initWithBytes: sourceFile length: strlen(sourceFile) encoding: NSUTF8StringEncoding];
function = [NSString stringWithCString: functionName];
print = [[NSString alloc] initWithFormat: format arguments: ap];
va_end(ap);
NSString* type = nil;
if (logtype == LOGTYPE_INFO) {
type = [NSString stringWithFormat:@"INFO"];
}
else
{
type = [NSString stringWithFormat:@"ERROR"];
}
NSLog(@"[%@] [%@] [%@] [NO.%d] %@", type,[file lastPathComponent], function, lineNumber, print);
}
@end
测试代码
log(@"%d %@",1,@"123");
NSString* text = @"12322";
LOGINFO(@"%@ %@",@"1231",text);