fix
杨锴
2025-06-16 3fa53409f5132333ce6d83fff796e108ddd62090
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
//  DestinationProtocol.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by Dave Wood on 2014-06-06.
//  Copyright © 2014 Dave Wood, Cerebral Gardens.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/master/LICENSE.txt
//
 
// MARK: - DestinationProtocol
/// Protocol for destination classes to conform to
public protocol DestinationProtocol: CustomDebugStringConvertible {
    // MARK: - Properties
    /// Logger that owns the destination object
    var owner: XCGLogger? {get set}
 
    /// Identifier for the destination (should be unique)
    var identifier: String {get set}
 
    /// Log level for this destination
    var outputLevel: XCGLogger.Level {get set}
 
    /// Flag whether or not we've logged the app details to this destination
    var haveLoggedAppDetails: Bool { get set }
 
    /// Array of log formatters to apply to messages before they're output
    var formatters: [LogFormatterProtocol]? { get set }
 
    /// Array of log filters to apply to messages before they're output
    var filters: [FilterProtocol]? { get set }
 
    // MARK: - Methods
    /// Process the log details.
    ///
    /// - Parameters:
    ///     - logDetails:   Structure with all of the details for the log to process.
    ///
    /// - Returns:  Nothing
    ///
    func process(logDetails: LogDetails)
 
    /// Process the log details (internal use, same as processLogDetails but omits function/file/line info).
    ///
    /// - Parameters:
    ///     - logDetails:   Structure with all of the details for the log to process.
    ///
    /// - Returns:  Nothing
    ///
    func processInternal(logDetails: LogDetails)
 
    /// Check if the destination's log level is equal to or lower than the specified level.
    ///
    /// - Parameters:
    ///     - level: The log level to check.
    ///
    /// - Returns:
    ///     - true:     Log destination is at the log level specified or lower.
    ///     - false:    Log destination is at a higher log level.
    ///
    func isEnabledFor(level: XCGLogger.Level) -> Bool
 
    /// Apply filters to determine if the log message should be logged.
    ///
    /// - Parameters:
    ///     - logDetails:   The log details.
    ///     - message:      Formatted/processed message ready for output.
    ///
    /// - Returns:
    ///     - true:     Drop this log message.
    ///     - false:    Keep this log message and continue processing.
    ///
    func shouldExclude(logDetails: inout LogDetails, message: inout String) -> Bool
 
    /// Apply formatters.
    ///
    /// - Parameters:
    ///     - logDetails:   The log details.
    ///     - message:      Formatted/processed message ready for output.
    ///
    /// - Returns:  Nothing
    ///
    func applyFormatters(logDetails: inout LogDetails, message: inout String)
}
 
extension DestinationProtocol {
 
    /// Iterate over all of the log filters in this destination, or the logger if none set for the destination.
    ///
    /// - Parameters:
    ///     - logDetails: The log details.
    ///     - message: Formatted/processed message ready for output.
    ///
    /// - Returns:
    ///     - true:     Drop this log message.
    ///     - false:    Keep this log message and continue processing.
    ///
    public func shouldExclude(logDetails: inout LogDetails, message: inout String) -> Bool {
        guard let filters = self.filters ?? self.owner?.filters, filters.count > 0 else { return false }
 
        for filter in filters {
            if filter.shouldExclude(logDetails: &logDetails, message: &message) {
                return true
            }
        }
 
        return false
    }
 
    /// Iterate over all of the log formatters in this destination, or the logger if none set for the destination.
    ///
    /// - Parameters:
    ///     - logDetails:   The log details.
    ///     - message:      Formatted/processed message ready for output.
    ///
    /// - Returns:  Nothing
    ///
    public func applyFormatters(logDetails: inout LogDetails, message: inout String) {
        guard let formatters = self.formatters ?? self.owner?.formatters, formatters.count > 0 else { return }
 
        for formatter in formatters {
            formatter.format(logDetails: &logDetails, message: &message)
        }
    }
}