From 339eafd34d3fa0e98fc57bf95078ecabd1dc927c Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Mon, 30 Jan 2023 19:10:52 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Extra=20verbose=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit You can start extra verbose logging by running: `touch ~/.config/phpmon/verbose` Once this file exists, you can find the latest log in: `~/.config/phpmon/last_session.log`. --- phpmon/Common/Core/Logger.swift | 45 ++++++++++++++++++++++------- phpmon/Common/Shell/RealShell.swift | 2 +- phpmon/Domain/App/AppDelegate.swift | 10 +++++-- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/phpmon/Common/Core/Logger.swift b/phpmon/Common/Core/Logger.swift index 1b43581..043db2b 100644 --- a/phpmon/Common/Core/Logger.swift +++ b/phpmon/Common/Core/Logger.swift @@ -12,6 +12,10 @@ class Log { static var shared = Log() + var logFilePath = "~/.config/phpmon/last_session.log" + + var logExists = false + enum Verbosity: Int { case error = 1, warning = 2, @@ -24,42 +28,61 @@ class Log { } } - var verbosity: Verbosity = .warning + public func prepareLogFile() { + if !isRunningTests && Verbosity.cli.isApplicable() { + _ = system("mkdir -p ~/.config/phpmon 2> /dev/null") + _ = system("rm ~/.config/phpmon/last_session.log 2> /dev/null") + _ = system("touch ~/.config/phpmon/last_session.log 2> /dev/null") + self.logExists = FileSystem.fileExists(self.logFilePath) + } + } + + var verbosity: Verbosity = .warning { + didSet { + self.prepareLogFile() + } + } static func err(_ item: Any) { if Verbosity.error.isApplicable() { - print("[E] \(item)") + Log.shared.log("[E] \(item)") } } static func warn(_ item: Any) { if Verbosity.warning.isApplicable() { - print("[W] \(item)") + Log.shared.log("[W] \(item)") } } static func info(_ item: Any) { if Verbosity.info.isApplicable() { - print("\(item)") + Log.shared.log("\(item)") } } static func perf(_ item: Any) { if Verbosity.performance.isApplicable() { - print("[P] \(item)") + Log.shared.log("[P] \(item)") } } static func separator(as verbosity: Verbosity = .info) { if verbosity.isApplicable() { - print("==================================") + Log.shared.log("==================================") } } - static func line(as verbosity: Verbosity = .info) { - if verbosity.isApplicable() { - print("----------------------------------") + private func log(_ text: String) { + print(text) + + if logExists && Verbosity.cli.isApplicable() { + let logFile = URL(string: self.logFilePath.replacingTildeWithHomeDirectory)! + if let fileHandle = try? FileHandle(forWritingTo: logFile) { + fileHandle.seekToEndOfFile() + fileHandle.write(text.appending("\n").data(using: .utf8).unsafelyUnwrapped) + fileHandle.closeFile() + } } } - -} +} \ No newline at end of file diff --git a/phpmon/Common/Shell/RealShell.swift b/phpmon/Common/Shell/RealShell.swift index ed69343..1ece815 100644 --- a/phpmon/Common/Shell/RealShell.swift +++ b/phpmon/Common/Shell/RealShell.swift @@ -140,7 +140,7 @@ class RealShell: ShellProtocol { """) - print(log) + Log.info(log) } return .out(stdOut, stdErr) diff --git a/phpmon/Domain/App/AppDelegate.swift b/phpmon/Domain/App/AppDelegate.swift index e5df2a6..fc72c1f 100644 --- a/phpmon/Domain/App/AppDelegate.swift +++ b/phpmon/Domain/App/AppDelegate.swift @@ -61,10 +61,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele When the application initializes, create all singletons. */ override init() { - logger.verbosity = .info - #if DEBUG logger.verbosity = .performance + if let profile = CommandLine.arguments.first(where: { $0.matches(pattern: "--configuration:*") }) { Self.initializeTestingProfile(profile.replacingOccurrences(of: "--configuration:", with: "")) } @@ -77,7 +76,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele if CommandLine.arguments.contains("--cli") { logger.verbosity = .cli - Log.info("Extra CLI mode has been activated.") + Log.info("Extra CLI mode has been activated via --cli flag.") + } + + if FileSystem.fileExists("~/.config/phpmon/verbose") { + logger.verbosity = .cli + Log.info("Extra CLI mode is on (`~/.config/phpmon/verbose` exists).") } Log.separator(as: .info)