How do I redirect all errors, including uncaught exceptions, NSLog calls, and other logs, to a log file on Mac OS X?

后端 未结 3 1547
忘掉有多难
忘掉有多难 2020-12-15 21:48

I am attempting to find a logging framework for a Cocoa application, written in ObjC.

What I\'ve attempted so far:

  1. Use NSLog, but then realise that it
3条回答
  •  [愿得一人]
    2020-12-15 22:11

    You can use the Foundation function NSSetUncaughtExceptionHandler to set a callback function that will handle all uncaught exceptions:

    void CustomLogger(NSString *format, ...) {
       //do other awesome logging stuff here...
    }
    
    void uncaughtExceptionHandler(NSException *exception) {
       //do something useful, like this:
       CustomLogger(@"%@", [exception reason]);
    }
    
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    

    For NSLog, you can use a macro to override it :)

    #define NSLog(...) CustomLogger(__VA_ARGS__);
    

提交回复
热议问题