Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New logging engine #11

Open
wants to merge 4 commits into
base: senpai
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Harbor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -73,6 +74,7 @@
AB6A967E2A31045A003A019E /* SetupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupView.swift; sourceTree = "<group>"; };
AB7A80FF2A30CC7100AA71A6 /* BrewUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrewUtils.swift; sourceTree = "<group>"; };
AB7A81012A30D2FE00AA71A6 /* HarborUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarborUtils.swift; sourceTree = "<group>"; };
AB7F9D172A38AC58003014D8 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
AB7D8E322A4DDE3400B55527 /* WinetricksUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WinetricksUtils.swift; sourceTree = "<group>"; };
AB87CAF32A4AC67C00C32025 /* HarborShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarborShortcuts.swift; sourceTree = "<group>"; };
AB95D9FC2A5011C5003402D2 /* GPTKConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPTKConfigView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -220,6 +222,7 @@
isa = PBXGroup;
children = (
ABC0BFD02A31629300382A42 /* HarborBottle.swift */,
AB7F9D172A38AC58003014D8 /* Logger.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
15 changes: 15 additions & 0 deletions Harbor/Models/HarborBottle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@

// swiftlint:disable cyclomatic_complexity
// This function is a bit too complex right now
func launchApplication(_ application: String, arguments: [String] = [], environmentVars: [String: String] = [:],

Check failure on line 32 in Harbor/Models/HarborBottle.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Function Body Length Violation: Function body should span 50 lines or less excluding comments and whitespace: currently spans 53 lines (function_body_length)
workDir: String = "", isUnixPath: Bool = false) {
let task = Process()
let logger = Logger()

task.launchPath = "/usr/local/opt/game-porting-toolkit/bin/wine64"
task.arguments = ["start"]

Expand Down Expand Up @@ -74,6 +76,19 @@
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 {
Expand Down
40 changes: 40 additions & 0 deletions Harbor/Models/Logger.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}