Monday 28 October 2013

Tutorial: How to customize NSLog for detailed information during debugging.

We can get the detailed information of each method while execution. since it is very helpful in debugging the crash all over the application. Also you can verify the data your are passing or save is same as you want.

Customization of NSLog is very simple. Just follow these below steps and get customize logs.

You can get information about File name, Line No., Message Name, and many more variables.
Just follow the simple steps below:


in Prefix.pch file, <you can find this file in Supporting Files group>.


add the following code:

#define LogEnable 1     //   0  ==> Disable, 1  ==> Enable

 // If you set the value to 1 it means customLog will apperar on the debug window 
Always set one for Debug Mode you can set it 1 on debug mode  using this code snippet:

 #ifdef DEBUG
    #define LogEnable 1
#else
    #define LogEnable 0

 #define DebugLog(__FORMAT__, ...) { if (LogEnable) NSLog((@"%@ %s %d " __FORMAT__), [[NSString stringWithUTF8String: __FILE__] lastPathComponent], __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}

Now try it on any Method in a file


like I am trying it on Sample.m file in checkingCustomLog method at line number 10:

//
//  Sample.m.m
//  MyProject
//
//  Created by Sahil Mahajan on 9/13/13.
//  Copyright (c) 2013 Sahil Mahajan. All rights reserved.
//

 #import "Sample.h"

 @implementation Sample

 -(void) checkingCustomLog
{
    DebugLog(@”Got a custom log”);
}


And the output is like:

2013-10-28 14:58:04.816 MyProject[17368:70b] Sample.m -[Sample checkingCustomLog] 15 Got a custom log

No comments:

Post a Comment