FunctionLogger: includescript to log function calls and its parameters for debugging

Debugging Dopus scripts can be hard and basically is logging values. This include script aims to help in this process by replacing the call to the function with a call to LogFunction but maintaining the parameters so the impact on your code is minimal.

Your old code

function MyFunction(a, b) 
{
    DOpus.Output("Myfunction(" + a + ", " + b + ")");
    return a + b; 
}
var result = MyFunction(1, 2);

becomes

function MyFunction(a, b) { return a + b; }
var result = LogFunction(MyFunction, 1, 2); 

and outputs
"Calling MyFunction(a = 1, b = 2)"

inc_FunctionLogger.opusscriptinstall (v 1.1, 1.4 KB)

inc_FunctionLogger.opusscriptinstall (v1.0, 996 Bytes)

inc_FunctionLogger.js
// FunctionLogger
// (c) 2026 Felix.Froemel

// This is an include file script for Directory Opus.
// See https://www.gpsoft.com.au/endpoints/redirect.php?page=scripts for development information.

// Called by Directory Opus to initialize the include file script
function OnInitIncludeFile(initData)
{
	initData.name = "FunctionLogger";
	initData.version = "1.1";
	initData.copyright = "(c) 2026 Felix.Froemel";
	initData.url = "https://resource.dopus.com/t/functionlogger-includescript-to-log-function-calls-and-its-parameters-for-debugging/58532";
	initData.desc = "";
	initData.min_version = "13.0";
	initData.shared = true;
}

//Log a function and its param values
function LogFunction(fn) 
{
	var params = [];
	var paramNamesAndValues = [];
  	for (var i = 1; i < arguments.length; i++)
	{
		params.push(arguments[i]);
		paramNamesAndValues.push(String(arguments[i]));
	}

	var funcName = GetFunctionName(fn);
	var funcParamNames = GetFunctionParameterNames(fn);
	var paramNamesAndValues = MergeParamNamesAndValues(funcParamNames, paramNamesAndValues);
	  
	DOpus.Output("Calling " + funcName + "(" + paramNamesAndValues + ")");
  
  	return fn.apply(null, params);
}

//Get the name of a function
function GetFunctionName(fn)
{
	var funcStr = fn.toString();
	var name = funcStr.substr("function ".length);
	name = name.substr(0, name.indexOf("("));
	return name;
}

//Get the names of the parameters of a function
function GetFunctionParameterNames(fn)
{
	var funcStr = fn.toString();
    var matches = funcStr.match(/function\s+\w+\s*\(([^)]*)\)/);
    
    if (!matches)
        return [];
    
    var paramsString = matches[1];
    
    if (paramsString === '')
        return [];
    
	var params = paramsString.split(',');
	for(var i = 0; i < params.length; i++)
		params[i] = params[i].replace(/^\s+|\s+$/g, ""); //trim
    return params;
}

//combine param names and values to paramName = paramValue
function MergeParamNamesAndValues(names, values)
{
	if(names.length != values.length)
	{
		DOpus.Output("Function Logger: param names do not match values", true);
		return "";
	}

	var result = "";
	for(var i = 0; i < names.length; i++)
	{
		var seperator = i < names.length - 1 ? ", " : "";
		result += names[i] + " = " + values[i] + seperator;
	}
	return result;
}

Version 1.1 now logs also the names of the parameters. Example output

Calling MyFunction(a = 1, b = 2)