mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-08-07 12:00:09 +02:00
✨ Extra verbose logging
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`.
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,7 +140,7 @@ class RealShell: ShellProtocol {
|
||||
|
||||
""")
|
||||
|
||||
print(log)
|
||||
Log.info(log)
|
||||
}
|
||||
|
||||
return .out(stdOut, stdErr)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user