Idan
Idan

Reputation: 5807

Using preprocessor macros in Objective-C/Xcode

I'm setting up some macros for Logging purposes.

#define LOG_NETWORK_DEBUG(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Network",2,__VA_ARGS__)
#define LOG_NETWORK_INFO(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Network",1,__VA_ARGS__)
#define LOG_NETWORK_ERROR(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Network",0,__VA_ARGS__)


#define LOG_MEDIA_DEBUG(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Media",2,__VA_ARGS__)
#define LOG_MEDIA_INFO(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Media",1,__VA_ARGS__)
#define LOG_MEDIA_ERROR(...)    LogMessageF(__FILE__,__LINE__,__FUNCTION__,@"Media",0,__VA_ARGS__)

However, since my code is currently infested with with NSLog calls, I want to transfer the NSLog calls to LogMessageF calls. (till I have time to migrate all my code to above macros) My project for iOS is currently comprised of 2 sub projects: Network, Media.

I want each NSLog calls to be converted to the appropriate Macro. (NSLog calls in the Media project would be converted to LOG_MEDIA_DEBUG).

I thought about setting a preprocessor macros in Xcode for each project called PROJ_NAME that will include the project name.

My question is how to use that Macro? I want to do something like this:

#define NSLog(...) LOG_$PROJ_NAME_DEBUG(__VA_ARGS__)

Upvotes: 0

Views: 835

Answers (1)

zloyrobot
zloyrobot

Reputation: 11

You can use (token concatenation). HELPER macro is used to evaluate PROJ_NAME macro before concatenation.

#define PROJ_NAME NETWORK

#define LOG_DEBUG(P) LOG_##P##_DEBUG
#define HELPER(X) LOG_DEBUG(X)
#define NSLog(...) HELPER(PROJ_NAME)(__VA_ARGS__)

Upvotes: 1

Related Questions