user1913341
user1913341

Reputation: 85

How to pass parameter values to the custom NSLOG

I'm developing one logger class for my application.NSLog will be printed only in debug mode. I have customized the NSLog where Name of the source file,Source code line number,Name of the class and method where NSLog() was called is printed. That is , my current NSLOG looks like (ClassName MethodName) (SourceFileName:LineNumber) NSLog output

Now, I want to log, parameter values of the methodname. How to get those parameter values in NSLog??? I want the output as (ClassName MethodName) (SourceFileName:LineNumber) (Parameter Values) NSLog output

Upvotes: 2

Views: 892

Answers (3)

bbum
bbum

Reputation: 162722

There isn't a way to automatically introspect the values passed to a method. Even in DEBUG builds (where the optimizer is out of the way), any attempts to write code to introspect said iVars is going to be incredibly complex (you'll have to dive into the symbol tables, extract offsets, etc, and then try and find the arguments that were likely destroyed in the attempt to grab them).

So, no, no way to really automate that.

In general, though, any such logging mechanism would generate such an atrociously huge amount of output that you are far better off creating (potentially debugging only) logging that is both configurable and highly tuned to your application.


You can pass args like this (thanks to @hoha for the simpler version).

#import <Foundation/Foundation.h>

#define FooLog(fmt, ...) NSLog(@"(%s): %@", __PRETTY_FUNCTION__, ## __VA_ARGS__)

@interface Bob:NSObject
@end
@implementation Bob
- (void)yourUncle
{
    FooLog(@"%@", self);
}
@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSString *w = @"World";
        FooLog(@"Hello, %@!", w);
        [[Bob new] yourUncle];
    }
}

Output:

2013-09-02 10:51:49.447 Untitled[60967:507] (int main(int, char **)): Hello, World!
2013-09-02 10:51:49.453 Untitled[60967:507] (-[Bob yourUncle]): <Bob: 0x7fde8840a490>

Upvotes: 2

hoha
hoha

Reputation: 4428

Something like this should work

#define InstanceLog(fmt, ...) NSLog(@"(%@.%@)(%s:%d) " fmt, NSStringFromClass(self.class), NSStringFromSelector(_cmd), __FILE__, __LINE__, ##__VA_ARGS__)

You may use it as NSLog within Objective-C methods

InstanceLog(@"simple string");
InstanceLog(@"%@ %@", @"hello", @"world");

Upvotes: 3

Amit Pandey
Amit Pandey

Reputation: 79


for handle custom logging you can use or refer following link.
You can customise Lumberjack as per you needs.

Lumberjack error logging for ios
Lumberjack error logging tutorial

Upvotes: 0

Related Questions