Advertisement
DOGGYWOOF

Untitled

Nov 20th, 2024
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. -- Function to check and create the /.tmp directory and subdirectory if they don't exist
  2. local function checkTmpDirectory(subDir)
  3. local tmpPath = "/.tmp/" .. subDir
  4. if not fs.exists(tmpPath) then
  5. fs.makeDir(tmpPath)
  6. print("Directory /.tmp/" .. subDir .. " created.")
  7. end
  8. end
  9.  
  10. -- Function to get the real-time formatted timestamp (HH:MM:SS) for continuous logging
  11. local function getRealTime()
  12. local time = os.date("*t") -- Get the current time as a table
  13. local realTime = string.format("%04d-%02d-%02d %02d:%02d:%02d",
  14. time.year, time.month, time.day,
  15. time.hour, time.min, time.sec)
  16. return realTime
  17. end
  18.  
  19. -- Function to log errors with the given name and log message
  20. local function logError(logName, logMessage, subDir)
  21. -- Ensure the /.tmp/<subDir> directory exists
  22. checkTmpDirectory(subDir)
  23.  
  24. -- Get the real-time timestamp
  25. local currentTime = getRealTime()
  26.  
  27. -- Construct the log file path
  28. local logFilePath = "/.tmp/" .. subDir .. "/" .. logName .. "_log.txt"
  29.  
  30. -- Open the log file for appending
  31. local logFile = fs.open(logFilePath, "a")
  32.  
  33. -- Write detailed log entry
  34. logFile.writeLine("--------------------------------------------------")
  35. logFile.writeLine("Timestamp: " .. currentTime)
  36. logFile.writeLine("Log Name: " .. logName)
  37. logFile.writeLine("Message: " .. logMessage)
  38. logFile.writeLine("Log Directory: /.tmp/" .. subDir)
  39. logFile.writeLine("--------------------------------------------------")
  40.  
  41. -- Close the log file
  42. logFile.close()
  43.  
  44. print("Log written to " .. logFilePath)
  45. end
  46.  
  47. -- Function to parse and execute the command from arguments
  48. local function executeCommand(...)
  49. local args = {...}
  50.  
  51. if #args < 3 then
  52. print("Usage: <msg> <logName> <subDirectory>")
  53. return
  54. end
  55.  
  56. -- Extract the arguments
  57. local logMessage = args[1] -- The first argument is the log message
  58. local logName = args[2] -- The second argument is the log name
  59. local subDir = args[3] -- The third argument is the subdirectory
  60.  
  61. -- Call the logError function with the provided inputs
  62. logError(logName, logMessage, subDir)
  63. end
  64.  
  65. -- Capture the arguments passed in the shell
  66. executeCommand(...)
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement