oof icon indicating copy to clipboard operation
oof copied to clipboard

Class for logging

Open LuaFan2 opened this issue 5 years ago • 1 comments

I think we could make a special class that would allow you to log errors/warnings with a single style easily. This is certainly not a very important addition, but it is not useless either.

LuaFan2 avatar Aug 17 '20 14:08 LuaFan2

Currently we use print() for logging on both client and server.

We have overloaded Lua's print() function (see overloads.lua) to output the resource name that the error occurred in and the timestamp:

local OgLuaPrint = print
function print(...)
    local args = {...}
    local output_str = string.format("%s[%s]%s", 
        "^5", tostring(GetCurrentResourceName()), "^7")

    if IsServer then
        local t = os.date("*t", os.time())
        output_str = output_str .. " [" .. f_time(t.hour) .. ":" .. f_time(t.min) .. ":" .. f_time(t.sec) .. "]: "
    elseif IsClient then
        local year, month, day, hour, min, sec = GetPosixTime()
        output_str = output_str .. " [" .. f_time(hour) .. ":" .. f_time(min) .. ":" .. f_time(sec) .. "]: "
    end

    for index, arg in ipairs(args) do
        output_str = output_str .. tostring(arg)
    end

    OgLuaPrint(output_str)
end

Here you can style the output to the logs files in any way you want and it will be effective for any print() statement.

Regarding errors - we currently don't catch the script errors that happen. Doing so would require wrapping all our code in pcall(). By default, the client and server errors output to the log so that's all we need. You can create your own script errors by using Lua's error() or assert() functions.

rdev34 avatar Aug 17 '20 14:08 rdev34