This question already has an answer here:
这个问题已经有了答案:
- How to print out the method name and line number and conditionally disable NSLog? 13 answers
- 如何打印方法名和行号并有条件地禁用NSLog?13个答案
Say I have 10 different implementation files that are run in an chaotic order, and in each of them I have an NSLog(@"Log");
, and when I run the program I will get 10 Log
's to my console output, but how can I know which one was logged by which file? I'm searching for something like
假设我有10个不同的实现文件,它们以混乱的顺序运行,在每个文件中我都有一个NSLog(@"Log"),当我运行程序时,我的控制台输出将得到10个Log,但是我怎么知道哪个文件记录了哪个?我在找类似的东西
`In someFile1.m: Log`
`In someFile3.m: Log`
`In someFile2.m: Log`
`...`
And so on and so forth. Is that possible?
等等。这有可能吗?
2 个解决方案
#1
4
You can use preprocessor macros for that, take a look at this example:
你可以使用预处理器宏,看看这个例子:
NSLog(@"In %s - %s:%d someObject=%@", __FILE__, __func__, __LINE__, someObject);
Here's what's available: https://developer.apple.com/library/ios/qa/qa1669/_index.html
这是可用的:https://developer.apple.com/library/ios/qa/qa1669/_index.html
#2
2
You can use __FILE__
macro:
您可以使用__FILE__宏:
NSLog(@"%s",__FILE__ );
Which outputs filename:
输出文件名:
2013-10-16 20:49:17.536 ABC[3637:a0b] /Users/who/where//DeviceViewController.m
#1
4
You can use preprocessor macros for that, take a look at this example:
你可以使用预处理器宏,看看这个例子:
NSLog(@"In %s - %s:%d someObject=%@", __FILE__, __func__, __LINE__, someObject);
Here's what's available: https://developer.apple.com/library/ios/qa/qa1669/_index.html
这是可用的:https://developer.apple.com/library/ios/qa/qa1669/_index.html
#2
2
You can use __FILE__
macro:
您可以使用__FILE__宏:
NSLog(@"%s",__FILE__ );
Which outputs filename:
输出文件名:
2013-10-16 20:49:17.536 ABC[3637:a0b] /Users/who/where//DeviceViewController.m