Skip to content

Commit

Permalink
Logging : Make it log to same file for a day...
Browse files Browse the repository at this point in the history
  • Loading branch information
antokne committed May 16, 2023
1 parent 91f58bf commit efb7613
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
5 changes: 4 additions & 1 deletion Taggy/TaggyApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct TaggyApp: App {

var viewModel = AGContentViewModel()

// Initing the log view model here so it's not created multiple times...
var logViewModel = TaggyLoggingViewModel()

var body: some Scene {

WindowGroup {
Expand Down Expand Up @@ -55,7 +58,7 @@ struct TaggyApp: App {
.keyboardShortcut("T", modifiers: [.command])

Window("Taggy Log", id: TaggyLogWindowName) {
TaggyLoggingView()
TaggyLoggingView(viewModel: logViewModel)
}
.keyboardShortcut("L", modifiers: [.command])

Expand Down
4 changes: 2 additions & 2 deletions Taggy/Views/LogWindow/TaggyLoggingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct TaggyLoggingView: View {
@State var textLog: String = ""

@ObservedObject var viewModel = TaggyLoggingViewModel()
@ObservedObject var viewModel: TaggyLoggingViewModel

var body: some View {
VStack {
Expand All @@ -28,6 +28,6 @@ struct TaggyLoggingView: View {

struct TaggyLoggingView_Previews: PreviewProvider {
static var previews: some View {
TaggyLoggingView()
TaggyLoggingView(viewModel: TaggyLoggingViewModel())
}
}
41 changes: 35 additions & 6 deletions Taggy/Views/LogWindow/TaggyLoggingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TaggyLoggingViewModel: ObservableObject {
private var messageCancellable: AnyCancellable?

init() {
loadCurrentLog()
messageCancellable = AGTaggyManager.shared.collector.$message
.receive(on: RunLoop.main)
.sink { [weak self] message in
Expand All @@ -36,19 +37,47 @@ class TaggyLoggingViewModel: ObservableObject {
let logMessage = String(format: "%@: %@\n" , dateFormatter.string(from: Date()), message)
textLog += logMessage

let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)

guard let bundleId = Bundle.mainBundleId else {
guard let logFileURL = logFileURL() else {
return
}


try? textLog.write(toFile: logFileURL.path, atomically: true, encoding: .utf8)
}

func loadCurrentLog() {
guard let logFileURL = logFileURL() else {
return
}

do {
let logContents = try String(contentsOf: logFileURL)
textLog = logContents
}
catch {
// silently failing is not great.
// we just end up creating a new log so not the end of the world I think.
}
}

func logFileURL() -> URL? {

let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)

guard let bundleId = Bundle.mainBundleId else {
return nil
}

let dir = URL(fileURLWithFileSystemRepresentation: bundleId, isDirectory: true, relativeTo: caches.first)
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)

let logFile = URL(fileURLWithFileSystemRepresentation: "\(Date())-taggy.log", isDirectory: false, relativeTo: dir)

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyy-MM-dd"
let dateString = dateFormatter.string(from: Date())

let logFile = URL(fileURLWithFileSystemRepresentation: "\(dateString)-taggy.log", isDirectory: false, relativeTo: dir)

try? textLog.write(toFile: logFile.path, atomically: true, encoding: .utf8)
return logFile
}

}

0 comments on commit efb7613

Please sign in to comment.