Better DEBUG testing setup; fix munkilog to create a missing log file before attempting to append to it

This commit is contained in:
Greg Neagle
2024-08-27 13:08:25 -07:00
parent 1920910179
commit 362ca1442a
3 changed files with 18 additions and 7 deletions
+5 -1
View File
@@ -33,7 +33,11 @@ let EXIT_STATUS_INVALID_PARAMETERS = 200
let EXIT_STATUS_ROOT_REQUIRED = 201
let BUNDLE_ID = "ManagedInstalls" as CFString
let DEFAULT_MANAGED_INSTALLS_DIR = "/Library/Managed Installs"
#if DEBUG
let DEFAULT_MANAGED_INSTALLS_DIR = "/Users/Shared/Managed Installs"
#else
let DEFAULT_MANAGED_INSTALLS_DIR = "/Library/Managed Installs"
#endif
let DEFAULT_GUI_CACHE_AGE_SECS = 3600
let WRITEABLE_SELF_SERVICE_MANIFEST_PATH = "/Users/Shared/.SelfServeManifest"
+8 -1
View File
@@ -35,7 +35,11 @@ func munkiLog(_ message: String, logFile: String = "") {
dateformatter.dateFormat = "MMM dd yyyy HH:mm:ss Z"
let timestamp = dateformatter.string(from: Date())
let logString = "\(timestamp) \(message)\n"
let defaultLogPath = pref("LogFile") as? String ?? managedInstallsDir(subpath: "Logs/ManagedSoftwareUpdate.log")
#if DEBUG
let defaultLogPath = "/Users/Shared/Managed Installs/Logs/ManagedSoftwareUpdate.log"
#else
let defaultLogPath = pref("LogFile") as? String ?? managedInstallsDir(subpath: "Logs/ManagedSoftwareUpdate.log")
#endif
var logPath = ""
if logFile.isEmpty {
logPath = defaultLogPath
@@ -44,6 +48,9 @@ func munkiLog(_ message: String, logFile: String = "") {
logPath = (logPath as NSString).appendingPathComponent(logFile)
}
if let logData = logString.data(using: String.Encoding.utf8) {
if !pathExists(logPath) {
FileManager.default.createFile(atPath: logPath, contents: nil)
}
if let fh = FileHandle(forUpdatingAtPath: logPath) {
let _ = fh.seekToEndOfFile()
fh.write(logData)
+5 -5
View File
@@ -191,11 +191,11 @@ func intPref(_ prefName: String) -> Int? {
func managedInstallsDir(subpath: String? = nil) -> String {
// convenience function to return the path to the Managed Installs dir
// or a subpath of that directory
let managedInstallsDir: String = if pref("DeveloperDebug") as? Bool ?? false {
"/Users/Shared/Managed Installs"
} else {
pref("ManagedInstallDir") as? String ?? DEFAULT_MANAGED_INSTALLS_DIR
}
#if DEBUG
let managedInstallsDir = "/Users/Shared/Managed Installs"
#else
let managedInstallsDir = pref("ManagedInstallDir") as? String ?? DEFAULT_MANAGED_INSTALLS_DIR
#endif
if let subpath {
return (managedInstallsDir as NSString).appendingPathComponent(subpath)
}