ios自定义NSLog的输出内容
2015-06-29 13:23
399 查看
本文章转发自:http://my.oschina.net/juwenz/blog/178781
数据可能像这样
我们希望输出更多的内容,比如文件名,方法名,代码行数等,简单来说就像下面这种格式
打开ExtendNSLogFunctionality.h,加入下面代码
3.打开ExtendNSLogFunctionality.m , 加入下面代码
4.把ExtendNSLogFunctionality.h 加入到Prefix.pch 的OBJCsection中
问题
NSLog输出的信息很少,格式如下Date Time OurApp[<processid>] NSLog output
数据可能像这样
20131111 12:35:35.025 TestApp[460:c07] Hello world
我们希望输出更多的内容,比如文件名,方法名,代码行数等,简单来说就像下面这种格式
(ClassName MethodName) (SourceFileName:LineNumber NSLog output
解决方法
步骤
新建一个类,命名为ExtendNSLogFunctionality;打开ExtendNSLogFunctionality.h,加入下面代码
#ifdef DEBUG #define NSLog(args...) ExtendNSLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args); #else #define NSLog(x...) #endif void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);
3.打开ExtendNSLogFunctionality.m , 加入下面代码
void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...) { // Type to hold information about variable arguments. va_list ap; // Initialize a variable argument list. va_start (ap, format); // NSLog only adds a newline to the end of the NSLog format if // one is not already there. // Here we are utilizing this feature of NSLog() if (![format hasSuffix: @"\n"]) { format = [format stringByAppendingString: @"\n"]; } NSString *body = [[NSString alloc] initWithFormat:format arguments:ap]; // End using variable argument list. va_end (ap); NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent]; fprintf(stderr, "(%s) (%s:%d) %s", functionName, [fileName UTF8String], lineNumber, [body UTF8String]); }
4.把ExtendNSLogFunctionality.h 加入到Prefix.pch 的OBJCsection中
#ifdef __OBJC__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import "ExtendNSLogFunctionality.h" #endif
测试结果
在某个文件的某个方法中NSLog了一下,结果如下(-[TestNSLogVC resetSize]) (TestNSLogVC.m:78) height : 460.000000 width:320.000000
相关文章推荐
- ios的@property属性和@synthesize属性
- ios内存管理2-对象之间的内存管理
- ios内存管理1-retain和release的简单使用
- ios学习中对于懒加载的个人理解
- ios学习中懒加载的个人理解
- iOs画图 划线的三种方式
- ios画图 划线的两种方式
- iOS IAP恢复
- iOS 隐藏状态栏
- iOS画图 圆形裁剪
- iOS画图 上下文栈的使用
- iOS 对相机及相册的操作
- iOS开发之Xcode6之后不再自动创建Pch预编译文件(解决unknown type name........)
- 画1像素的线
- iOS 如何 理解 代理
- IOS开发---获取用户当前地理坐标(最新)
- IOS调试技巧——异常断点
- iOS: 如何正确的绘制1像素的线
- 如何让iOS模拟器也能测试蓝牙4.0程序?
- 细数iOS上那些你绝对不会注意到的神细节