使用Objective-C中的宏来记录函数名和行号

我有以下简化的代码示例:
// in .h file
// define a macro for partial method
#define debugWithMsg context:(CFStringRef)__FUNCTION__ lineNumber:__LINE__ debug:

@interface MyLogger : NSObject {
  ...
}

- (void) context:(CFStringRef)function 
      lineNumber:(int)line 
           debug:(NSString*)messageFormat, ...;
@end
我在其他类中使用此方法将调试消息打印到XCode控制台。这是一个测试我的调试方法的例子(在带有表视图的MyViewController类中):
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
  ...
  // myLogger is an instance of MyLogger.
  [myLogger debugWithMsg@"something for %@", @"testing!"];
  ...
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  ...
  [myLogger debugWithMsg@"something for %@", @"another testing!"];
  ...
}
上面的例子效果很好;但是,对我来说,它看起来不像标准的C / C ++或ObjC语法。我需要使用这个宏的原因是我想将前两个参数作为“常量”传递给方法。实际上,这两个参数也是宏:__FUNCTION__是C ++宏,__LINE__是C标准宏。这两个特殊宏将动态映射到函数的字符串和调用方法的行号。例如,上面的调试方法调用在XCode控制台中打印一个msg,如下所示:
[timestamp] -[MyViewController tableView:numberOfRowsInSection:] line:107 - something for testing!
...
[timestamp] -[MyViewController tableView:cellForRowAtIndexPath:] line:124 - something for another testing!
MyLogger类主要供我内部使用。有没有其他更好的做法来获得相同的结果?或者这个实现有什么问题吗? 我想以与NSLog(fmt,...)相同的方式定义我的宏,这也是一个宏:
MyDebugLog(instance, fmt, ....)
其中instance是MyLogger的一个实例,fmt,...是格式字符串和var列表。这是我对宏的试用定义:
#define MyDebugLog(logger, fmt, ...) 
  [logger, context:(CFStringRef)__FUNCTION__ lineNumber:__LINE__ 
   debug:fmt, ## _VA_ARGS__]
我在使用宏的代码中编译错误说“'context'unclaclared”:
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
  ...
  // myLogger is an instance of MyLogger.
  MyDebugLog(myLogger, @"something for %@", @"testing!"); // compiling error!
  ...
}
不确定我的定义有什么问题。有什么建议?     
已邀请:
这就是我使用的。
#define SFLog(message, ...) NSLog((@"SFLOG: %s [Line %d] " message), __PRETTY_FUNCTION__, __LINE__, ## __VA_ARGS__)
你使用它完全像
SFLog(@"Some var %f", myFloat);
,除了它预先设置'SFLog'(我也使用'SFError')和
__PRETTY_FUNCTION__
宏。 [编辑] 我找到了这个SO帖子。它就在这里。 [编辑] 评论者Jonathan Leffler在下面指出,可以在没有
##
的情况下定义宏。
##
纯粹是GCC的东西,不符合C99标准。
##
也适用于LLVM / Clang 1.5(XCode 3.2.4)以及-std = c99和-std = gnu99的编译器设置。只要了解宏如果不能按预期工作就有一些限制。     

要回复问题请先登录注册