[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  A function to help debug function calls

A function to help debug function calls

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Nick Gammon   Australia  (23,019 posts)  [Biography] bio   Forum Administrator
Date Mon 07 May 2007 03:13 AM (UTC)

Amended on Mon 07 May 2007 05:00 AM (UTC) by Nick Gammon

The code below illustrates how to make a special debugging version of any Lua function (including the MUSHclient world functions).

What it does is save the original function into a local variable, and then replace the function of that name in the global table (_G) with a debug version.

The debug version accepts any number of arguments (by using "..."), and then displays:

  • The name of the function that was called
  • Where it was called from (function name and line number)
  • A list of all the arguments which were sent to the function.

Then it calls the original function, passing any arguments that were sent to it.

It uses TraceOut to display this information, so to see it you need to turn Trace on (Game Menu -> Trace).

function DebugFunction (f)
  assert (type (f) == "string" and type (_G [f]) == "function", 
          "DebugFunction must be called with a function name")
  local old_f = _G [f]  -- save original one for later
  _G [f] = function (...)  -- make new function
    local t = debug.getinfo (2, "ln")  -- get line and caller name
    t.name = t.name or "<unknown place>"
    TraceOut ("Function ", f, " called.")  -- show name
    TraceOut ("Called from ", t.name, " at line ", t.currentline)  -- show caller 
    local n = select ("#", ...)  -- number of arguments
    if n == 0 then
      TraceOut ("No arguments.")
      for i = 1, n do  -- show each argument
        TraceOut ("Argument ", i, " = ", tostring ((select (i, ...))))
      end -- for each argument
    end -- have some arguments
    old_f (...)  -- call original function now
  end -- debug version of the function
end -- DebugFunction 

Now to use it, you call DebugFunction for each function you want to debug, passing the function name as an argument (you must get the spelling and capitalization exactly right):

DebugFunction ("ColourNote")
DebugFunction ("BroadcastPlugin")

This effectively replaces (in this example) ColourNote and BroadcastPlugin with debugging versions.

Now to test it:

function mytest ()
  ColourNote ("red", "green", "hello, world")  -- line 25
  BroadcastPlugin (100, "some message")   -- line 26
end -- mytest

mytest ()


TRACE: Function ColourNote called.
TRACE: Called from mytest at line 25
TRACE: Argument 1 = red
TRACE: Argument 2 = green
TRACE: Argument 3 = hello, world
hello, world
TRACE: Function BroadcastPlugin called.
TRACE: Called from mytest at line 26
TRACE: Argument 1 = 100
TRACE: Argument 2 = some message

You can see from the trace output, that our debug version was called from "mytest" function, we are told the exact line number, and we are told the exact arguments.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (23,019 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 07 May 2007 03:42 AM (UTC)
If you do this multiple times, the debug functions will "chain" and you will get multiple debugging displays. You would need to do a "reload script file" to remove all the debugging stuff.

Also, this particular version replaces the function in the global table (_G) not the world table. Thus if you did:

world.ColourNote ("", "", "blah blah")

... you would not get the debug version, because only the one in the _G table had been replaced. (There are not really 2 versions - the global table has a metatable put on it, so that if a function is not found there, it looks in the world table).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]