Redirect NSLog to File in Swift not working

后端 未结 1 898
执笔经年
执笔经年 2020-12-02 02:45

I am trying to send NSLog to a file in Swift 3 running on Simulator, IOS 10.2 and nothing is being produced

How to NSLog into a file

func redirectCon         


        
相关标签:
1条回答
  • 2020-12-02 03:08

    The absoluteString property of an URL produces an URL string, e.g.

        file:///path/to/file.txt
    

    which is not suitable as argument to freopen(). To get the file path as a string, use path instead:

    let logPath = dir.appendingPathComponent(file).path
    

    Better, use the dedicated method to pass an URLs path to a system call:

    let logFileURL = dir.appendingPathComponent(file)
    logFileURL.withUnsafeFileSystemRepresentation {
        _ = freopen($0, "a+", stderr)
    }
    
    0 讨论(0)
提交回复
热议问题