|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +// |
| 18 | + |
| 19 | +import Foundation |
| 20 | +import Logging |
| 21 | + |
| 22 | +/// Basic log handler for where simple message output is needed, |
| 23 | +/// such as CLI commands. |
| 24 | +public struct StderrLogHandler: LogHandler { |
| 25 | + public var logLevel: Logger.Level = .info |
| 26 | + public var metadata: Logger.Metadata = [:] |
| 27 | + |
| 28 | + public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { |
| 29 | + get { |
| 30 | + self.metadata[metadataKey] |
| 31 | + } |
| 32 | + set { |
| 33 | + self.metadata[metadataKey] = newValue |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public init() {} |
| 38 | + |
| 39 | + public func log( |
| 40 | + level: Logger.Level, |
| 41 | + message: Logger.Message, |
| 42 | + metadata: Logger.Metadata?, |
| 43 | + source: String, |
| 44 | + file: String, |
| 45 | + function: String, |
| 46 | + line: UInt |
| 47 | + ) { |
| 48 | + let messageText = message.description |
| 49 | + let data = messageText.data(using: .utf8) ?? Data() |
| 50 | + |
| 51 | + // Use a single write call for atomicity |
| 52 | + var output = data |
| 53 | + output.append("\n".data(using: .utf8)!) |
| 54 | + FileHandle.standardError.write(output) |
| 55 | + } |
| 56 | +} |
0 commit comments