Jump to content

Save time


Hugos

Recommended Posts

  • Moderators

https://wiki.multitheftauto.com/wiki/OnResourceStop

Its log when the resource stops, make a new resource only for this code.

local filePath = "shutdown.log" -- log file path
addEventHandler("onResourceStop", resourceRoot, function() -- call this function at resource stop
    -- logfile not found, create it first.
    if not fileExists(filePath) then
        fileCreate(filePath)
    end
	
    -- get the current datetime and make the new line what we want to insert end of the file
    local DateTime = getRealTime()
    DateTime.year = DateTime.year + 1900
    DateTime.month = DateTime.month + 1
    local timeString = ("%d-%02d-%02d %02d:%02d:%02d"):format(DateTime.year, DateTime.month, DateTime.monthday, DateTime.hour, DateTime.minute, DateTime.second)
    local newLine = "["..timeString.."] Server shutdown!"
	
    -- open file
    local file = fileOpen(filePath)
    local fileSize = fileGetSize(file) -- length of text in file
    fileSetPos(file, fileSize) -- move cursor the end of the file (because we want to write to the end of the file)
    fileWrite(file, newLine.."\n") -- add the new line with linebreak on the end
    fileClose(file) -- close the file (save)
end)
Edited by stPatrick
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...