Skip to content

Commit f90a553

Browse files
committed
Improved logging
1 parent 204d210 commit f90a553

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

Sources/InterposeKit/Hooks/Hook.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,27 @@ public final class Hook {
177177
// ============================================================================ //
178178

179179
deinit {
180-
var logComponents = [String]()
181-
182-
switch self.state {
183-
case .pending:
184-
logComponents.append("Releasing")
185-
case .active:
186-
logComponents.append("Keeping")
187-
case .failed:
188-
logComponents.append("Leaking")
189-
}
190-
191-
logComponents.append("-[\(self.class) \(self.selector)]")
192-
193-
if let hookIMP = self.strategy.appliedHookIMP {
194-
logComponents.append("IMP: \(hookIMP)")
195-
}
196-
197-
Interpose.log(logComponents.joined(separator: " "))
180+
Interpose.log({
181+
var components = [String]()
182+
183+
switch self.state {
184+
case .pending:
185+
components.append("Releasing")
186+
case .active:
187+
components.append("Keeping")
188+
case .failed:
189+
components.append("Leaking")
190+
}
191+
192+
components.append("hook for")
193+
components.append("-[\(self.class) \(self.selector)]")
194+
195+
if let hookIMP = self.strategy.appliedHookIMP {
196+
components.append("IMP: \(hookIMP)")
197+
}
198+
199+
return components.joined(separator: " ")
200+
}())
198201
}
199202

200203
}

Sources/InterposeKit/Hooks/HookStrategy/ClassHookStrategy/ClassHookStrategy.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal final class ClassHookStrategy: HookStrategy {
8181
self.appliedHookIMP = hookIMP
8282
self.storedOriginalIMP = originalIMP
8383

84-
Interpose.log("Swizzled -[\(self.class) \(self.selector)] IMP: \(originalIMP) -> \(hookIMP)")
84+
Interpose.log("Replaced implementation for -[\(self.class) \(self.selector)] IMP: \(originalIMP) -> \(hookIMP)")
8585
}
8686

8787
internal func restoreImplementation() throws {
@@ -116,7 +116,7 @@ internal final class ClassHookStrategy: HookStrategy {
116116
)
117117
}
118118

119-
Interpose.log("Restored -[\(self.class) \(self.selector)] IMP: \(originalIMP)")
119+
Interpose.log("Restored implementation for -[\(self.class) \(self.selector)] IMP: \(originalIMP)")
120120
}
121121

122122
}

Sources/InterposeKit/Hooks/HookStrategy/ObjectHookStrategy/ObjectHookStrategy.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ final class ObjectHookStrategy: HookStrategy {
7474
do {
7575
try ITKSuperBuilder.addSuperInstanceMethod(to: dynamicSubclass, selector: self.selector)
7676
let imp = class_getMethodImplementation(dynamicSubclass, self.selector)!
77-
Interpose.log("Added super trampoline for -[\(dynamicSubclass) \(self.selector)]: \(imp)")
77+
Interpose.log("Added super trampoline for -[\(dynamicSubclass) \(self.selector)] IMP: \(imp)")
7878
} catch {
7979
// Interpose.log("Failed to add super implementation to -[\(dynamicClass).\(selector)]: \(error)")
8080
throw InterposeError.unknownError(String(describing: error))
@@ -92,7 +92,7 @@ final class ObjectHookStrategy: HookStrategy {
9292
selector: self.selector
9393
)
9494
}
95-
Interpose.log("Added -[\(self.class).\(self.selector)] IMP: \(self.storedOriginalIMP!) -> \(hookIMP)")
95+
Interpose.log("Replaced implementation for -[\(self.class) \(self.selector)] IMP: \(self.storedOriginalIMP!) -> \(hookIMP)")
9696
}
9797

9898
func restoreImplementation() throws {
@@ -127,7 +127,7 @@ final class ObjectHookStrategy: HookStrategy {
127127
imp: previousIMP
128128
)
129129
}
130-
Interpose.log("Restored -[\(self.class).\(self.selector)] IMP: \(originalIMP)")
130+
Interpose.log("Restored implementation for -[\(self.class) \(self.selector)] IMP: \(originalIMP)")
131131
} else {
132132
let nextHook = self._findParentHook(from: currentIMP)
133133
// Replace next's original IMP

Sources/InterposeKit/Hooks/HookStrategy/ObjectHookStrategy/ObjectSubclassManager.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ internal enum ObjectSubclassManager {
2323
) throws -> AnyClass {
2424
// If there is a dynamic subclass already installed on the object, reuse it straightaway.
2525
if let installedSubclass = self.installedSubclass(for: object) {
26+
Interpose.log({
27+
let subclassName = NSStringFromClass(installedSubclass)
28+
let objectAddress = String(format: "%p", object)
29+
return "Reused subclass: \(subclassName) for object \(objectAddress)"
30+
}())
31+
2632
return installedSubclass
2733
}
2834

@@ -33,8 +39,18 @@ internal enum ObjectSubclassManager {
3339
// Then, set the created class on the object.
3440
object_setClass(object, subclass)
3541

36-
let oldName = NSStringFromClass(class_getSuperclass(object_getClass(object))!)
37-
Interpose.log("Generated \(NSStringFromClass(subclass)) for object (was: \(oldName))")
42+
Interpose.log({
43+
let subclassName = NSStringFromClass(subclass)
44+
let objectAddress = String(format: "%p", object)
45+
var message = "Created subclass: \(subclassName) for object \(objectAddress)"
46+
47+
if let superclass = class_getSuperclass(subclass) {
48+
let superclassName = NSStringFromClass(superclass)
49+
message += " (was: \(superclassName))"
50+
}
51+
52+
return message
53+
}())
3854

3955
return subclass
4056
}

Sources/InterposeKit/Interpose.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,18 @@ public enum Interpose {
8888
/// The flag indicating whether logging is enabled.
8989
public static var isLoggingEnabled = false
9090

91-
internal static func log(_ message: String) {
91+
internal static func log(
92+
_ message: @autoclosure () -> String
93+
) {
9294
if self.isLoggingEnabled {
93-
print("[InterposeKit] \(message)")
95+
print("[InterposeKit] \(message())")
9496
}
9597
}
9698

97-
internal static func fail(_ message: String) -> Never {
98-
fatalError("[InterposeKit] \(message)")
99+
internal static func fail(
100+
_ message: @autoclosure () -> String
101+
) -> Never {
102+
fatalError("[InterposeKit] \(message())")
99103
}
100104

101105
}

0 commit comments

Comments
 (0)