2

I'm looking for a way to capture all NSLog output in an NSArray or any other memory structure, while keeping my console logs intact. I'm creating a tool that sends a copy of the latest X log lines to a server upon request. So far, these are the options that I ruled out:

  • I can't redirect NSLog to a file as suggested here, as that would remove also all log messages from console.
  • I tried swizzling NSLog with another custom function, but I'm having trouble finding how to swizzle C functions as opposed to class methods.
  • Wrapping NSLog in my own CustomLog function is possible, but I'd rather have it as a last resort. The project is quite big and there are too many calls to replace. I'm also afraid of losing console output from libraries, where it would be hard to add my CustomLog.

I've been looking for a way to intercept NSLog output, or to capture logs after they've been printed in console, but I couldn't find anything that worked. Any suggestions?

Update: I ended up rethinking the approach, and I replaced all log calls through the code with my own logging method. It was extra work, but the flexibility to change implementation details paid in the end.

Community
  • 1
  • 1
Tjarom
  • 21
  • 2

2 Answers2

1

How about you redirect the logs to console.log in documents directory as you mentioned in the link How to redirect the nslog output to file instead of console.

When you want to send the logs to server, you can read from console.log which is present in the document directory and send it to server.


Another not pretty solution would be to redefine NSLog like this:

void myLoggingFunction(NSString *format, ...)
{

    va_list args;
    va_start(args, format);

    // Do my stuff with log
    NSLog(@"Sample log: Doing my stuff");

    NSLog(format, args);

    va_end(args);
}
#define NSLog myLoggingFunction

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Test Log output");

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

The above code output is :

2017-02-09 14:39:18.619 Logging_objc[91206:3161193] Sample log: Doing my stuff
2017-02-09 14:39:18.620 Logging_objc[91206:3161193] Test Log output
Community
  • 1
  • 1
manishg
  • 9,520
  • 1
  • 16
  • 19
  • Redirecting all logs to a file also removes them from console. Ideally, I'd like to keep logs on console AND be able to capture them somehow. – Tjarom Feb 09 '17 at 19:15
  • See my edit, you can use this approach to override NSLog. May be add the define in your top level header file – manishg Feb 09 '17 at 19:41
0

Incredibly difficult.

However, I don't have a single NSLog call in my code - use a logging library, or just define some macros, which will do what you want them to do, either calling NSLog or doing something else.

gnasher729
  • 51,477
  • 5
  • 75
  • 98