-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLogToFile.rbbas
More file actions
235 lines (185 loc) · 7.55 KB
/
LogToFile.rbbas
File metadata and controls
235 lines (185 loc) · 7.55 KB
1
#tag ModuleProtected Module LogToFile #tag Method, Flags = &h1 Protected Function DeleteOldLogs(numLogFilesToKeep as integer) As Boolean dim loopVar as integer dim numLogFolderItems as integer dim logFileBaseName as string dim logItem as FolderItem dim numLogFilesFound as integer dim logFileNames() as string dim tempStr as string dim numLogFilesToDelete as integer LogToFile("LogToFile.DeleteOldLogs: ---->") if (DebugLogFolder = nil) then LogToFile("LogToFile.DeleteOldLogs: ERROR: LogFolderItem is nil, no directory to delete. Returning false.") return false end if if (numLogFilesToKeep < 2) then LogToFile("LogToFile.DeleteOldLogs: numLogFilesToKeep is less than 2, so no files to delete. Returning true.") LogToFile("LogToFile.DeleteOldLogs: <----") return true end if // Delete old log files next LogToFile("LogToFile.DeleteOldLogs: Received number of log files to keep: " + str(numLogFilesToKeep)) numLogFolderItems = DebugLogFolder.Count LogToFile("LogToFile.DeleteOldLogs: Number of items in the log folder: " + str(numLogFolderItems)) if (numLogFolderItems < numLogFilesToKeep) then LogToFile("LogToFile.DeleteOldLogs: The number of items in the log folder is less than the number of log files to keep, nothing to scan. Exiting.") LogToFile("LogToFile.DeleteOldLogs: <----") return true end if logFileBaseName = NthField(DebugLogFile.Name,".",1) // Need to get the count of the number of log files first before counting them otherwise we'll count the other items as log file counts for loopVar = 1 to numLogFolderItems logItem = DebugLogFolder.Item(loopVar) if ( left(logItem.Name,len(logFileBaseName)) = logFileBaseName ) then numLogFilesFound = numLogFilesFound + 1 logFileNames.Append logItem.Name end if next LogToFile("LogToFile.DeleteOldLogs: Number of log files found: " + str(UBound(logFileNames)+1) ) numLogFilesToDelete = UBound(logFileNames) - numLogFilesToKeep // Keep in mind that the array is zero based... // Delete log files until N are left: for loopVar = 0 to numLogFilesToDelete tempStr = logFileNames(loopVar) LogToFile("LogToFile.DeleteOldLogs: Processing log file: '" + logFileNames(loopVar) + "'") if (not(logFileNames(loopVar) = DebugLogFile.Name)) then // Check the folderitem name doesn't match the name of the active log file before deleting it LogToFile("LogToFile.DeleteOldLogs: The found log item does NOT match the current active log filed named '"+ DebugLogFile.Name + "' and WILL BE DELETED.") if (DebugLogFolder.Child(logFileNames(loopVar)).Exists) then LogToFile("LogToFile.DeleteOldLogs: Log file exists, deleting it ...") DebugLogFolder.Child(logFileNames(loopVar)).Delete else LogToFile("LogToFile.DeleteOldLogs: Log file does NOT exist, nothing to delete.") end if else LogToFile("LogToFile.DeleteOldLogs: The found log item MATCHES the current active log filed named '"+ DebugLogFile.Name + "', will NOT be deleted.") end if next LogToFile("LogToFile.DeleteOldLogs: <----") return true End Function #tag EndMethod #tag Method, Flags = &h1 Protected Function Initialize(debugFileName as string, appFolder as FolderItem) As Boolean if (debugFileName = "") then return false end if if (appFolder = nil) then return false end if DebugLogFolder = appFolder.child("IdleLogout") if (not(DebugLogFolder.Exists)) then DebugLogFolder.CreateAsFolder DebugLogFolder.Permissions = &o777 if ((DebugLogFolder.Exists) and (DebugLogFolder.Directory)) then // beep // MsgBox "Created 'Logs' folder." + EndOfLine.Macintosh + EndOfLine.Macintosh + "In order to save the run log file, I created a 'Logs' folder at the path of '" + Globals.gAppFolderItem.ShellPath + "'." else // beep // msgBox "Failed to create the 'Logs' folder for storing the run log file." return false end if end if DebugLogFile = DebugLogFolder.child(debugFileName) if not(DebugLogFile.IsWriteable) then // beep // MsgBox "Permissions prevent me from writing to the run log file in the 'Logs' folder." + EndOfLine.Macintosh + EndOfLine.Macintosh + "Created run log file in '/tmp' directory as a backup." DebugLogFile = Volume(0).Child("tmp").Child(debugFileName) end if if (not(DebugLogFile.exists)) then fileStream=TextOutputStream.Create(DebugLogFile) fileStream.Close DebugLogFile.Permissions = &o777 LogToFile("LogToFile.Initialize: New log file created.") end if return true End Function #tag EndMethod #tag Method, Flags = &h0 Sub LogToFile(logMessage as string) // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // WARNING: DO NOT CALL LogToFile in this method or you will have circular references! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Dim theTime as date theTime = new date if (DebugLogFile = NIL) then beep msgBox "LogToFile error: logFile = NIL. Unable to save to log file." return end if if (DebugLogFile.Exists) then fileStream = TextOutputStream.Append(DebugLogFile) else return end if if (fileStream <> NIL) then // Log the message if it does not contain "<DEBUG>", OR if the ENABLE_DEBUG_LOGGING config pref key is "YES" AND the logMessage contains "<DEBUG>": if ( ( (InStr(logMessage,"<DEBUG>")=0)) or ( (InStr(logMessage,"<DEBUG>")>0) and (Globals.gDebugLoggingEnabled)) ) then #if TargetWin32 then fileStream.Write (theTime.ShortDate + " " + theTime.LongTime + ": " + logMessage + EndOfLine.Windows) // Winders Line Feeds #else fileStream.Write (theTime.ShortDate + " " + theTime.LongTime + ": " + logMessage + EndOfLine.UNIX) // Unix line feeds #endif end if fileStream.Close return else beep msgBox "LogToFile error: fileStream = NIL - can't write to log file." + EndOfLine + EndOfLine + "Is the file possibly busy or open in an editor?" return end if End Sub #tag EndMethod #tag Method, Flags = &h1 Protected Function openLogFile() As Boolean DebugLogFile.launch(true) return true End Function #tag EndMethod #tag Property, Flags = &h21 Private DebugLogFile As FolderItem #tag EndProperty #tag Property, Flags = &h21 Private DebugLogFolder As FolderItem #tag EndProperty #tag Property, Flags = &h21 Private fileStream As TextOutputStream #tag EndProperty #tag ViewBehavior #tag ViewProperty Name="Index" Visible=true Group="ID" InitialValue="-2147483648" InheritedFrom="Object" #tag EndViewProperty #tag ViewProperty Name="Left" Visible=true Group="Position" InitialValue="0" InheritedFrom="Object" #tag EndViewProperty #tag ViewProperty Name="Name" Visible=true Group="ID" InheritedFrom="Object" #tag EndViewProperty #tag ViewProperty Name="Super" Visible=true Group="ID" InheritedFrom="Object" #tag EndViewProperty #tag ViewProperty Name="Top" Visible=true Group="Position" InitialValue="0" InheritedFrom="Object" #tag EndViewProperty #tag EndViewBehaviorEnd Module#tag EndModule