forked from tuist/tuist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the debugging experience by persisting logs in the file syste…
…m for every run (tuist#7261) * Show log file at the end * Add a clarifying note * Remove the dependency between Environment and FileHandle * Address some comments * Document how to diagnose with logs * Address comments * Clean old logs * Have the default in a single place * Address some comments * Address more comments * Update docs/docs/en/cli/logging.md Co-authored-by: Marek Fořt <[email protected]> * Add support for selective testing for Xcode non-generated projects (tuist#7287) * Add support for selective testing for Xcode projects not generated with Tuist * Update XcodeGraph * Update reference from xctest-dynamic-overlay to swift-issue-reporting in Package.resolved * Add xcode_project_with_tests to a list of fixtures * Add log for which test modules are skipped * Add Tuist.swift for the xcode_project_with_tests fixture * Override acceptance test case arguments for XcodeBuildCommand * Add logs for which targets were skippped only when some were * Show log file at the end * Document how to diagnose issues on CI * Add some guidelines around how to do logging * Address some issues after rebasing * Rename LogsCleaner to LogsController and extract some logic from TuistCLI into the controller * Add missing dependency * Fix issues after rebasing * Fix linting issue * Update Sources/TuistKit/Utils/LogsController.swift Co-authored-by: Marek Fořt <[email protected]> --------- Co-authored-by: Marek Fořt <[email protected]>
- Loading branch information
Showing
20 changed files
with
436 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
enum LogFilePathDisplayStrategy: Decodable { | ||
/// Only shows the path to the log file on error | ||
case onError | ||
|
||
/// Always shows the path to the log file. | ||
case always | ||
|
||
/// The log file path is never shown. | ||
case never | ||
} | ||
|
||
/// This is a protocol that commands can conform to provide their preferences | ||
/// regarding how the log file path should be shown. | ||
/// | ||
/// If a command doesn't conform to this protocol, the default strategy used is "onError" | ||
protocol LogConfigurableCommand { | ||
var logFilePathDisplayStrategy: LogFilePathDisplayStrategy { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import FileSystem | ||
import Foundation | ||
import Path | ||
import TuistSupport | ||
|
||
public struct LogsController { | ||
private let fileSystem: FileSystem | ||
|
||
public init(fileSystem: FileSystem = FileSystem()) { | ||
self.fileSystem = fileSystem | ||
} | ||
|
||
public func setup( | ||
stateDirectory: AbsolutePath, | ||
action: (@escaping @Sendable (String) -> any LogHandler, AbsolutePath) async throws -> Void | ||
) async throws { | ||
let logFilePath = try await touchLogFile(stateDirectory: stateDirectory) | ||
let machineReadableCommands = [DumpCommand.self] | ||
// swiftformat:disable all | ||
let isCommandMachineReadable = | ||
CommandLine.arguments.count > 1 | ||
&& machineReadableCommands.map { $0._commandName }.contains(CommandLine.arguments[1]) | ||
// swiftformat:enable all | ||
let loggingConfig = | ||
if isCommandMachineReadable || CommandLine.arguments.contains("--json") { | ||
LoggingConfig( | ||
loggerType: .json, | ||
verbose: ProcessInfo.processInfo.environment[Constants.EnvironmentVariables.verbose] != nil | ||
) | ||
} else { | ||
LoggingConfig.default | ||
} | ||
|
||
try await clean(logsDirectory: logFilePath.parentDirectory) | ||
|
||
let loggerHandler = try Logger.defaultLoggerHandler( | ||
config: loggingConfig, logFilePath: logFilePath | ||
) | ||
|
||
try await action(loggerHandler, logFilePath) | ||
} | ||
|
||
private func clean(logsDirectory: AbsolutePath) async throws { | ||
let fiveDaysAgo = Calendar.current.date(byAdding: .day, value: -5, to: Date())! | ||
|
||
for logPath in try await fileSystem.glob(directory: logsDirectory, include: ["*"]).collect() { | ||
if let creationDate = try FileManager.default.attributesOfItem(atPath: logPath.pathString)[.creationDate] as? Date, | ||
creationDate < fiveDaysAgo | ||
{ | ||
try await fileSystem.remove(logPath) | ||
} | ||
} | ||
} | ||
|
||
private func touchLogFile(stateDirectory: AbsolutePath) async throws -> Path.AbsolutePath { | ||
let fileSystem = FileSystem() | ||
let logFilePath = stateDirectory.appending(components: [ | ||
"logs", "\(UUID().uuidString).log", | ||
]) | ||
if !(try await fileSystem.exists(logFilePath.parentDirectory)) { | ||
try await fileSystem.makeDirectory(at: logFilePath.parentDirectory) | ||
} | ||
try await fileSystem.touch(logFilePath) | ||
return logFilePath | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.