Game Development Community

GetCurrentFunctionName() ?

by Orion Elenzil · in Torque Game Engine · 02/13/2007 (11:39 am) · 7 replies

I write a lot of TS debug statements like:


function foo()
{
   if (there's a problem)
   {
      error("in function foo() there's a problem");
   }
}

i love to replace that with:
error("in function" SPC getCurrentFunctionName() SPC "there's a problem");


is this already in ?

if not, any pointers ?

tia,
ooo

#1
02/16/2007 (6:44 am)
I know in C++ you can use __FUNCTION__ to get the function name in string format. As far as getting a script function name, that's a good question.

I bet we can find the answer somewhere in the Tenet debugger code.

*EDIT* As I look through the Telnet Debugger, I find references and execution on the class Codeblock, which store the name of the function.

Not really sure where to go on this, but it might be a starting point.
#2
02/16/2007 (10:14 am)
Heya -

when i get around to it
i'm planning on implementing this by looking at the backTrace() console function.
#3
02/21/2007 (10:25 am)
Here it is.
caveat: i am not a lawyer, i am not a medical professional. i am also not familiar with the whole notion of scope in TGE, so please consult a doctor and a lawyer before using this code for anything critical.

ConsoleFunction(getScopeName, const char*, 1, 2, "return the script scope name, either current or parental")
{
   S32 depth = 0;
   char* ret = Con::getReturnBuffer(1024);
   ret[0]    = '[[62880fac258c5]]';

   if (argc > 1)
      depth = dAtoi(argv[1]);

   depth = gEvalState.stack.size() - depth - 1;

   if (depth >= 0)
   {
      if(gEvalState.stack[depth]->scopeNamespace && gEvalState.stack[depth]->scopeNamespace->mName)
      {
         dStrcat(ret, gEvalState.stack[depth]->scopeNamespace->mName);
         dStrcat(ret, "::");
      }
      dStrcat(ret, gEvalState.stack[depth]->scopeName);
   }
   else
   {
      dStrcat(ret, "none");
   }

   return ret;
}

usage:
function foo()
{
   bar();
}

function bar()
{
   echo("current     scope is" SPC getScopeName());
   echo("parent      scope is" SPC getScopeName(1));
   echo("grandparent scope is" SPC getScopeName(2));
}

calling foo() from the console yields:
[2/21/07 10:24:34][Inf][General] current     scope is bar
[2/21/07 10:24:34][Inf][General] parent      scope is foo
[2/21/07 10:24:34][Inf][General] grandparent scope is (none)
#4
02/21/2007 (10:26 am)
Very very nice Orion...
#5
02/21/2007 (10:42 am)
Aa crud, some problem here.
it's not getting the grandparent when i call into a player method from a serverCmd.
update soon, hopefully..
#6
02/21/2007 (10:58 am)
Okay, fixed and improved.
the main problem was i had a "> 0" where i should've had a ">= 0",
but also added more namespace clarification so that we'll get "player::foo" instead of just "foo".
#7
02/27/2007 (10:20 am)
Submitted a resource for this,
plus an addition so that undefined variable warnings include the parent and the grandparent.
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12426