我试图把所有的控制台记录到一个文件中。
我找到了像this这样的帖子。我一直在测试:没有用NSLog显示的内容(用打印显示的内容),但是没有将数据写入文件中,无论是NSLog还是打印的。
当应用程序关闭时,我会关闭文件。
我所做的如下:
var file: UnsafeMutablePointer<FILE>?
func logAll() {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let filename = "/\(Date().timeIntervalSinceReferenceDate).log"
let logFilePath = paths[0].appending(filename)
file = freopen(logFilePath.cString(using: .ascii), "a+", stderr)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
logAll()
/// rest of the code
}
func applicationWillTerminate(_ application: UIApplication) {
if let file = file {
fclose(file)
}
}我在这里错过了什么?
发布于 2022-05-10 16:16:04
好的,我有这个。
有3个std文件:stdin、stdout和stderr。
当我尝试使用打印时,我没有看到它,但是常规打印存储在stdout文件中。稍后,在具有更多库的项目中尝试,似乎(例如,firebase)日志存储在stderr中。
所以,我不是只打开一个文件,而是打开3个文件来获取所有日志。
现在唯一的骗局是我没有办法得到正确的订单
https://stackoverflow.com/questions/72183925
复制相似问题