Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Class for logging #5

Open
LuaFan2 opened this issue Aug 17, 2020 · 1 comment
Open

Class for logging #5

LuaFan2 opened this issue Aug 17, 2020 · 1 comment

Comments

@LuaFan2
Copy link

LuaFan2 commented Aug 17, 2020

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.

@rdev34
Copy link
Collaborator

rdev34 commented Aug 17, 2020

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants