用以替换系统NSLog的YouXianMingLog
这是本人自己使用并改良的用以替换系统NSLog的类,非常好用,以下是使用示例,现在开源出来并提供源码,好用的话顶一下吧^_^
效果:
YouXianMingLog.h 与 YouXianMingLog.m
//
// YouXianMingLog.h
//
// http://home.cnblogs.com/u/YouXianMing/
// https://github.com/YouXianMing
//
// Created by YouXianMing on 15/1/3.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
#define ON 1
#define OFF 0
/////////////// CONFIG /////////////////
#define NO_LOG OFF // 禁用Log
#define SWITCH_LOG ON // 切换打印
#define TIME_STAMP OFF // 时间戳
#define FILE_NAME OFF // 文件名
////////////////////////////////////////////
#if SWITCH_LOG
#if NO_LOG
#define NSLog(args...)
#else
#define NSLog(args...) ExtendNSLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args);
#endif
#endif
void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);
//
// YouXianMingLog.m
//
// http://home.cnblogs.com/u/YouXianMing/
// https://github.com/YouXianMing
//
// Created by YouXianMing on 15/1/3.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "YouXianMingLog.h"
#ifndef GCDExecOnce
#define GCDExecOnce(block) \
{ \
static dispatch_once_t predicate = 0; \
dispatch_once(&predicate, block); \
}
#endif
#define DMDEBUG_DEFAULT_DATEFORMAT @"HH:mm:ss.SSS"
static NSDateFormatter* _DMLogDateFormatter = nil;
void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...)
{
// 获取时间格式
GCDExecOnce(^{
_DMLogDateFormatter = [[NSDateFormatter alloc] init];
[_DMLogDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[_DMLogDateFormatter setDateFormat:DMDEBUG_DEFAULT_DATEFORMAT];
});
#if TIME_STAMP
// 获取当前时间戳
const char *nowCString = [[_DMLogDateFormatter stringFromDate:[NSDate date]] cStringUsingEncoding:NSUTF8StringEncoding];
// 处理format
va_list ap;
va_start (ap, format);
if (![format hasSuffix: @"\n"]) {
format = [format stringByAppendingString: @"\n"];
}
NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];
va_end (ap);
#if FILE_NAME
// 获取一些参数
NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];
// 打印
fprintf(stderr, "%s %s:%d %s", nowCString, [fileName UTF8String], lineNumber, [body UTF8String]);
#else
fprintf(stderr, "%s %s", nowCString, [body UTF8String]);
#endif
#else
// 处理format
va_list ap;
va_start (ap, format);
if (![format hasSuffix: @"\n"]) {
format = [format stringByAppendingString: @"\n"];
}
NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];
va_end (ap);
#if FILE_NAME
// 获取一些参数
NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];
// 打印
fprintf(stderr, "%s:%d %s", [fileName UTF8String], lineNumber, [body UTF8String]);
#else
fprintf(stderr, "%s", [body UTF8String]);
#endif
#endif
}
以下是配置的地方: