How to disable logging?

后端 未结 1 550
天涯浪人
天涯浪人 2020-12-11 05:30

I have an app that is making use of UITextChecker class provided by Apple. This class has a following bug: If the iOS device is offline (expected to be often fo

1条回答
  •  囚心锁ツ
    2020-12-11 06:01

    Warning: This answer uses the private-yet-sort-of-documented Cocoa C functions _NSSetLogCStringFunction() and _NSLogCStringFunction().

    _NSSetLogCStringFunction() is an interface created by Apple to handle the implementation function for NSLog. It was initially exposed for WebObjects to hook into NSLog statements on Windows machines, but still exists in the iOS and OS X APIs today. It is documented in this support article.

    The function takes in a function pointer with the arguments const char* message, the string to log, unsigned length, the length of the message, and a BOOL withSysLogBanner which toggles the standard logging banner. If we create our own hook function for logging that doesn't actually do anything (an empty implementation rather than calling fprintf like NSLog does behind-the-scenes), we can effectively disable all logging for your application.

    Objective-C Example (or Swift with bridging header):

    extern void _NSSetLogCStringFunction(void(*)(const char*, unsigned, BOOL));
    
    static void hookFunc(const char* message, unsigned length, BOOL withSysLogBanner) { /* Empty */ }
    
    // Later in your application
    
    _NSSetLogCStringFunction(hookFunc);
    
    NSLog(@"Hello _NSSetLogCStringFunction!\n\n");  // observe this isn't logged
    

    An example implementation of this can be found in YILogHook, which provides an interface to add an array of blocks to any NSLog statement (write to file, etc).

    Pure Swift Example:

    @asmname("_NSSetLogCStringFunction") // NOTE: As of Swift 2.2 @asmname has been renamed to @_silgen_name
    func _NSSetLogCStringFunction(_: ((UnsafePointer, UInt32, Bool) -> Void)) -> Void
    
    func hookFunc(message: UnsafePointer, _ length: UInt32, _ withSysLogBanner: Bool) -> Void { /* Empty */ }
    
    _NSSetLogCStringFunction(hookFunc)
    
    NSLog("Hello _NSSetLogCStringFunction!\n\n");  // observe this isn't logged
    

    In Swift, you can also chose to ignore all of the block parameters without using hookFunc like so:

    _NSSetLogCStringFunction { _,_,_ in }
    

    To turn logging back on using Objective-C, just pass in NULL as the function pointer:

    _NSSetLogCStringFunction(NULL);
    

    With Swift things are a little different, since the compiler will complain about a type mismatch if we try to pass in nil or a nil pointer (NULL is unavailable in Swift). To solve this, we need to access another system function, _NSLogCStringFunction, to get a pointer to the default logging implementation, retain that reference while logging is disabled, and set the reference back when we want to turn logging back on.

    I've cleaned up the Swift implementation of this by adding a NSLogCStringFunc typedef:

    /// Represents the C function signature used under-the-hood by NSLog
    typealias NSLogCStringFunc = (UnsafePointer, UInt32, Bool) -> Void
    
    /// Sets the C function used by NSLog
    @_silgen_name("_NSSetLogCStringFunction") // NOTE: As of Swift 2.2 @asmname has been renamed to @_silgen_name
    func _NSSetLogCStringFunction(_: NSLogCStringFunc) -> Void
    
    /// Retrieves the current C function used by NSLog
    @_silgen_name("_NSLogCStringFunction")
    func _NSLogCStringFunction() -> NSLogCStringFunc
    
    let logFunc = _NSLogCStringFunction() // get function pointer to the system log function before we override it
    
    _NSSetLogCStringFunction { (_, _, _) in } // set our own log function to do nothing in an anonymous closure
    
    NSLog("Observe this isn't logged.");
    
    _NSSetLogCStringFunction(logFunc) // switch back to the system log function
    
    NSLog("Observe this is logged.")
    

    0 讨论(0)
提交回复
热议问题