iOS 自定义带有开关的NSLog

自定义的NSLog

代码如下:

下面代码

//  Custom_NSLog.h

//  VoiceChat

//  Created by tl on 16/4/7.

//  Copyright © 2016年 A. All rights reserved.

//  自定义Log类,外部控制Log开关#import

/**

*  自定义Log,可配置开关(用于替换NSLog)

*/

#define NSLog_Custom(format,...) CustomLog(__FUNCTION__,__LINE__,format,##__VA_ARGS__)

/**

*  自定义Log

*  @warning 外部可直接调用 NSLog_Custom

*  @param  func        方法名

*  @param  lineNumber  行号

*  @param  format      Log内容

*  @param  ...          个数可变的Log参数

*/

void CustomLog(const char *func, int lineNumber, NSString *format, ...);

@interface Custom_NSLog : NSObject

/**

*  Log 输出开关 (默认开启)

*  @param flag 是否开启 YES:显示;NO:不显示

*/

+ (void)setLogEnable:(BOOL)flag;

/**

*  是否开启了 Log 输出

*  @return Log 开关状态

*/

+ (BOOL)logEnable;

@end



下面代码

//

//  Custom_NSLog.m

//  VoiceChat

//

//  Created by tl on 16/4/7.

//  Copyright © 2016年 A. All rights reserved.

//

#import "Custom_NSLog.h"

// Log 开关状态,默认输出log信息

static BOOL Log_Switch = YES;

@implementation Custom_NSLog

void CustomLog(const char *func, int lineNumber, NSString *format, ...)

{

if ([Custom_NSLog logEnable]) {  // 开启了Log

va_list args;

va_start(args, format);

NSString *string = [[NSString alloc] initWithFormat:format arguments:args];

va_end(args);

//NSString *strFormat = [NSString stringWithFormat:@"%s, Line:%i, Log:%@",func,lineNumber,string];

NSString *strFormat = [NSString stringWithFormat:@"%@",string];

NSLog(@"%@", strFormat);

}

}

+ (BOOL)logEnable {

return Log_Switch;

}

+ (void)setLogEnable:(BOOL)flag {

Log_Switch = flag;

}

@end

你可能感兴趣的:(iOS 自定义带有开关的NSLog)