繁体   English   中英

NSLog / OSLog间歇性显示在控制台中

[英]NSLog/OSLog intermittently showing up in console

我注意到我的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”和“写给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”中添加了以下键,然后可以使用“文件”应用在手机上打开文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM