Skip to content

Commit

Permalink
Added log rotate function - crude for now, should probably look at ht…
Browse files Browse the repository at this point in the history
  • Loading branch information
bartreardon committed Sep 5, 2023
1 parent f03950b commit 70bdfcd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Outset/Outset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ let loginOncePrivilegedDir = outsetDirectory+"login-privileged-once"
let onDemandDir = outsetDirectory+"on-demand"
let shareDirectory = outsetDirectory+"share/"
let logDirectory = outsetDirectory+"logs"
let logFile = logDirectory+"/outset.log"
let logFileName = "outset.log"
let logFilePath = logDirectory+"/"+logFileName

let onDemandTrigger = "/private/tmp/.io.macadmins.outset.ondemand.launchd"
let loginPrivilegedTrigger = "/private/tmp/.io.macadmins.outset.login-privileged.launchd"
Expand All @@ -46,6 +47,7 @@ var prefs = loadOutsetPreferences()
// Log Stuff
let bundleID = Bundle.main.bundleIdentifier ?? "io.macadmins.Outset"
let osLog = OSLog(subsystem: bundleID, category: "main")
let logRotateDays: Int = 30

// Logic insertion point
@main
Expand Down Expand Up @@ -155,6 +157,9 @@ struct Outset: ParsableCommand {
}

if boot {
// perform log file rotation
performLogRotation(logFolderPath: logDirectory, logFileBaseName: logFileName)

writeLog("Processing scheduled runs for boot", logLevel: .debug)
ensureWorkingFolders()

Expand Down
32 changes: 32 additions & 0 deletions Outset/Utils/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,35 @@ func detachDmg(dmgMount: String) -> String {
}
return output.trimmingCharacters(in: .whitespacesAndNewlines)
}

func performLogRotation(logFolderPath: String, logFileBaseName: String, maxLogFiles: Int = 30) {
let fileManager = FileManager.default
let currentDay = Calendar.current.component(.day, from: Date())

// Check if the day has changed
let newestLogFile = logFolderPath + "/" + logFileBaseName
if fileManager.fileExists(atPath: newestLogFile) {
let fileCreationDate = try? fileManager.attributesOfItem(atPath: newestLogFile)[.creationDate] as? Date
if let creationDate = fileCreationDate {
let dayOfCreation = Calendar.current.component(.day, from: creationDate)
if dayOfCreation != currentDay {
// rotate files
for archivedLogFile in (1...maxLogFiles).reversed() {
let sourcePath = logFolderPath + "/" + (archivedLogFile == 1 ? logFileBaseName : "\(logFileBaseName).\(archivedLogFile-1)")
let destinationPath = logFolderPath + "/" + "\(logFileBaseName).\(archivedLogFile)"

if fileManager.fileExists(atPath: sourcePath) {
if archivedLogFile == maxLogFiles {
// Delete the oldest log file if it exists
try? fileManager.removeItem(atPath: sourcePath)
} else {
// Move the log file to the next number in the rotation
try? fileManager.moveItem(atPath: sourcePath, toPath: destinationPath)
}
}
}
writeLog("Logrotate complete", logLevel: .debug)
}
}
}
}
10 changes: 5 additions & 5 deletions Outset/Utils/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StandardError: TextOutputStream {
if #available(macOS 10.15.4, *) {
try! FileHandle.standardError.write(contentsOf: Data(string.utf8))
} else {
// Fallback on earlier versions
// Fallback on earlier versions (should work on pre 10.15.4 but untested)
if let data = string.data(using: .utf8) {
FileHandle.standardError.write(data)
}
Expand Down Expand Up @@ -65,14 +65,14 @@ func writeFileLog(message: String, logLevel: OSLogType) {
if logLevel == .debug && !debugMode {
return
}
let logFileURL = URL(fileURLWithPath: logFile)
if !checkFileExists(path: logFile) {
let logFileURL = URL(fileURLWithPath: logFilePath)
if !checkFileExists(path: logFilePath) {
FileManager.default.createFile(atPath: logFileURL.path, contents: nil, attributes: nil)
let attributes = [FileAttributeKey.posixPermissions: 0o666]
do {
try FileManager.default.setAttributes(attributes, ofItemAtPath: logFileURL.path)
} catch {
printStdErr("\(oslogTypeToString(.error).uppercased()): Unable to create log file at \(logFile)")
printStdErr("\(oslogTypeToString(.error).uppercased()): Unable to create log file at \(logFilePath)")
printStdErr(error.localizedDescription)
return
}
Expand All @@ -90,7 +90,7 @@ func writeFileLog(message: String, logLevel: OSLogType) {
fileHandle.seekToEndOfFile()
fileHandle.write(logEntry.data(using: .utf8)!)
} catch {
printStdErr("\(oslogTypeToString(.error).uppercased()): Unable to read log file at \(logFile)")
printStdErr("\(oslogTypeToString(.error).uppercased()): Unable to read log file at \(logFilePath)")
printStdErr(error.localizedDescription)
return
}
Expand Down

0 comments on commit 70bdfcd

Please sign in to comment.