p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial; min-height: 16.0px}
Logging is very useful and powerful tool. All iOS programmers knows about such great method as NSLog(NSString *format);
But there is one problem on active usage of that method – usually you don’t need to logging something in Release version of a product, because logging need resources.
Let’s take a look on two solutions to avoid.
But to start a little prerequisites.
1. Open your project settings (Project -> Edit Project Settings).
2. Go to the ‘Build‘ tab.
3. Make ‘Debug‘ as currently selected configuration.
4. Set ‘
DEBUG=1‘ to configure
Preprocessor Macros.p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
First solution
1. Add the following macro into
*Prefix.pchfile
#ifdef DEBUG #define LOG(...) NSLog(__VA_ARGS__)#else #define LOG(...)#endif
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial; min-height: 16.0px}
This allow us to use LOG() macro from whatever place in the programm instead of NSLog() using and also this disable logging for Release versions.
Simple and convenient. But this method has one drawback. What if the project is large and the information it logged a lot? If so you will get much unnecessary info in your console. But this is not a problem, let’s go to the second solution.
Second solution
The idea is to split all info to a different urgency levels and allow to configure which levels we need to write into log. Let’ go.
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
1. Add new class into the project and name it ‘Log‘.
2. Add macros for different urgency levels into Log.h
LOG_LEVEL_MOST_IMPORTANT – for high priority information
LOG_LEVEL_NORMAL – for normal priority information
LOG_LEVEL_COMMON – other information that doesn’t need much attention (debug info etc.)
#define LOG_LEVEL_MOST_IMPORTANT 1#define LOG_LEVEL_NORMAL 2#define LOG_LEVEL_COMMON 3
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
3. Add method definition for logging
+ (Void) log: (NSString *) message level: (short) level;
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
Log.hfinal version should looks like:
#define LOG_LEVEL_MOST_IMPORTANT 1#define LOG_LEVEL_NORMAL
2#define LOG_LEVEL_COMMON 3 @interface Log : NSObject {} +
(void)log:(NSString *)message level:(short)level;
@end
4. Add two directives into Log.m
LOG_LEVEL_NOTHING - show nothing in console
CURRENT_LOG_LEVEL - show information with current detalisation level (is equal one of LOG_LEVEL_xxx macros). This directive must be wrapped by #ifdef DEBUG … #else … #endif to avoid logging in Release versions.
#define LOG_LEVEL_NOTHING 0 #ifdef DEBUG
#define CURRENT_LOG_LEVEL LOG_LEVEL_ MOST_IMPORTANT#else
#define CURRENT_LOG_LEVEL LOG_LEVEL_NOTHING#endif
5. Implement method for logging
+ (void)log:(NSString *)message level:(short)level { if (CURRENT_LOG_LEVEL >= level)
NSLog(@"%@", message);}
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; min-height: 17.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
Pretty simple.. huh?
6. The last step – add import of Log.h into *Prefix.pch that allow us to use our newly created logging method
#import “Log.h”
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}
Now you can use
[Log log:@"test message" level:LOG_LEVEL_COMMON];
to log information with normal urgency level, or
[Log log:@"Warning! Server not response!" log:LOG_LEVEL_MOST_IMPORTANT];
to log most important information, like problems with connection to a server or whatever.
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Arial; min-height: 16.0px}
For tests in different versions you can set CURRENT_LOG_LEVEL = LOG_LEVEL_MOST_IMPORTANT.
This allow to write most important information into the console.
Of course, this solution is not as elegant as using of the LOG() macro, but it is quite flexible.