我正在做一个关于在Xcode中调试的演示,想要获得更多关于有效使用NSLog的信息。
我特别想问两个问题:
是否有一种方法可以轻松地NSLog当前方法的名称/行号? 在编译发布代码之前,是否有一种方法可以轻松地“禁用”所有nslog ?
我正在做一个关于在Xcode中调试的演示,想要获得更多关于有效使用NSLog的信息。
我特别想问两个问题:
是否有一种方法可以轻松地NSLog当前方法的名称/行号? 在编译发布代码之前,是否有一种方法可以轻松地“禁用”所有nslog ?
当前回答
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
并且没有日志输出显示,但是我不知道这是否有任何副作用。
其他回答
为了补充上面的答案,在某些情况下使用NSLog的替代品是非常有用的,尤其是在调试的时候。例如,删除每行上的所有日期和进程名称/id信息可以使输出更具可读性和更快地启动。
下面的链接提供了很多有用的方法,可以使简单的日志记录变得更好。
http://cocoaheads.byu.edu/wiki/a-different-nslog
我对这个问题的回答可能会有所帮助,看起来它与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
我已经从上面取了DLog和ALog,并添加了ULog,它会引发一个UIAlertView消息。
总结:
只有当DEBUG变量被设置时,DLog才会像NSLog一样输出 ALog总是像NSLog一样输出 只有当DEBUG变量被设置时,ULog才显示UIAlertView
#ifdef DEBUG # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DLog(...) #endif #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #ifdef DEBUG # define ULog(fmt, ...) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } #else # define ULog(...) #endif
这是它的样子:
+ 1 Diederik
这是一个没有答案的新把戏。你可以用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
在以上答案的基础上,以下是我抄袭并想出的答案。还添加了内存日志记录。
#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