杨锴
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
//  ANSIColorLogFormatter.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by Dave Wood on 2016-08-30.
//  Copyright © 2016 Dave Wood, Cerebral Gardens.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/main/LICENSE.txt
//
 
// MARK: - ANSIColorLogFormatter
/// A log formatter that will add ANSI colour codes to the message
open class ANSIColorLogFormatter: LogFormatterProtocol, CustomDebugStringConvertible {
 
    /// ANSI Escape code
    public static let escape: String = "\u{001b}["
 
    /// ANSI Reset colours code
    public static let reset: String = "\(escape)m"
 
    /// Enum to specify ANSI colours
    public enum ANSIColor: CustomStringConvertible {
        case black
        case red
        case green
        case yellow
        case blue
        case magenta
        case cyan
        case lightGrey, lightGray
        case darkGrey, darkGray
        case lightRed
        case lightGreen
        case lightYellow
        case lightBlue
        case lightMagenta
        case lightCyan
        case white
        case `default`
        case rgb(red: Int, green: Int, blue: Int)
        case colorIndex(number: Int)
 
        public var foregroundCode: String {
            switch self {
            case .black:
                return "30"
            case .red:
                return "31"
            case .green:
                return "32"
            case .yellow:
                return "33"
            case .blue:
                return "34"
            case .magenta:
                return "35"
            case .cyan:
                return "36"
            case .lightGrey, .lightGray:
                return "37"
            case .darkGrey, .darkGray:
                return "90"
            case .lightRed:
                return "91"
            case .lightGreen:
                return "92"
            case .lightYellow:
                return "93"
            case .lightBlue:
                return "94"
            case .lightMagenta:
                return "95"
            case .lightCyan:
                return "96"
            case .white:
                return "97"
            case .default: // Note: Different from the default: at the end of a switch, this is the `default` colour
                return "39"
            case .rgb(let red, let green, let blue):
                return "38;2;\(min(max(0, red), 255));\(min(max(0, green), 255));\(min(max(0, blue), 255))"
            case .colorIndex(let number):
                return "38;5;\(min(max(0, number), 255))"
            }
        }
 
        public var backgroundCode: String {
            switch self {
            case .black:
                return "40"
            case .red:
                return "41"
            case .green:
                return "42"
            case .yellow:
                return "43"
            case .blue:
                return "44"
            case .magenta:
                return "45"
            case .cyan:
                return "46"
            case .lightGrey, .lightGray:
                return "47"
            case .darkGrey, .darkGray:
                return "100"
            case .lightRed:
                return "101"
            case .lightGreen:
                return "102"
            case .lightYellow:
                return "103"
            case .lightBlue:
                return "104"
            case .lightMagenta:
                return "105"
            case .lightCyan:
                return "106"
            case .white:
                return "107"
            case .default: // Note: Different from the default: at the end of a switch, this is the `default` colour
                return "49"
            case .rgb(let red, let green, let blue):
                return "48;2;\(min(max(0, red), 255));\(min(max(0, green), 255));\(min(max(0, blue), 255))"
            case .colorIndex(let number):
                return "48;5;\(min(max(0, number), 255))"
            }
        }
 
        /// Human readable description of this colour (CustomStringConvertible)
        public var description: String {
            switch self {
            case .black:
                return "Black"
            case .red:
                return "Red"
            case .green:
                return "Green"
            case .yellow:
                return "Yellow"
            case .blue:
                return "Blue"
            case .magenta:
                return "Magenta"
            case .cyan:
                return "Cyan"
            case .lightGrey, .lightGray:
                return "Light Grey"
            case .darkGrey, .darkGray:
                return "Dark Grey"
            case .lightRed:
                return "Light Red"
            case .lightGreen:
                return "Light Green"
            case .lightYellow:
                return "Light Yellow"
            case .lightBlue:
                return "Light Blue"
            case .lightMagenta:
                return "Light Magenta"
            case .lightCyan:
                return "Light Cyan"
            case .white:
                return "White"
            case .default: // Note: Different from the default: at the end of a switch, this is the `default` colour
                return "Default"
            case .rgb(let red, let green, let blue):
                return String(format: "(r: %d, g: %d, b: %d) #%02X%02X%02X", red, green, blue, red, green, blue)
            case .colorIndex(let number):
                return "ANSI color index: \(number)"
            }
        }
    }
 
    /// Enum to specific ANSI options
    public enum ANSIOption: CustomStringConvertible {
        case bold
        case faint
        case italic
        case underline
        case blink
        case blinkFast
        case strikethrough
 
        public var code: String {
            switch self {
            case .bold:
                return "1"
            case .faint:
                return "2"
            case .italic:
                return "3"
            case .underline:
                return "4"
            case .blink:
                return "5"
            case .blinkFast:
                return "6"
            case .strikethrough:
                return "9"
            }
        }
 
        public var description: String {
            switch self {
            case .bold:
                return "Bold"
            case .faint:
                return "Faint"
            case .italic:
                return "Italic"
            case .underline:
                return "Underline"
            case .blink:
                return "Blink"
            case .blinkFast:
                return "Blink Fast"
            case .strikethrough:
                return "Strikethrough"
            }
        }
    }
 
    /// Internal cache of the ANSI codes for each log level
    internal var formatStrings: [XCGLogger.Level: String] = [:]
 
    /// Internal cache of the description for each log level
    internal var descriptionStrings: [XCGLogger.Level: String] = [:]
 
    public init() {
        resetFormatting()
    }
 
    /// Set the colours and/or options for a specific log level.
    ///
    /// - Parameters:
    ///     - level:            The log level.
    ///     - foregroundColor:  The text colour of the message. **Default:** Restore default text colour
    ///     - backgroundColor:  The background colour of the message. **Default:** Restore default background colour
    ///     - options:          Array of ANSIOptions to apply to the message. **Default:** No options
    ///
    /// - Returns:  Nothing
    ///
    open func colorize(level: XCGLogger.Level, with foregroundColor: ANSIColor = .default, on backgroundColor: ANSIColor = .default, options: [ANSIOption] = []) {
        var codes: [String] = [foregroundColor.foregroundCode, backgroundColor.backgroundCode]
        var description: String = "\(foregroundColor) on \(backgroundColor)"
 
        for option in options {
            codes.append(option.code)
            description += "/\(option)"
        }
 
        formatStrings[level] = ANSIColorLogFormatter.escape + codes.joined(separator: ";") + "m"
        descriptionStrings[level] = description
    }
 
    /// Set the colours and/or options for a specific log level.
    ///
    /// - Parameters:
    ///     - level:    The log level.
    ///     - custom:   A specific ANSI code to use.
    ///
    /// - Returns:  Nothing
    ///
    open func colorize(level: XCGLogger.Level, custom: String) {
        if custom.hasPrefix(ANSIColorLogFormatter.escape) {
            formatStrings[level] = "\(custom)"
            descriptionStrings[level] = "Custom: \(custom[custom.index(custom.startIndex, offsetBy: ANSIColorLogFormatter.escape.lengthOfBytes(using: .utf8)) ..< custom.endIndex])"
        }
        else {
            formatStrings[level] = ANSIColorLogFormatter.escape + "\(custom)"
            descriptionStrings[level] = "Custom: \(custom)"
        }
    }
 
    /// Get the cached ANSI codes for the specified log level.
    ///
    /// - Parameters:
    ///     - level:            The log level.
    ///
    /// - Returns:  The ANSI codes for the specified log level.
    ///
    internal func formatString(for level: XCGLogger.Level) -> String {
        return formatStrings[level] ?? ANSIColorLogFormatter.reset
    }
 
    /// Apply a default set of colours.
    ///
    /// - Parameters:   None
    ///
    /// - Returns:  Nothing
    ///
    open func resetFormatting() {
        colorize(level: .verbose, with: .white, options: [.bold])
        colorize(level: .debug, with: .black)
        colorize(level: .info, with: .blue)
        colorize(level: .warning, with: .yellow)
        colorize(level: .error, with: .red, options: [.bold])
        colorize(level: .severe, with: .white, on: .red)
        colorize(level: .none)
    }
 
    /// Clear all previously set colours. (Sets each log level back to default)
    ///
    /// - Parameters:   None
    ///
    /// - Returns:  Nothing
    ///
    open func clearFormatting() {
        colorize(level: .verbose)
        colorize(level: .debug)
        colorize(level: .info)
        colorize(level: .warning)
        colorize(level: .error)
        colorize(level: .severe)
        colorize(level: .none)
    }
 
    // 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 = "\(formatString(for: logDetails.level))\(message)\(ANSIColorLogFormatter.reset)"
        return message
    }
 
    // MARK: - CustomDebugStringConvertible
    open var debugDescription: String {
        get {
            var description: String = "\(extractTypeName(self)): "
            for level in XCGLogger.Level.allCases {
                description += "\n\t- \(level) > \(descriptionStrings[level] ?? "None")"
            }
 
            return description
        }
    }
}