是否可以将每个NSLog不仅写入控制台,还可以写入文件?我想无需更换准备这个NSLog成someExternalFunctionForLogging.
替换所有人将是真正的问题NSLog.也许有可能从控制台解析数据或捕获消息?
我试图将NSLog发送到在模拟器,IOS 10.2上运行的Swift 3中的文件,并且没有生成任何内容
func redirectConsoleLogToDocumentFolder() {
let file = "file.txt"
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let logPath = dir.appendingPathComponent(file).absoluteString
print("log:\(logPath)")
freopen(logPath, "a+", stderr)
}
NSLog("print nslog")
}
Run Code Online (Sandbox Code Playgroud)
产量
~/Library/Developer/CoreSimulator/Devices/A7B717-3ED8-493A-9778-C594AF9FF446/data/Containers/Data/Application/B0386-64BB-46EB-9BF2-65209FC748CD/Documents/file.txt
Run Code Online (Sandbox Code Playgroud)
唯一的影响是输出不再打印到控制台.
我试过了
freopen(logPath.cString(using: .utf8), "a+", stderr)
Run Code Online (Sandbox Code Playgroud)
以及其他各种组合
使用我收到的路径写入文件没有问题,所以没有任何问题
我希望在路径中看到一个名为file.txt的文件,并且该文件包含"print nslog".我尝试先创建文件但没有成功.