Skip to content

Instantly share code, notes, and snippets.

@yume190
Created December 5, 2018 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yume190/a4cbfd96ee798540a098a3b33fe77421 to your computer and use it in GitHub Desktop.
Save yume190/a4cbfd96ee798540a098a3b33fe77421 to your computer and use it in GitHub Desktop.
WWDC 2018_405 signpost
//
// SignPost.swift
//
// Created by Yume on 2018/12/5.
// Copyright © 2018 Yume. All rights reserved.
//
import Foundation
import os.signpost
/// Log category: Related operations
/// Signpost name: An operation to measure
/// Signpost ID: Single interval
struct SignPost {
enum Category: String {
case baseDataUpdate
var log: OSLog {
// if !ProcessInfo.processInfo.environment.keys.contains("SIGNPOSTS_FOR_REFRESH") {
// return .disabled
// }
return OSLog(subsystem: "com.maxwin.FlowerBus", category: self.rawValue)
}
/// Signpost IDs are process-scoped
/// Making from object is convenient if you have the same object at .begin and .end
@available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
func id(object: AnyObject) -> OSSignpostID {
return OSSignpostID(log: self.log, object: object)
}
// @available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
// public func os_signpost(_ type: OSSignpostType, dso: UnsafeRawPointer = #dsohandle, log: OSLog, name: StaticString, signpostID: OSSignpostID = default, _ format: StaticString, _ arguments: CVarArg...)
// dso: UnsafeRawPointer = #dsohandle
// _ format: StaticString, _ arguments: CVarArg...
@available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
func post(type: OSSignpostType, name: StaticString, id: OSSignpostID? = nil) {
if let id = id {
os_signpost(type, log: self.log, name: name, signpostID: id)
} else {
os_signpost(type, log: self.log, name: name)
}
}
@available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
func post(type: OSSignpostType, name: StaticString, object: AnyObject? = nil) {
let id: OSSignpostID?
if let object = object {
id = self.id(object: object)
} else {
id = nil
}
self.post(type: type, name: name, id: id)
}
@available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
func execute(name: StaticString, id: OSSignpostID, function: () throws -> ()) rethrows {
self.post(type: .begin, name: name, id: id)
try function()
self.post(type: .end, name: name, id: id)
}
@available(OSX 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *)
func execute(name: StaticString, object: AnyObject? = nil, function: () throws -> ()) rethrows {
self.post(type: .begin, name: name, object: object)
try function()
self.post(type: .end, name: name, object: object)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment