forked from apple/swift-nio-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugOutboundEventsHandler.swift
105 lines (89 loc) · 3.71 KB
/
DebugOutboundEventsHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
/// ChannelOutboundHandler that prints all outbound events that pass through the pipeline by default,
/// overridable by providing your own closure for custom logging. See DebugInboundEventsHandler for inbound events.
public class DebugOutboundEventsHandler: ChannelOutboundHandler {
public typealias OutboundIn = Any
public typealias OutboundOut = Any
public enum Event {
case register
case bind(address: SocketAddress)
case connect(address: SocketAddress)
case write(data: NIOAny)
case flush
case read
case close(mode: CloseMode)
case triggerUserOutboundEvent(event: Any)
}
var logger: (Event, ChannelHandlerContext) -> ()
public init(logger: @escaping (Event, ChannelHandlerContext) -> () = DebugOutboundEventsHandler.defaultPrint) {
self.logger = logger
}
public func register(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) {
logger(.register, context)
context.register(promise: promise)
}
public func bind(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
logger(.bind(address: address), context)
context.bind(to: address, promise: promise)
}
public func connect(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
logger(.connect(address: address), context)
context.connect(to: address, promise: promise)
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
logger(.write(data: data), context)
context.write(data, promise: promise)
}
public func flush(context: ChannelHandlerContext) {
logger(.flush, context)
context.flush()
}
public func read(context: ChannelHandlerContext) {
logger(.read, context)
context.read()
}
public func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
logger(.close(mode: mode), context)
context.close(mode: mode, promise: promise)
}
public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
logger(.triggerUserOutboundEvent(event: event), context)
context.triggerUserOutboundEvent(event, promise: promise)
}
public static func defaultPrint(event: Event, in context: ChannelHandlerContext) {
let message: String
switch event {
case .register:
message = "Registering channel"
case .bind(let address):
message = "Binding to \(address)"
case .connect(let address):
message = "Connecting to \(address)"
case .write(let data):
message = "Writing \(data)"
case .flush:
message = "Flushing"
case .read:
message = "Reading"
case .close(let mode):
message = "Closing with mode \(mode)"
print()
case .triggerUserOutboundEvent(let event):
message = "Triggering user outbound event: { \(event) }"
}
print(message + " in \(context.name)")
}
}