Notifications
Clear all

[Closed] Get current function name

Is it possible to get current function name? I was thinking about creating function that prints log to listener (or file). Something like this:

(
   	fn DebugPrinter fnName message =
   	(
   		format "%::% 
" fnName message
   	)
   	
   	fn someFunction1 =
   	(
   		DebugPrinter thisFunctionName "My message from someFunction1"  
   	)
   	
   	fn someFunction2 =
   	(
   		DebugPrinter thisFunctionName "My message from someFunction2"  
   	)
   )

And the output would be:

someFunction1()::My message from someFunction1
   someFunction2()::My message from someFunction2

So is there any way to get that ‘thisFunctionName’ ?

EDIT: Looks like assert-function can print lots of data and function name to listener, but it needs expression and propably cant write info to file. Also assert-function seems kinda rough because i might want to print other info than actual errors based on expressions.

9 Replies

I’m not sure why you’d want that. Isn’t it easier to just keep the format statement inside someFunction1 or someFunction2? Or just having the function name defined inside the function directly?

I’ve done something similar to what you’re doing here, and have just settled on passing the name of the current function to the debugger as an extra argument. Not ideal, but it works.

You could send the results of a call to stack() to a stringstream and then extract the file name from the dump it gives you.

I’d be interested to know if there is an easier way to get the current function name (and file offset!)

…Denis?

is it not the same as print function name on debug level inside the function?


global debugFunctionPointer
fn myFunction debug:off &globalPointer: = 
(
 if debug do
 (
  format "the function: %
" (myFunction as string)
  globalPointer = myFunction 
  ok
 )
/*
 code
*/ 
)
myFunction debug:on globalPointer:&debugFunctionPointer

?

1 Reply
(@biddle)
Joined: 11 months ago

Posts: 0

Yeah, he says sheepishly, it is.

Looking at the results of the “stack()” call got me thinking it would be nice to have that info returned in a struct. You could examine the call stack yourself and provide additional insight in any debug spam you want to print out. Knowing what function called you would let you customize the behaviour of the function.

That’s too easy. There’s must some more complicated way of doing it?

the using stack() and threads() sounds very interesting… i always thought about my own customizable script debugger… eh, unfortunately i see no way to break a code without popping up the maxscript debugger dialog.

Here is a start using stack().

Have not tested it in all situations, but it seems to work in the general cases:

fn printFunctionName =
 (
 	local ss =  stringstream ""
 	stack showLocals:false firstFrameOnly:false to:ss
 	seek ss 0 --go to start of stream
 	skipToString ss "[stack level: 1]
"  --skip to level 1 which is the caller
 	local theLine = readLine ss --read the next line and print the fn name:
 	format ">Called Function '%'
" (filterString theLine "; ")[3]
 )
 
 (
 	fn test =
 	(
 		printFunctionName()
 	)
 
 	rollout testRollout "Test"
 	(
 		button btn_test "Press Me!"
 		on btn_test pressed do printFunctionName()
 		
 		on testRollout open do printFunctionName()	
 		
 		on testRollout close do printFunctionName()	
 		
 		on testRollout moved pos do printFunctionName()	
 	)
 
 	test() --call local function
 
 	createDialog testRollout --create dialog, watch it print (handlers are function too)
 )

Looks like Bobo beat me by parsing stack()

	   
     fn printDebug message:"" debugLevel:1 =
      (
 local debugLevels = #(true,false,true)
 if(debugLevel > 3 OR debugLevel < 1) then debugLevel = 1
 			
       local sstream = stringstream ""
       stack showLocals:false to:sstream
       seek sstream 0
       skipToString sstream "[stack level: 1]
"
       local stackLine = readLine sstream
       local lineArr = filterString stackLine " ;"
       local functionName = lineArr[3]
       local scriptFile = filenameFromPath lineArr[5]
       local fromPos = lineArr[7]
       local fromLine = lineArr[9]
 
       if(debugLevels[debugLevel]) then format "% - %[%,%]::% %
" localtime scriptFile fromLine fromPos functionName message
     )
     
  And output was:

     3.11.2009 8:48:28 - debug_test.ms[9,153]::test1() 
      3.11.2009 8:48:28 - debug_test.ms[20,329]::test3() some message
      3.11.2009 8:48:28 - debug_test.ms[9,153]::test1() 
      3.11.2009 8:48:28 - debug_test.ms[15,255]::test2() 
     
  So far this has work nicely here, although I've just tested it in simple scripts. I added debuglevel value so it's easy to decide what to print.