diff --git a/Harbor.xcodeproj/project.pbxproj b/Harbor.xcodeproj/project.pbxproj index 5a9f3e7..95f3a4e 100644 --- a/Harbor.xcodeproj/project.pbxproj +++ b/Harbor.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ AB6A967F2A31045A003A019E /* SetupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB6A967E2A31045A003A019E /* SetupView.swift */; }; AB7A81002A30CC7100AA71A6 /* BrewUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7A80FF2A30CC7100AA71A6 /* BrewUtils.swift */; }; AB7A81022A30D2FE00AA71A6 /* HarborUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7A81012A30D2FE00AA71A6 /* HarborUtils.swift */; }; + AB7F9D182A38AC58003014D8 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7F9D172A38AC58003014D8 /* Logger.swift */; }; AB7D8E332A4DDE3400B55527 /* WinetricksUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB7D8E322A4DDE3400B55527 /* WinetricksUtils.swift */; }; AB87CAF42A4AC67C00C32025 /* HarborShortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB87CAF32A4AC67C00C32025 /* HarborShortcuts.swift */; }; AB95D9FD2A5011C5003402D2 /* GPTKConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB95D9FC2A5011C5003402D2 /* GPTKConfigView.swift */; }; @@ -73,6 +74,7 @@ AB6A967E2A31045A003A019E /* SetupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupView.swift; sourceTree = ""; }; AB7A80FF2A30CC7100AA71A6 /* BrewUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrewUtils.swift; sourceTree = ""; }; AB7A81012A30D2FE00AA71A6 /* HarborUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarborUtils.swift; sourceTree = ""; }; + AB7F9D172A38AC58003014D8 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; AB7D8E322A4DDE3400B55527 /* WinetricksUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WinetricksUtils.swift; sourceTree = ""; }; AB87CAF32A4AC67C00C32025 /* HarborShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarborShortcuts.swift; sourceTree = ""; }; AB95D9FC2A5011C5003402D2 /* GPTKConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPTKConfigView.swift; sourceTree = ""; }; @@ -220,6 +222,7 @@ isa = PBXGroup; children = ( ABC0BFD02A31629300382A42 /* HarborBottle.swift */, + AB7F9D172A38AC58003014D8 /* Logger.swift */, ); path = Models; sourceTree = ""; @@ -363,6 +366,7 @@ AB0D004C2A40B8900019D62F /* DXVKInstallView.swift in Sources */, AB0D004E2A40E3D30019D62F /* EnvironmentVarsEditor.swift in Sources */, AB7A81002A30CC7100AA71A6 /* BrewUtils.swift in Sources */, + AB7F9D182A38AC58003014D8 /* Logger.swift in Sources */, AB6652CD2A3350EC00F3FC5D /* BottleConfigDropdown.swift in Sources */, AB5CC6D62A30910300AEBB2B /* GPKUtils.swift in Sources */, ABDA74592AD904E700802792 /* BottleDetailsCommonView.swift in Sources */, diff --git a/Harbor/Models/HarborBottle.swift b/Harbor/Models/HarborBottle.swift index 626aaab..2099661 100644 --- a/Harbor/Models/HarborBottle.swift +++ b/Harbor/Models/HarborBottle.swift @@ -32,6 +32,8 @@ struct HarborBottle: Identifiable, Equatable, Codable { func launchApplication(_ application: String, arguments: [String] = [], environmentVars: [String: String] = [:], workDir: String = "", isUnixPath: Bool = false) { let task = Process() + let logger = Logger() + task.launchPath = "/usr/local/opt/game-porting-toolkit/bin/wine64" task.arguments = ["start"] @@ -74,6 +76,19 @@ struct HarborBottle: Identifiable, Equatable, Codable { if pleaseShutUp { task.standardOutput = nil task.standardError = nil + } else { + let pipe = Pipe() + task.standardOutput = pipe + task.standardError = pipe + + pipe.fileHandleForReading.readabilityHandler = { handle in + let data = handle.availableData + if let output = String(data: data, encoding: .utf8) { + Task.detached { + await logger.log(output) + } + } + } } do { diff --git a/Harbor/Models/Logger.swift b/Harbor/Models/Logger.swift new file mode 100644 index 0000000..6ae1016 --- /dev/null +++ b/Harbor/Models/Logger.swift @@ -0,0 +1,40 @@ +// +// Logger.swift +// Harbor +// +// Created by Venti on 13/06/2023. +// + +import Foundation + +actor Logger { + // File handle to log directly to disk + private var fileHandle: FileHandle? + + init() { + do { + let logFile = HarborUtils.shared.getContainerHome().appendingPathComponent("harbor.log") + FileManager.default.createFile(atPath: logFile.path, contents: nil, attributes: nil) + fileHandle = try FileHandle(forWritingTo: logFile) + } catch { + print("Failed to create log file") + } + } + + deinit { + fileHandle?.closeFile() + } + + func log(_ message: String) { + if !message.isEmpty { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" + let timestamp = dateFormatter.string(from: Date()) + let logMessage = "\(timestamp) \(message)" + print(logMessage) + if let logMessageData = logMessage.data(using: .utf8) { + fileHandle?.write(logMessageData) + } + } + } +}