lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, 22 Mar 2022 at 12:05, Matthew Wild <mwild1@gmail.com> wrote:
On Tue, 22 Mar 2022 at 06:21, Eduardo Ochs <eduardoochs@gmail.com> wrote:
Hi list,

where can I find a version in Lua of debug.traceback()?

Probably not 100% what you want, but here is a reimplementation of debug.traceback() in Lua, though it has a different, much more verbose, output format (though it also has a 'table' output so you could try to generate something more like Lua's own if you want): https://hg.prosody.im/trunk/file/tip/util/debug.lua#l121


Regards,
Matthew

 Hi Matthew,

Thanks and WOW!!!

I like the format of the traceback from Prosody much more than the
default format in Lua! I didn't learn how to run the tests that you
suggested, but I adapted the middle part of the _traceback() function
in debug.lua(), and got this:

  -- Adapted from (the middle part of) the traceback function
  -- of Prosody. See the message by Matthew Wild in 2022mar22
  -- in <http://lua-users.org/lists/lua-l/2022-03/>.
  prosodytraceback = function (info)
    local line
    local func_type = info.namewhat.." "
    local source_desc = (info.short_src == "[C]" and "C code")
                      or info.short_src or "Unknown"
    if func_type == " " then func_type = "" end
    if info.short_src == "[C]" then
      line = "[ C ] "..func_type.."C function "
           ..(info.name and ("%q"):format(info.name) or "(unknown name)")
    elseif info.what == "main" then
      line = "[Lua] "..info.short_src.." line "..info.currentline
    else
      local name = info.name or " "
      if name ~= " "
      then name = ("%q"):format(name)
      end
      if func_type == "global " or func_type == "local "
      then func_type = func_type.."function "
      end
      line = "[Lua] "..info.short_src.." line "..
        info.currentline.." in "..func_type..name..
        " (defined on line "..info.linedefined..")"
    end
    return line
  end

Thanks again =),
  Eduardo Ochs
  http://angg.twu.net/#eev