Back to community

free snippet: better print() that includes the resource name and line

23 rep185 views1 min read

tired of seeing [script:resource] for every print and having to grep through 80 resources to find which one printed something.

local resourceName = GetCurrentResourceName()

local function dprint(...)
    local info = debug.getinfo(2, "Sl")
    local file = info.source:match("@?(.+)$") or "?"
    local line = info.currentline or "?"
    print(('[%s:%s:%d]'):format(resourceName, file, line), ...)
end

-- usage
dprint('hello', 'world', { foo = 1 })
-- output: [my_resource:server.lua:42] hello world table: 0x...

drop into a shared util and import everywhere. small thing but saves hours over a year.

13 2 commentsSign in to vote

Comments (2)

Sign in to leave a comment.
  • add a log level prefix (INFO/WARN/ERR) and you basically have a real logger. nice base

    3 points
  • simple but huge QoL upgrade. stealing this

    2 points