杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
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
//
//  PrePostFixLogFormatter.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by Dave Wood on 2016-09-20.
//  Copyright © 2016 Dave Wood, Cerebral Gardens.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/main/LICENSE.txt
//
 
#if os(macOS)
    import AppKit
#elseif os(iOS) || os(tvOS) || os(watchOS)
    import UIKit
#endif
 
// MARK: - PrePostFixLogFormatter
/// A log formatter that will optionally add a prefix, and/or postfix string to a message
open class PrePostFixLogFormatter: LogFormatterProtocol, CustomDebugStringConvertible {
 
    /// Internal cache of the prefix strings for each log level
    internal var prefixStrings: [XCGLogger.Level: String] = [:]
 
    /// Internal cache of the postfix strings codes for each log level
    internal var postfixStrings: [XCGLogger.Level: String] = [:]
 
    public init() {
    }
 
    /// Set the prefix/postfix strings for a specific log level.
    ///
    /// - Parameters:
    ///     - prefix:   A string to prepend to log messages.
    ///     - postfix:  A string to postpend to log messages.
    ///     - level:    The log level.
    ///
    /// - Returns:  Nothing
    ///
    open func apply(prefix: String? = nil, postfix: String? = nil, to level: XCGLogger.Level? = nil) {
        guard let level = level else {
            guard prefix != nil || postfix != nil else { clearFormatting(); return }
 
            // No level specified, so, apply to all levels
            for level in XCGLogger.Level.allCases {
                self.apply(prefix: prefix, postfix: postfix, to: level)
            }
            return
        }
 
        if let prefix = prefix {
            prefixStrings[level] = prefix
        }
        else {
            prefixStrings.removeValue(forKey: level)
        }
 
        if let postfix = postfix {
            postfixStrings[level] = postfix
        }
        else {
            postfixStrings.removeValue(forKey: level)
        }
    }
 
    /// Clear all previously set colours. (Sets each log level back to default)
    ///
    /// - Parameters:   None
    ///
    /// - Returns:  Nothing
    ///
    open func clearFormatting() {
        prefixStrings = [:]
        postfixStrings = [:]
    }
 
    // MARK: - LogFormatterProtocol
    /// Apply some additional formatting to the message if appropriate.
    ///
    /// - Parameters:
    ///     - logDetails:   The log details.
    ///     - message:      Formatted/processed message ready for output.
    ///
    /// - Returns:  message with the additional formatting
    ///
    @discardableResult open func format(logDetails: inout LogDetails, message: inout String) -> String {
        message = "\(prefixStrings[logDetails.level] ?? "")\(message)\(postfixStrings[logDetails.level] ?? "")"
        return message
    }
 
    // MARK: - CustomDebugStringConvertible
    open var debugDescription: String {
        get {
            var description: String = "\(extractTypeName(self)): "
            for level in XCGLogger.Level.allCases {
                description += "\n\t- \(level) > \(prefixStrings[level] ?? "None") | \(postfixStrings[level] ?? "None")"
            }
 
            return description
        }
    }
}