当我将[<object> description]作为参数</object>传递时,自定义NSLog会生成错误

时间:2014-04-22 04:11:14

标签: ios nslog

我已按照How to NSLog into a file创建自己的NSLog。

在iFob-Prefix.pch中:

    #import "Log.h"
    #ifdef DEBUG_MODE
    #define NSLog( args, ... )      _Log(@"Prefixs", __FILE__, __LINE__, __PRETTY_FUNCTION__, args);
    #else
    #define NSLog( args, ... )  
Log.h中的

        @interface Log : NSObject
        void _Log(NSString *prefix, const char *file, int lineNumber, const char *funcName, NSString *format,...);

        @end
Log.m中的

#import "Log.h"
#import <objc/runtime.h>
#define PADDING_TABS 10

@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];

    msg = formatJsonData(msg);

    va_end (ap);
    fprintf(stderr,"\n%s[%30s:%4d] - %s",[prefix UTF8String], [[[NSString stringWithUTF8String:file] lastPathComponent]UTF8String] , lineNumber, [msg UTF8String]);
    append(msg);

}

每次通过[&lt;“object”&gt;时我都会收到错误说明]进入日志。 像:

    NSManagedObjectContext *context = rasEngine.managedObjectContext ;
    NSLog(@"context: %@",[context description])

错误:  error message

1 个答案:

答案 0 :(得分:1)

NSLog( args, ... )更改为NSLog( args... )

否则你在调用NSLog(@"content %@");之类的东西而不传递对象