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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • http://www.open-open.com/lib/view/open1390651437117.html ...
    Xtuphe阅读 1,285评论 0 10
  • 宏定义在C系开发中可以说占有举足轻重的作用。底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可...
    你好自己阅读 1,061评论 0 5
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,143评论 1 6
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,500评论 0 12
  • iOS开发过程中,使用的一些常用宏定义 字符串是否为空#define kStringIsEmpty(str) ([...
    goyohol阅读 5,391评论 30 85