From 4d7b01831baab3d147f82b23a6a9b622407c3d34 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 | 40 ++++++++++++++++++++++++----- phpmon/Common/Shell/RealShell.swift | 2 +- phpmon/Domain/App/AppDelegate.swift | 10 +++++--- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/phpmon/Common/Core/Logger.swift b/phpmon/Common/Core/Logger.swift index 7be4247..8bdb4c8 100644 --- a/phpmon/Common/Core/Logger.swift +++ b/phpmon/Common/Core/Logger.swift @@ -12,6 +12,9 @@ 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,36 +27,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("==================================") } } + 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() + } + } + } } 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 4afceae..27eba8e 100644 --- a/phpmon/Domain/App/AppDelegate.swift +++ b/phpmon/Domain/App/AppDelegate.swift @@ -56,10 +56,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: "")) } @@ -72,7 +71,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)