mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-09 05:40:07 +01:00
♻️ Added logger class
This commit is contained in:
@@ -20,13 +20,13 @@ extension App {
|
||||
func loadGlobalHotkey() {
|
||||
// Make sure we can retrieve the hotkey from preferences
|
||||
guard let hotkey = Preferences.preferences[.globalHotkey] as? String else {
|
||||
print("No global hotkey was saved in preferences. None set.")
|
||||
Log.info("No global hotkey was saved in preferences. None set.")
|
||||
return
|
||||
}
|
||||
|
||||
// Make sure we can parse the JSON into the desired format
|
||||
guard let keybindPref = GlobalKeybindPreference.fromJson(hotkey) else {
|
||||
print("No global hotkey loaded, could not be parsed!")
|
||||
Log.err("No global hotkey loaded, could not be parsed!")
|
||||
shortcutHotkey = nil
|
||||
return
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ extension AppDelegate {
|
||||
notificationCenter.delegate = self
|
||||
notificationCenter.requestAuthorization(options: [.alert], completionHandler: { granted, error in
|
||||
if !granted {
|
||||
print("PHP Monitor does not have permission to show notifications.")
|
||||
Log.warn("PHP Monitor does not have permission to show notifications.")
|
||||
}
|
||||
if let error = error {
|
||||
print("PHP Monitor encounted an error determining notification permissions:")
|
||||
print(error)
|
||||
Log.err("PHP Monitor encounted an error determining notification permissions:")
|
||||
Log.err(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -51,16 +51,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
||||
*/
|
||||
var switcher: PhpSwitcher! = nil
|
||||
|
||||
var logger: Log = Log.shared
|
||||
|
||||
// MARK: - Initializer
|
||||
|
||||
/**
|
||||
When the application initializes, create all singletons.
|
||||
*/
|
||||
override init() {
|
||||
print("==================================")
|
||||
print("PHP MONITOR by Nico Verbruggen")
|
||||
print("Version \(App.version)")
|
||||
print("==================================")
|
||||
Log.shared.verbosity = .info
|
||||
Log.info("==================================")
|
||||
Log.info("PHP MONITOR by Nico Verbruggen")
|
||||
Log.info("Version \(App.version)")
|
||||
Log.info("==================================")
|
||||
self.sharedShell = Shell.user
|
||||
self.state = App.shared
|
||||
self.menu = MainMenu.shared
|
||||
|
||||
@@ -77,7 +77,7 @@ class Startup {
|
||||
|
||||
if (!failed) {
|
||||
initializeSwitcher()
|
||||
print("PHP Monitor has determined the application has successfully passed all checks.")
|
||||
Log.info("PHP Monitor has determined the application has successfully passed all checks.")
|
||||
success()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class LocalNotification {
|
||||
let notificationCenter = UNUserNotificationCenter.current()
|
||||
notificationCenter.add(request) { (error) in
|
||||
if error != nil {
|
||||
print(error!)
|
||||
Log.err(error!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class PMWindowController: NSWindowController, NSWindowDelegate {
|
||||
}
|
||||
|
||||
deinit {
|
||||
print("Window controller '\(windowName)' was deinitialized")
|
||||
Log.perf("Window controller '\(windowName)' was deinitialized")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ class HomebrewDiagnostics {
|
||||
let tapAlias = Shell.pipe("\(Paths.brew) info shivammathur/php/php --json")
|
||||
|
||||
if tapAlias.contains("brew tap shivammathur/php") || tapAlias.contains("Error") {
|
||||
print("The user does not appear to have tapped: shivammathur/php")
|
||||
Log.info("The user does not appear to have tapped: shivammathur/php")
|
||||
return false
|
||||
} else {
|
||||
print("The user DOES have the following tapped: shivammathur/php")
|
||||
print("Checking for `php` formula conflicts...")
|
||||
Log.info("The user DOES have the following tapped: shivammathur/php")
|
||||
Log.info("Checking for `php` formula conflicts...")
|
||||
|
||||
let tapPhp = try! JSONDecoder().decode(
|
||||
[HomebrewPackage].self,
|
||||
@@ -47,22 +47,22 @@ class HomebrewDiagnostics {
|
||||
).first!
|
||||
|
||||
if tapPhp.version != PhpSwitcher.brewPhpVersion {
|
||||
print("The `php` formula alias seems to be the different between the tap and core. This could be a problem!")
|
||||
print("Determining whether both of these versions are installed...")
|
||||
Log.warn("The `php` formula alias seems to be the different between the tap and core. This could be a problem!")
|
||||
Log.info("Determining whether both of these versions are installed...")
|
||||
|
||||
let bothInstalled = PhpSwitcher.shared.availablePhpVersions.contains(tapPhp.version)
|
||||
&& PhpSwitcher.shared.availablePhpVersions.contains(PhpSwitcher.brewPhpVersion)
|
||||
|
||||
if bothInstalled {
|
||||
print("Both conflicting aliases seem to be installed, warning the user!")
|
||||
Log.warn("Both conflicting aliases seem to be installed, warning the user!")
|
||||
} else {
|
||||
print("Conflicting aliases are not both installed, seems fine!")
|
||||
Log.info("Conflicting aliases are not both installed, seems fine!")
|
||||
}
|
||||
|
||||
return bothInstalled
|
||||
}
|
||||
|
||||
print("All seems to be OK. No conflicts, both are PHP \(tapPhp.version).")
|
||||
Log.info("All seems to be OK. No conflicts, both are PHP \(tapPhp.version).")
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class Valet {
|
||||
public func startPreloadingSites() {
|
||||
if self.sites.count <= 10 {
|
||||
// Preload the sites and their drivers
|
||||
print("Fewer than or 11 sites found, preloading list of sites...")
|
||||
Log.info("Fewer than or 11 sites found, preloading list of sites...")
|
||||
self.reloadSites()
|
||||
}
|
||||
}
|
||||
@@ -50,17 +50,17 @@ class Valet {
|
||||
|
||||
public func validateVersion() -> Void {
|
||||
if version == "UNKNOWN" {
|
||||
return print("The Valet version could not be extracted... that does not bode well.")
|
||||
return Log.warn("The Valet version could not be extracted... that does not bode well.")
|
||||
}
|
||||
|
||||
if version.versionCompare(Constants.MinimumRecommendedValetVersion) == .orderedAscending {
|
||||
let version = version
|
||||
print("Valet version \(version) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))")
|
||||
Log.warn("Valet version \(version) is too old! (recommended: \(Constants.MinimumRecommendedValetVersion))")
|
||||
DispatchQueue.main.async {
|
||||
Alert.notify(message: "alert.min_valet_version.title".localized, info: "alert.min_valet_version.info".localized(version, Constants.MinimumRecommendedValetVersion))
|
||||
}
|
||||
} else {
|
||||
print("Valet version \(version) is recent enough, OK (recommended: \(Constants.MinimumRecommendedValetVersion))")
|
||||
Log.info("Valet version \(version) is recent enough, OK (recommended: \(Constants.MinimumRecommendedValetVersion))")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,31 +55,31 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
|
||||
|
||||
updatePhpVersionInStatusBar()
|
||||
|
||||
print("Determining broken PHP-FPM...")
|
||||
Log.info("Determining broken PHP-FPM...")
|
||||
// Attempt to find out if PHP-FPM is broken
|
||||
let installation = PhpSwitcher.phpInstall
|
||||
installation.notifyAboutBrokenPhpFpm()
|
||||
|
||||
// Set up the config watchers on launch (these are automatically updated via delegate methods if the user switches)
|
||||
print("Setting up watchers...")
|
||||
Log.info("Setting up watchers...")
|
||||
App.shared.handlePhpConfigWatcher()
|
||||
|
||||
print("Detecting applications...")
|
||||
Log.info("Detecting applications...")
|
||||
// Attempt to load list of applications
|
||||
App.shared.detectedApplications = Application.detectPresetApplications()
|
||||
let appNames = App.shared.detectedApplications.map { app in
|
||||
return app.name
|
||||
}
|
||||
print("Detected applications: \(appNames)")
|
||||
Log.info("Detected applications: \(appNames)")
|
||||
|
||||
// Load the global hotkey
|
||||
App.shared.loadGlobalHotkey()
|
||||
|
||||
// Attempt to find out more info about Valet
|
||||
print("PHP Monitor has extracted the version number of Valet: \(Valet.shared.version)")
|
||||
Log.info("PHP Monitor has extracted the version number of Valet: \(Valet.shared.version)")
|
||||
Valet.shared.validateVersion()
|
||||
Valet.shared.startPreloadingSites()
|
||||
print("PHP Monitor is ready to serve!")
|
||||
Log.info("PHP Monitor is ready to serve!")
|
||||
|
||||
// Schedule a request to fetch the PHP version every 60 seconds
|
||||
DispatchQueue.main.async { [self] in
|
||||
@@ -232,14 +232,14 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
|
||||
@objc func reloadPhpMonitorMenuInBackground() {
|
||||
waitAndExecute {
|
||||
// This automatically reloads the menu
|
||||
print("Reloading information about the PHP installation (in the background)...")
|
||||
Log.info("Reloading information about the PHP installation (in the background)...")
|
||||
}
|
||||
}
|
||||
|
||||
@objc func reloadPhpMonitorMenu() {
|
||||
waitAndExecute {
|
||||
// This automatically reloads the menu
|
||||
print("Reloading information about the PHP installation...")
|
||||
Log.info("Reloading information about the PHP installation...")
|
||||
} completion: {
|
||||
// Add a slight delay to make sure it loads the new menu
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
@@ -378,13 +378,13 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate {
|
||||
DispatchQueue.main.async {
|
||||
window?.addToConsole(string)
|
||||
}
|
||||
print("\(string.trimmingCharacters(in: .newlines))")
|
||||
Log.perf("\(string.trimmingCharacters(in: .newlines))")
|
||||
},
|
||||
didReceiveStdErrData: { string in
|
||||
DispatchQueue.main.async {
|
||||
window?.addToConsole(string)
|
||||
}
|
||||
print("\(string.trimmingCharacters(in: .newlines))")
|
||||
Log.perf("\(string.trimmingCharacters(in: .newlines))")
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ class Preferences {
|
||||
if UserDefaults.standard.bool(forKey: PreferenceName.wasLaunchedBefore.rawValue) {
|
||||
return
|
||||
}
|
||||
print("Saving first-time preferences!")
|
||||
Log.info("Saving first-time preferences!")
|
||||
UserDefaults.standard.setValue(true, forKey: PreferenceName.wasLaunchedBefore.rawValue)
|
||||
UserDefaults.standard.synchronize()
|
||||
}
|
||||
|
||||
@@ -110,6 +110,6 @@ class PrefsVC: NSViewController {
|
||||
// MARK: - Deinitialization
|
||||
|
||||
deinit {
|
||||
print("VC deallocated")
|
||||
Log.perf("PrefsVC deallocated")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class PrefsWC: PMWindowController {
|
||||
if let vc = contentViewController as? PrefsVC {
|
||||
if vc.listeningForHotkeyView != nil {
|
||||
if event.keyCode == Keys.Escape || event.keyCode == Keys.Space {
|
||||
print("A blacklisted key was pressed, canceling listen")
|
||||
Log.info("A blacklisted key was pressed, canceling listen!")
|
||||
vc.listeningForHotkeyView = nil
|
||||
} else {
|
||||
vc.listeningForHotkeyView!.updateShortcut(event)
|
||||
|
||||
@@ -56,7 +56,7 @@ class ProgressWindowController: NSWindowController, NSWindowDelegate {
|
||||
}
|
||||
|
||||
deinit {
|
||||
// print("Deinitializing Progress Window Controller")
|
||||
Log.perf("Deinitializing ProgressWindowController")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class ProgressViewController: NSViewController {
|
||||
@IBOutlet weak var imageViewType: NSImageView!
|
||||
|
||||
deinit {
|
||||
// print("Deinitializing Progress View Controller")
|
||||
Log.perf("Deinitializing ProgressViewController")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -268,6 +268,6 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
|
||||
// MARK: - Deinitialization
|
||||
|
||||
deinit {
|
||||
print("VC deallocated")
|
||||
Log.perf("SiteListVC deallocated")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,16 @@ import Foundation
|
||||
extension App {
|
||||
|
||||
func startWatcher(_ url: URL) {
|
||||
print("No watcher currently active...")
|
||||
Log.info("No watcher currently active...")
|
||||
self.watcher = PhpConfigWatcher(for: url)
|
||||
|
||||
self.watcher.didChange = { url in
|
||||
print("Something has changed in: \(url)")
|
||||
Log.info("Something has changed in: \(url)")
|
||||
|
||||
// Check if the watcher has last updated the menu less than 0.75s ago
|
||||
let distance = self.watcher.lastUpdate?.distance(to: Date().timeIntervalSince1970)
|
||||
if distance == nil || distance != nil && distance! > 0.75 {
|
||||
print("Refreshing menu...")
|
||||
Log.info("Refreshing menu...")
|
||||
MainMenu.shared.reloadPhpMonitorMenuInBackground()
|
||||
self.watcher.lastUpdate = Date().timeIntervalSince1970
|
||||
}
|
||||
@@ -39,7 +39,7 @@ extension App {
|
||||
if self.watcher.url != url || forceReload {
|
||||
self.watcher.disable()
|
||||
self.watcher = nil
|
||||
print("Watcher has stopped watching files. Starting new one...")
|
||||
Log.info("Watcher has stopped watching files. Starting new one...")
|
||||
startWatcher(url)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ class PhpConfigWatcher {
|
||||
self.addWatcher(for: self.url.appendingPathComponent("conf.d/\(file)"), eventMask: .write)
|
||||
}
|
||||
|
||||
print("A watcher exists for the following config paths:")
|
||||
print(self.watchers.map({ watcher in
|
||||
Log.info("A watcher exists for the following config paths:")
|
||||
Log.info(self.watchers.map({ watcher in
|
||||
return watcher.url.relativePath
|
||||
}))
|
||||
}
|
||||
@@ -52,14 +52,14 @@ class PhpConfigWatcher {
|
||||
}
|
||||
|
||||
func disable() {
|
||||
print("Turning off existing watchers...")
|
||||
Log.info("Turning off existing watchers...")
|
||||
self.watchers.forEach { (watcher) in
|
||||
watcher.stopMonitoring()
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
print("An existing config watcher has been deinitialized.")
|
||||
Log.perf("A PhpConfigWatcher has been deinitialized.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user