如何打印出方法名和行号,并有条件地禁止的NSLog?
2016-05-26 15:20
447 查看
转自:http://codego.net/57428/
objective-c cocoa cocoa-touch xcode nslog
我在做调试在Xcode的介绍,并希望得到有效NSLog的。 特别是,我有两个问题: 有没有办法可以轻松的NSLog的/行号? 有没有办法为“禁用”的所有NSLogs轻松地发布代码吗?
本文地址 :CodeGo.net/57428/
-------------------------------------------------------------------------------------------------------------------------
1. 以下是各地的NSLog有用的宏了很多:
#ifdef DEBUG # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #else # define DLog(...) #endif // ALog always displays output regardless of the DEBUG setting #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
该DLOG宏仅输出,当了DEBUG变量设置(-DDEBUG在项目的C标志为调试confirguration)。 考勤记录将始终输出文本(如定期的NSLog)。 输出(如考勤记录(@的“Hello world”))看起来就像这样:
-[LibraryController awakeFromNib] [Line 364] Hello world
2. 我已经采取DLOG和考勤记录从上面,并添加ULOG这引起了一
总结: DLOG将输出类似的NSLog只有当了DEBUG变量设置 考勤记录将始终输出喜欢的NSLog ULOG将显示UIAlertView只有当了DEBUG变量设置 的#ifdef DEBUG #定义DLOG(FMT,......)的NSLog((@“%s的[%d行]”FMT),__ PRETTY_FUNCTION__,__ LINE__,##__VA_ARGS__); 其他# #定义DLOG(...) #ENDIF #定义考勤记录(FMT CodeGo.net,......)的NSLog((@“%s的[%d行]”FMT),__
PRETTY_FUNCTION__,__ LINE__,##__VA_ARGS__); 的#ifdef DEBUG #定义ULOG(FMT,...){UIAlertView *警报=[[UIAlertView的alloc] initWithTitle:[NSString的stringWithFormat:@“%S \\ N [%d行]”,__ PRETTY_FUNCTION__,stringWithFormat:FMT,##__VA_ARGS__]委托:无cancelButtonTitle:@“确定”otherButtonTitles:无]
[警惕秀];} 其他# #定义ULOG(...) #ENDIF 这是什么样子: 1 Diederik
3.
NSLog(@"%s %d %s %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__);
输出文件中的行号,和函数
/proj/cocoa/cdcli/cdcli.m 121 managedObjectContext managedObjectContext
__FUNCTION__在C ++中显示错位
__PRETTY_FUNCTION__显示了在cocoa不错的函数,他们看的
我不知道什么是禁止的NSLog,我做的正确方法:
#define NSLog
没有记录输出露面,但我不知道这是否有任何副作用。
4. 在这里,一个大集合,享受调试常数。
// Uncomment the defitions to show additional info. // #define DEBUG // #define DEBUGWHERE_SHOWFULLINFO // #define DEBUG_SHOWLINES // #define DEBUG_SHOWFULLPATH // #define DEBUG_SHOWSEPARATORS // #define DEBUG_SHOWFULLINFO // Definition of DEBUG functions. Only work if DEBUG is defined. #ifdef DEBUG #define debug_separator() NSLog( @"────────────────────────────────────────────────────────────────────────────" ); #ifdef DEBUG_SHOWSEPARATORS #define debug_showSeparators() debug_separator(); #else #define debug_showSeparators() #endif /// /// /// ////// ///// #ifdef DEBUG_SHOWFULLPATH #define debug_whereFull() debug_showSeparators(); NSLog(@"Line:%d : %s : %s", __LINE__,__FILE__,__FUNCTION__); debug_showSeparators(); #else #define debug_whereFull() debug_showSeparators(); NSLog(@"Line:%d : %s : %s", __LINE__,[ [ [ [NSString alloc] initWithBytes:__FILE__ length:strlen(__FILE__) encoding:NSUTF8StringEncoding] lastPathComponent] UTF8String ] ,__FUNCTION__); debug_showSeparators(); #endif /// /// /// ////// ///// #define debugExt(args,...) debug_separator(); debug_whereFull(); NSLog( args, ##__VA_ARGS__); debug_separator(); /// /// /// ////// ///// Debug Print Macros #ifdef DEBUG_SHOWFULLINFO #define debug(args,...) debugExt(args, ##__VA_ARGS__); #else #ifdef DEBUG_SHOWLINES #define debug(args,...) debug_showSeparators(); NSLog([ NSString stringWithFormat:@"Line:%d : %@", __LINE__, args ], ##__VA_ARGS__); debug_showSeparators(); #else #define debug(args,...) debug_showSeparators(); NSLog(args, ##__VA_ARGS__); debug_showSeparators(); #endif #endif /// /// /// ////// ///// Debug Specific Types #define debug_object( arg ) debug( @"Object: %@", arg ); #define debug_int( arg ) debug( @"integer: %i", arg ); #define debug_float( arg ) debug( @"float: %f", arg ); #define debug_rect( arg ) debug( @"CGRect ( %f, %f, %f, %f)", arg.origin.x, arg.origin.y, arg.size.width, arg.size.height ); #define debug_point( arg ) debug( @"CGPoint ( %f, %f )", arg.x, arg.y ); #define debug_bool( arg ) debug( @"Boolean: %@", ( arg == YES ? @"YES" : @"NO" ) ); /// /// /// ////// ///// Debug Where Macros #ifdef DEBUGWHERE_SHOWFULLINFO #define debug_where() debug_whereFull(); #else #define debug_where() debug(@"%s",__FUNCTION__); #endif #define debug_where_separators() debug_separator(); debug_where(); debug_separator(); /// /// /// ////// ///// #else #define debug(args,...) #define debug_separator() #define debug_where() #define debug_where_separators() #define debug_whereFull() #define debugExt(args,...) #define debug_object( arg ) #define debug_int( arg ) #define debug_rect( arg ) #define debug_bool( arg ) #define debug_point( arg ) #define debug_float( arg ) #endif
5. 有一个新的把戏,没有答案给的。您
printf代替
NSLog。这会给你一个干净的日志:
同
NSLog你得到的东西是这样的:
2011-11-03 13:43:55.632 myApp[3739:207] Hello Word
但随着
printf你只能得到:
Hello World
使用这个代码
#ifdef DEBUG #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); #else #define NSLog(...) {} #endif
6. 我回答这个问题可能有帮助,看起来它是一个类似于Diederik熟了起来。您可能也想用自己的自定义日志类的静态实例替换调用的NSLog(),这样你可以添加一个优先级标志用于调试/告警/到一个文件或数据库,以及控制台,或几乎任何其他你能想到的。
#define DEBUG_MODE #ifdef DEBUG_MODE #define DebugLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) #else #define DebugLog( s, ... ) #endif
7. 禁用所有NSLogs,对于过敏的宏,这里就是你太:
void SJLog(NSString *format,...) { if(LOG) { va_list args; va_start(args,format); NSLogv(format, args); va_end(args); } }
而且,使用它几乎像的NSLog:
SJLog(@"bye bye NSLogs !");
从这个博客:
8. 上述问题的答案,它可以是一个对的NSLog在某些情况下,调试时尤其如此。举例来说,摆脱对每一行的所有日期和过程可以使输出可读性更强,更快的启动。
下面的链接提供了相当多的弹药制作简单的日志记录好得多。
9. 新增加的DLOG。而不是完全从发布应用程序移除调试,只有将其禁用。有问题,这需要调试,只是告诉如何通过电子邮件启用调试在发布应用程序并请求日志数据。
短版:创建全局变量(是的,懒惰和简单的解决方案),并修改DLOG是这样的:
BOOL myDebugEnabled = FALSE; #define DLog(fmt, ...) if (myDebugEnabled) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
在iLessons网站iLearned较长的答案:如何做动态调试日志中发布的应用
10. 大楼上面的答案之上,这里是我抄袭,并与。也记录。
#import <mach/mach.h> #ifdef DEBUG # define DebugLog(fmt, ...) NSLog((@"%s(%d) " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DebugLog(...) #endif #define AlwaysLog(fmt, ...) NSLog((@"%s(%d) " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #ifdef DEBUG # define AlertLog(fmt, ...) { \ UIAlertView *alert = [[UIAlertView alloc] \ initWithTitle : [NSString stringWithFormat:@"%s(Line: %d) ", __PRETTY_FUNCTION__, __LINE__]\ message : [NSString stringWithFormat : fmt, ##__VA_ARGS__]\ delegate : nil\ cancelButtonTitle : @"Ok"\ otherButtonTitles : nil];\ [alert show];\ } #else # define AlertLog(...) #endif #ifdef DEBUG # define DPFLog NSLog(@"%s(%d)", __PRETTY_FUNCTION__, __LINE__);//Debug Pretty Function Log #else # define DPFLog #endif #ifdef DEBUG # define MemoryLog {\ struct task_basic_info info;\ mach_msg_type_number_t size = sizeof(info);\ kern_return_t e = task_info(mach_task_self(),\ TASK_BASIC_INFO,\ (task_info_t)&info,\ &size);\ if(KERN_SUCCESS == e) {\ NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; \ [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; \ DebugLog(@"%@ bytes", [formatter stringFromNumber:[NSNumber numberWithInteger:info.resident_size]]);\ } else {\ DebugLog(@"Error with task_info(): %s", mach_error_string(e));\ }\ } #else # define MemoryLog #endif
11. 这很容易改变你现有的NSLogs显示行号和类别从他们被称为。添加一行代码到您的前缀文件:
#define NSLog(__FORMAT__, ...) NSLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 如何组织构建多文件 C 语言程序(二)
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 如何写好 C main 函数
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- C语言练习题:自由落体的小球简单实例
- 使用C语言判断英文字符大小写的方法