5
5
import Foundation
6
6
7
7
extension URLSessionProxyDelegate {
8
- /// Enables automatic registration of `URLSessionProxyDelegate`. After calling this method, every time
9
- /// you initialize a `URLSession` using `init(configuration:delegate:delegateQueue:))` method, the
10
- /// delegate will automatically get replaced with a `URLSessionProxyDelegate` that logs all the
11
- /// needed events and forwards the methods to your original delegate.
8
+ /// Enables automatic logging and remote debugging of network requests using
9
+ /// `URLSessionProxyDelegate`.
10
+ ///
11
+ /// - note: This method works by swizzling `URLSession` init and adding
12
+ /// `URLSessionProxyDelegate` to the delegate chain and adding
13
+ /// `RemoteLoggerURLProtocol` to the list of session protocol classes.
14
+ ///
15
+ /// - warning: This logging method works only with delegate-based `URLSession`
16
+ /// instances. If it doesn't work for you, consider using ``URLSessionProxy``
17
+ /// for automatic logging or manually logging the requests using ``NetworkLogger``.
18
+ ///
19
+ /// - parameter logger: The network logger to be used for recording the requests.
12
20
@MainActor
13
21
public static func enableAutomaticRegistration( logger: NetworkLogger = . init( ) ) {
14
- guard sharedNetworkLogger == nil else {
15
- NSLog ( " Error: Puls network request logging is already enabled " )
16
- return
17
- }
22
+ guard !isAutomaticNetworkLoggingEnabled else { return }
23
+
18
24
sharedNetworkLogger = logger
19
25
if let lhs = class_getClassMethod ( URLSession . self, #selector( URLSession . init ( configuration: delegate: delegateQueue: ) ) ) ,
20
26
let rhs = class_getClassMethod ( URLSession . self, #selector( URLSession . pulse_init ( configuration: delegate: delegateQueue: ) ) ) {
@@ -23,15 +29,37 @@ extension URLSessionProxyDelegate {
23
29
}
24
30
}
25
31
26
- var sharedNetworkLogger : NetworkLogger ? {
32
+ /// Returns `true` if automatic logging was already enabled using one of the
33
+ /// existing mechanisms provided by Pulse.
34
+ @MainActor
35
+ var isAutomaticNetworkLoggingEnabled : Bool {
36
+ guard URLSessionProxy . proxy == nil else {
37
+ NSLog ( " Error: Pulse.URLSessionProxy already enabled " )
38
+ return true
39
+ }
40
+ guard sharedNetworkLogger == nil else {
41
+ NSLog ( " Error: Pulse network request logging is already enabled " )
42
+ return true
43
+ }
44
+ return false
45
+ }
46
+
47
+ func isConfiguringSessionSafe( delegate: URLSessionDelegate ? ) -> Bool {
48
+ if String ( describing: delegate) . contains ( " GTMSessionFetcher " ) {
49
+ return false
50
+ }
51
+ return true
52
+ }
53
+
54
+ private var sharedNetworkLogger : NetworkLogger ? {
27
55
get { _sharedLogger. value }
28
56
set { _sharedLogger. value = newValue }
29
57
}
30
58
private let _sharedLogger = Mutex < NetworkLogger ? > ( nil )
31
59
32
60
private extension URLSession {
33
61
@objc class func pulse_init( configuration: URLSessionConfiguration , delegate: URLSessionDelegate ? , delegateQueue: OperationQueue ? ) -> URLSession {
34
- guard !String ( describing : delegate) . contains ( " GTMSessionFetcher " ) else {
62
+ guard isConfiguringSessionSafe ( delegate : delegate) else {
35
63
return self . pulse_init ( configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
36
64
}
37
65
configuration. protocolClasses = [ RemoteLoggerURLProtocol . self] + ( configuration. protocolClasses ?? [ ] )
0 commit comments