gpt4 book ai didi

ios - NSLog/OSLog 间歇性地出现在控制台中

转载 作者:行者123 更新时间:2023-11-28 13:37:59 25 4
gpt4 key购买 nike

我注意到我的 NSLog(我也尝试过使用 os_log)语句在控制台应用程序中显示不一致。

下面是一些在我的应用程序启动时运行的代码。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

NSLog("xyz didFinishLaunchingWithOptions")
FirebaseApp.configure()
NSLog("xyz FirebaseApp.configure() done")
let db = FirestoreDelegate.db()
let now = Date()
NSLog("xyz about to write")
db.collection("atest").document(DateUtils.formatTimestampGMT(now)).setData(["ts": now, "note":"delegate startup"]) {
err in
if let err = err {
NSLog ("xyz error ats \(err)")
return
}
NSLog("xyz wrote to ats \(DateUtils.formatTimestampGMT(now))")
}
NSLog("xyz after write")
... extraneous code removed
}

当我运行时,有时我会看到“xyz didFinishLaunchingWithOptions”和“wrote to ats”,但看不到其他日志语句。有时我会看到所有这些,但它们非常不一致。它们是否以某种方式被过滤或优化?

我没有通过 xcode 进行调试,我只是在手机上运行应用程序并通过计算机上的控制台应用程序查看日志。

最佳答案

我没弄清楚为什么日志记录不一致。相反,我创建了一个写入文件的自定义记录器。

public class FileWriter {
static func getTs() -> String {
let timestampFormat = DateFormatter()
timestampFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
return timestampFormat.string(from: Date())
}

static func write(_ text: String) {
do {
let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
let url = dir.appendingPathComponent("log.txt")
if !FileManager.default.fileExists(atPath: url.path) {
FileManager.default.createFile(atPath: url.path, contents: nil, attributes: nil)
}
let fileHandle = try FileHandle(forWritingTo: url)

let line = "\(getTs()) \(text)\n"
let data = line.data(using: String.Encoding.utf8)!
fileHandle.seekToEndOfFile()
fileHandle.write(data)

fileHandle.closeFile()
} catch let error as NSError {
print ("error \(error)")
}
}
}

为了读取文件,我将以下键添加到我的 Info.plist“UIFileSharingEnabled”、“LSSupportsOpeningDocumentsInPlace”,然后我可以使用"file"应用在手机上打开文件。

关于ios - NSLog/OSLog 间歇性地出现在控制台中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56369801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com