Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Darwin
import Foundation
import os

/// The level of logging severity.
///
Expand Down Expand Up @@ -77,7 +76,7 @@ import os
#if os(iOS)
// On iOS, the user has no access to stdout.
// Output can be read from the log by the user or the `flutter` tool.
self.init(outputWriter: OSLogOutputWriter(), logLevel: .info)
self.init(outputWriter: SyslogOutputWriter(), logLevel: .info)
#elseif os(macOS)
// On macOS, both the user and the tool read from stdout.
self.init(outputWriter: StdoutOutputWriter(), logLevel: .info)
Expand Down Expand Up @@ -183,34 +182,11 @@ protocol OutputWriter: Sendable {
func writeLine(level: LogLevel, _ message: String)
}

private extension LogLevel {
/// The `OSLogType` used to emit a message at this level via `os_log`.
///
/// `OSLogType` is not a strict severity ladder like `LogLevel`. It's a small set of categories
/// with differing persistence and display behavior. Each level therefore maps to the type with
/// the closest semantics rather than a matching severity.
///
/// - `.info` is buffered in memory and not written to persistent store by default.
/// - `.warning` is written to persistent store.
/// - `.error` is logged with error metadata and is written to persistent store.
/// - `.important` is used by Dart `print` output, so is written to persistent store.
/// - `.fatal` is logged with fault metadata and is written to persistent store.
var osLogType: OSLogType {
switch self {
case .info: return .info
case .warning: return .default
case .error: return .error
case .important: return .default
case .fatal: return .fault
}
}
}

final class OSLogOutputWriter: OutputWriter, Sendable {
private let osLog = OSLog(subsystem: "io.flutter.flutter", category: "flutter")

final class SyslogOutputWriter: OutputWriter, Sendable {
func writeLine(level: LogLevel, _ message: String) {
os_log("%{public}@", log: osLog, type: level.osLogType, message)
// TODO(cbracken): replace this with os_log-based approach.
// https://github.com/flutter/flutter/issues/44030
message.withCString { vsyslog(LOG_ALERT, "%s", getVaList([$0])) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import Foundation
import Testing
@testable import test_utils_swift

// Several tests mutate shared `Logger` singleton properties (`outputWriter`, `logLevel`).
// Even though those mutations are all lock-guarded and thread-safe, we serialize the tests to keep
// them from racing each other.
@Suite(.serialized) struct LoggerTests {
@Suite struct LoggerTests {
Comment thread
cbracken marked this conversation as resolved.

@Test func testInitialization() {
let writer = StringOutputWriter()
Expand Down Expand Up @@ -138,10 +135,4 @@ import Testing
// races or crashes.
#expect(Logger.logLevel == .info || Logger.logLevel == .warning)
}

@Test func testDefaultInitialization() {
let logger = Logger()
#expect(logger.logLevel == .info)
logger.log(level: .info, "Test default logger")
}
}