Skip to content

Commit e9bd13d

Browse files
committed
Wrap OSLog on my own instead of depending on a third party
1 parent 3e2e286 commit e9bd13d

File tree

4 files changed

+108
-18
lines changed

4 files changed

+108
-18
lines changed
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// LoggingUtil.swift
3+
// allonet2
4+
//
5+
// Created by Nevyn Bengtsson on 2025-11-18.
6+
//
7+
8+
import Logging
9+
10+
extension Logger
11+
{
12+
public func forClient(_ cid: ClientId) -> Logger
13+
{
14+
var clientLogger = self
15+
clientLogger[metadataKey: "clientId"] = .stringConvertible(cid)
16+
return clientLogger
17+
}
18+
public func forInteraction(_ inter: Interaction) -> Logger
19+
{
20+
let requestId = inter.requestId
21+
var interactionLogger = self
22+
interactionLogger[metadataKey: "requestId"] = .string(requestId)
23+
return interactionLogger
24+
}
25+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// i e: anything but Linux
2+
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
3+
4+
import Foundation
5+
import Logging
6+
import os.log
7+
8+
public struct OSLogHandler: LogHandler
9+
{
10+
public init(subsystem: String, category: String) {
11+
self.log = OSLog(subsystem: subsystem, category: category)
12+
}
13+
14+
private let log: OSLog
15+
16+
public var logLevel: Logging.Logger.Level = .info
17+
public var metadataProvider: Logging.Logger.MetadataProvider?
18+
public var metadata = Logging.Logger.Metadata()
19+
20+
public subscript(metadataKey metadataKey: String) -> Logging.Logger.Metadata.Value? {
21+
get {
22+
self.metadata[metadataKey]
23+
}
24+
set {
25+
self.metadata[metadataKey] = newValue
26+
}
27+
}
28+
29+
public func log(
30+
level: Logging.Logger.Level,
31+
message: Logging.Logger.Message,
32+
metadata explicitMetadata: Logging.Logger.Metadata?,
33+
source: String,
34+
file: String,
35+
function: String,
36+
line: UInt
37+
) {
38+
let effectiveMetadata = Self.prepareMetadata(
39+
base: self.metadata,
40+
provider: self.metadataProvider,
41+
explicit: explicitMetadata
42+
)
43+
let metas = effectiveMetadata.map {"\($0): \($1)"}.joined(separator: ", ")
44+
let type = Self.levels[level]!
45+
os_log("%{public}@", log: log, type: type, "\(message)\n# from \(file)#\(line) \(function) [\(metas)]")
46+
}
47+
48+
internal static func prepareMetadata(
49+
base: Logging.Logger.Metadata,
50+
provider: Logging.Logger.MetadataProvider?,
51+
explicit: Logging.Logger.Metadata?
52+
) -> Logging.Logger.Metadata {
53+
var metadata = base
54+
55+
let provided = provider?.get() ?? [:]
56+
57+
guard !provided.isEmpty || !((explicit ?? [:]).isEmpty) else {
58+
return metadata
59+
}
60+
61+
if !provided.isEmpty {
62+
metadata.merge(provided, uniquingKeysWith: { _, provided in provided })
63+
}
64+
65+
if let explicit = explicit, !explicit.isEmpty {
66+
metadata.merge(explicit, uniquingKeysWith: { _, explicit in explicit })
67+
}
68+
69+
return metadata
70+
}
71+
72+
private static var levels: [Logging.Logger.Level: OSLogType] = [
73+
.trace: .debug,
74+
.debug: .debug,
75+
.info: .info,
76+
.notice: .info,
77+
.warning: .info,
78+
.error: .error,
79+
.critical: .fault
80+
]
81+
}
82+
83+
#endif

Sources/allonet2/util.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import Foundation
99
import simd
1010
import OpenCombineShim
11-
import Logging
1211

1312
func with<T>(_ value: T, using closure: (inout T) -> Void) -> T {
1413
var copy = value
@@ -303,20 +302,3 @@ public func configurePrintBuffering()
303302
setvbuf(stdout, nil, _IOLBF, 0) // line-buffered
304303
setvbuf(stderr, nil, _IONBF, 0) // unbuffered
305304
}
306-
307-
extension Logger
308-
{
309-
public func forClient(_ cid: ClientId) -> Logger
310-
{
311-
var clientLogger = self
312-
clientLogger[metadataKey: "clientId"] = .stringConvertible(cid)
313-
return clientLogger
314-
}
315-
public func forInteraction(_ inter: Interaction) -> Logger
316-
{
317-
let requestId = inter.requestId
318-
var interactionLogger = self
319-
interactionLogger[metadataKey: "requestId"] = .string(requestId)
320-
return interactionLogger
321-
}
322-
}

0 commit comments

Comments
 (0)