I have the following simplified code example:
// 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
I use this method in other classes to print debug messages to XCode console. Here is an example I test my debug method(in MyViewController class with a table view):
- (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!"];
...
}
The above example works well; however, for me, it does not look like a standard C/C++ or ObjC syntax. The reason I need to use this macro is that I would like to pass the first two arguments as "constants" to the method. Actually, those two arguments are also macros: __FUNCTION__ being a C++ macro, and __LINE__ being a C standard macro. Those two special macros would be dynamically mapped to a string of the function and a line number where the method is called. For example the above debug method call prints a msg in XCode console like this:
[timestamp] -[MyViewController tableView:numberOfRowsInSection:] line:107 - something for testing!
...
[timestamp] -[MyViewController tableView:cellForRowAtIndexPath:] line:124 - something for another testing!
The MyLogger class is mainly for my internal use. Is there any other better practice to get the same result? Or is there any problem with this implementation?
I am thinking to define my macro in the same way as NSLog(fmt, ...), which is a macro as well:
MyDebugLog(instance, fmt, ....)
where instance is an instance of MyLogger, and fmt, ... are format string and var list. This is my trial definition of the macro:
#define MyDebugLog(logger, fmt, ...)
[logger, context:(CFStringRef)__FUNCTION__ lineNumber:__LINE__
debug:fmt, ## _VA_ARGS__]
I got compiling error saying "'context' undeclared" in my code where the macro is used:
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
...
// myLogger is an instance of MyLogger.
MyDebugLog(myLogger, @"something for %@", @"testing!"); // compiling error!
...
}
Not sure what's wrong in my definition. Any suggestions?
See Question&Answers more detail:
os