| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- * Copyright 2021, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Logging
- import NIOCore
- /// Wraps `Logger` to always provide the source as "GRPC".
- ///
- /// See https://github.com/apple/swift-log/issues/145 for rationale.
- @usableFromInline
- internal struct GRPCLogger {
- @usableFromInline
- internal var logger: Logger
- internal var unwrapped: Logger {
- return self.logger
- }
- @inlinable
- internal init(wrapping logger: Logger) {
- self.logger = logger
- }
- internal subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
- get {
- return self.logger[metadataKey: metadataKey]
- }
- set {
- self.logger[metadataKey: metadataKey] = newValue
- }
- }
- @usableFromInline
- internal func trace(
- _ message: @autoclosure () -> Logger.Message,
- metadata: @autoclosure () -> Logger.Metadata? = nil,
- file: String = #fileID,
- function: String = #function,
- line: UInt = #line
- ) {
- self.logger.trace(
- message(),
- metadata: metadata(),
- source: "GRPC",
- file: file,
- function: function,
- line: line
- )
- }
- @usableFromInline
- internal func debug(
- _ message: @autoclosure () -> Logger.Message,
- metadata: @autoclosure () -> Logger.Metadata? = nil,
- file: String = #fileID,
- function: String = #function,
- line: UInt = #line
- ) {
- self.logger.debug(
- message(),
- metadata: metadata(),
- source: "GRPC",
- file: file,
- function: function,
- line: line
- )
- }
- @usableFromInline
- internal func notice(
- _ message: @autoclosure () -> Logger.Message,
- metadata: @autoclosure () -> Logger.Metadata? = nil,
- file: String = #fileID,
- function: String = #function,
- line: UInt = #line
- ) {
- self.logger.notice(
- message(),
- metadata: metadata(),
- source: "GRPC",
- file: file,
- function: function,
- line: line
- )
- }
- @usableFromInline
- internal func warning(
- _ message: @autoclosure () -> Logger.Message,
- metadata: @autoclosure () -> Logger.Metadata? = nil,
- file: String = #fileID,
- function: String = #function,
- line: UInt = #line
- ) {
- self.logger.warning(
- message(),
- metadata: metadata(),
- source: "GRPC",
- file: file,
- function: function,
- line: line
- )
- }
- }
- extension GRPCLogger {
- internal mutating func addIPAddressMetadata(local: SocketAddress?, remote: SocketAddress?) {
- self.logger.addIPAddressMetadata(local: local, remote: remote)
- }
- }
- extension Logger {
- @inlinable
- internal var wrapped: GRPCLogger {
- return GRPCLogger(wrapping: self)
- }
- }
|