Question

I use NSLoger in iOS app, it great, it can show function name, file line, just I also want show the instance of the class information.

eg: class A methods is 'doAction', a instance : A* a = [A alloc];

  in method 'doAction':
  {
        NSLog(@"out this instance information, do not need repeat set the argument [%@]",self );
  }

I meaning is that I do not need define the format and the 'self' argument in every NSLog statement....

how to do it ? may I explain clearly and need your help thanks.

so... then I try to add some code as follows:

 void LogMessageMe( const char *filename, int lineNumber, const char *functionName,id  target, int level, NSString *format, ...)
{
    NSString* strTarget = [target description];

    va_list args;
    va_start(args, format);

    LogMessageTo_internal(NULL,  filename, lineNumber,  functionName, strTarget, level, format, args);
    va_end(args);
}

and

#define NSLogMe(...)                      LogMessageMe( __FILE__, __LINE__, __FUNCTION__, self, 0, __VA_ARGS__)

the call code as follows:

 NSLogMe(@"didFinishLaunchingWithOptions");

then output log:

  <AppDelegate: 0x906e100> | Main thread | didFinishLaunchingWithOptions

that is really what I want to see... since in a app, many instance was create but that is the same class... if no the identify number, it is hard to trace the target instance ....

but still a issue...

IDE report warning message about the statement:

NSLogMe(@"didFinishLaunchingWithOptions");

"Implicit declaration of function ‘LogMessageMe’ is invalid in C99"

it seem small issue... anyone help ? thanks.

Was it helpful?

Solution

Use the %p specifier in NSLog statements to output the value of a pointer:

NSLogMe(@"Called from %@ instance %p", [self class], self);

These are standard [NSString stringWithFormat:...] specifiers. For a complete list, see the documentation for string format specifiers.

OTHER TIPS

The warning has nothing to do with NSLogger.

You need to define the macro NSLogMe in a place where the function signature LogMessageMe is visible.

And if you declare LogMessageMe outside of LoggerClient then you need to make LogMessageTo_internal public (now it isn't, as its name indicates).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top