Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/G6SensorCore/Session/TransmitterSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,10 @@ public final class TransmitterSession: TransmitterConnectionDelegate {

let ack = try peripheral.requestBackfill(startTime: startTime, endTime: endTime)

log.default("Backfill acknowledged: status %{public}@, backfillStatus %{public}@, id %{public}@, range %{public}@-%{public}@, length %{public}@, crc %{public}@",
String(ack.status), String(ack.backfillStatus), String(ack.identifier),
String(ack.startTime), String(ack.endTime),
String(ack.bufferLength), String(format: "%04x", ack.bufferCRC))
log.default("Backfill acknowledged: %{public}@",
"status \(ack.status), backfillStatus \(ack.backfillStatus), id \(ack.identifier), " +
"range \(ack.startTime)-\(ack.endTime), length \(ack.bufferLength), " +
"crc \(String(format: "%04x", ack.bufferCRC))")

// Nothing stored for the window: an empty result, not a failure.
guard ack.bufferLength > 0 else {
Expand Down
20 changes: 13 additions & 7 deletions Sources/G6SensorCore/Transport/OSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ extension OSLog {
}

private func log(_ message: StaticString, type: OSLogType, _ args: [CVarArg]) {
mirrorToFile(message, type: type, args)
let rendered = render(message, args)
mirrorToFile(rendered, type: type)

switch args.count {
case 0:
Expand All @@ -50,7 +51,10 @@ extension OSLog {
case 5:
os_log(message, log: self, type: type, args[0], args[1], args[2], args[3], args[4])
default:
os_log(message, log: self, type: type, args)
// os_log can't take the array as varargs: handing it over reads
// garbage pointers for each %@ and crashes. Log the rendered line.
assertionFailure("OSLog supports at most 5 arguments, got \(args.count): \(message)")
os_log("%{public}@", log: self, type: type, rendered)
}
}

Expand All @@ -65,15 +69,17 @@ extension OSLog {
return Self.categories[ObjectIdentifier(self)] ?? "G6SensorKit"
}

/// Mirrors every OSLog line into the exportable file log. Format
/// specifiers are OSLog-flavoured (%{public}@), so the privacy qualifier
/// is stripped before String(format:) sees them.
private func mirrorToFile(_ message: StaticString, type: OSLogType, _ args: [CVarArg]) {
/// Format specifiers are OSLog-flavoured (%{public}@), so the privacy
/// qualifier is stripped before String(format:) sees them.
private func render(_ message: StaticString, _ args: [CVarArg]) -> String {
let template = "\(message)"
.replacingOccurrences(of: "%{public}", with: "%")
.replacingOccurrences(of: "%{private}", with: "%")
let rendered = args.isEmpty ? template : String(format: template, arguments: args)
return args.isEmpty ? template : String(format: template, arguments: args)
}

/// Mirrors every OSLog line into the exportable file log.
private func mirrorToFile(_ rendered: String, type: OSLogType) {
let level: String
switch type {
case .debug: level = "DEBUG"
Expand Down