Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check and create the /.tmp directory and subdirectory if they don't exist
- local function checkTmpDirectory(subDir)
- local tmpPath = "/.tmp/" .. subDir
- if not fs.exists(tmpPath) then
- fs.makeDir(tmpPath)
- print("Directory /.tmp/" .. subDir .. " created.")
- end
- end
- -- Function to get the real-time formatted timestamp (HH:MM:SS) for continuous logging
- local function getRealTime()
- local time = os.date("*t") -- Get the current time as a table
- local realTime = string.format("%04d-%02d-%02d %02d:%02d:%02d",
- time.year, time.month, time.day,
- time.hour, time.min, time.sec)
- return realTime
- end
- -- Function to log errors with the given name and log message
- local function logError(logName, logMessage, subDir)
- -- Ensure the /.tmp/<subDir> directory exists
- checkTmpDirectory(subDir)
- -- Get the real-time timestamp
- local currentTime = getRealTime()
- -- Construct the log file path
- local logFilePath = "/.tmp/" .. subDir .. "/" .. logName .. "_log.txt"
- -- Open the log file for appending
- local logFile = fs.open(logFilePath, "a")
- -- Write detailed log entry
- logFile.writeLine("--------------------------------------------------")
- logFile.writeLine("Timestamp: " .. currentTime)
- logFile.writeLine("Log Name: " .. logName)
- logFile.writeLine("Message: " .. logMessage)
- logFile.writeLine("Log Directory: /.tmp/" .. subDir)
- logFile.writeLine("--------------------------------------------------")
- -- Close the log file
- logFile.close()
- print("Log written to " .. logFilePath)
- end
- -- Function to parse and execute the command from arguments
- local function executeCommand(...)
- local args = {...}
- if #args < 3 then
- print("Usage: <msg> <logName> <subDirectory>")
- return
- end
- -- Extract the arguments
- local logMessage = args[1] -- The first argument is the log message
- local logName = args[2] -- The second argument is the log name
- local subDir = args[3] -- The third argument is the subdirectory
- -- Call the logError function with the provided inputs
- logError(logName, logMessage, subDir)
- end
- -- Capture the arguments passed in the shell
- executeCommand(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement