ios nslog 例子_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值