如何打印出方法名和行号,并有条件地禁止的NSLog?

34 篇文章 0 订阅
20 篇文章 0 订阅
 

转自: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__)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值