如何修改oc中默認的NSLog函數

有時希望獲得consolo中打印輸出能夠定位到是哪個函數中輸出的,那麼則可以修改系統默認的NSLog函數來實現這個功能。

#import <Foundation/Foundation.h>

#define NSLog(args...) _Log(@"DEBUG ", __FILE__,__LINE__,__PRETTY_FUNCTION__,args);


@interface Log : NSObject

void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...);


@end


#import "Log.h"      

@implementation Log

void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...) {
    va_list ap;
    va_start (ap, format);
    format = [format stringByAppendingString:@"\n"];
    NSString *msg = [[NSString alloc] initWithFormat:[NSString stringWithFormat:@"%@",format] arguments:ap];
    va_end (ap);
    fprintf(stderr,"%s%50s:%3d - %s",[prefix UTF8String], funcName, lineNumber, [msg UTF8String]);
}

@end

同時在項目中加入.pch文件,以前的版本中默認有.pch文件,現在的要自己加。在其中加入  #import "Log.h"  。編譯運行,就可以進行函數名,行數和信息的輸出。


參考自http://stackoverflow.com/questions/7271528/how-to-nslog-into-a-file

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章