3

From an XQuery program I want to log the name of the current function. Is there a way to get the name of the current function without throwing an exception?

I can get the function information by throwing an exception and logging stack frame information. But is there a way to get the current function name without throwing an exception like this

xquery version "1.0-ml";
declare function local:logStuff( $str as xs:string )
{
    try { fn:error() }
    catch( $ex ) {
      let $frame := ($ex/error:stack/error:frame)[2]
      let $function as xs:string := $frame/error:operation
      let $line as xs:numeric := $frame/error:line
      let $column as xs:numeric := $frame/error:column
      return 
          fn:concat( 
              "message: '", $str, "' In Function: ", $function,
              " line: ", $line, " column: ", $column
          )
    }
};

declare function local:callingFunction()
{
  local:logStuff( "I want to log this line of code with function info.")
};

local:callingFunction()
,
"And continue with the program ..."

1 Answers1

0

I think trace events will provide what you are seeking. For more information read about them here: https://help.marklogic.com/Knowledgebase/Article/View/how-to-use-diagnostic-trace-events

They can be created using xdmp:trace in your code: https://docs.marklogic.com/xdmp:trace

Here is an example of how I use them in my code:

declare function myNamespace:functionName($listOfStrings){
    let $varIWantToSee := fn:string-join($listOfStrings, ", ")
    let $_ := xdmp:trace("myNamespace:functions",("", "===== myNamespace:functionName:varIWantToSee =====", $varIWantToSee)
    ...
    return $whatever
};

Trace events are great since they can be turned on and off as needed as opposed to xdmp:log. The information will show up in the associated application server error log. Its the best way I have found to fully test my code and is really helpful in troubleshooting.

Here is an example of the log entry:

TIMESTAMP Info: [Event:id=myNamespace:functions]
TIMESTAMP Info:+===== myNamespace:functionName:varIWantToSee =====
TIMESTAMP Info:+Foo, Bar, Value, Whatever
nobleb
  • 119
  • 1
  • 7