Skip to content

Instantly share code, notes, and snippets.

@erikng
Forked from steipete/OSLogTest.swift
Created February 15, 2021 22:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikng/4b47aad70c6a07a72d5a641251971c3f to your computer and use it in GitHub Desktop.
Save erikng/4b47aad70c6a07a72d5a641251971c3f to your computer and use it in GitHub Desktop.
Sample code to access OSLogStore on iOS and macOS.
let subsystem = "com.steipete.LoggingTest"
func getLogEntries() throws -> [OSLogEntryLog] {
// FB8269189: OSLogStore does not work iOS.
let logStore = try OSLogStore(scope: .currentProcessIdentifier)
let oneHourAgo = logStore.position(date: Date().addingTimeInterval(-3600))
#if os(macOS)
let allEntries = try logStore.getEntries(at: oneHourAgo)
#else
// FB8518476: The Swift shims for for the entries enumerator are missing.
let allEntries = try Array(logStore.__entriesEnumerator(position: oneHourAgo, predicate: nil))
#endif
// FB8518539: Using NSPredicate to filter the subsystem doesn't seem to work.
return allEntries
.compactMap { $0 as? OSLogEntryLog }
.filter { $0.subsystem == subsystem }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment