0

I'm trying to get google apps script to log the current function that is running. I have something similar in python like so:

code_obj = sys._getframe(1).f_code
file = os.path.basename(code_obj.co_filename).split('.')[0]
Def = code_obj.co_name
line = str(sys._getframe(1).f_lineno)

try:
    Class = get_class_from_frame(sys._getframe(1)).__name__
except:
    Class = ''

return "--> " + file + "." + Class + "()." + Def + "()." + line + " --> " + now() + '\n'

Anybody know how to get the name of the current running function?

jason
  • 3,811
  • 18
  • 92
  • 147
  • I never heard of such a thing in google apps script – Cooper Sep 09 '21 at 23:59
  • Do you mean getting that name through that same function's code? What's the usefulness of that. If that's the case, you could use `arguments.callee.name`, or `myFunctionName.name`, which looks a bit silly. – Iamblichus Sep 10 '21 at 07:33
  • @lamblichus. when google apps script executes, I have functions calling different functions. I want to see that each step has been performed and which function is being executed. Now I could hard code everything to the function name, but sometimes i go back and change the function name. It would be helpful, if the logs also just change themselves, as opposed to have to go through everything hardcoded. How would you handle that? – jason Sep 10 '21 at 21:36
  • Wouldn't [arguments.callee.name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee) be a solution for that? It would give you the function name, even if you change that name. – Iamblichus Sep 13 '21 at 09:28
  • Yes. it worked. Still green in javascript. thanks for the help. – jason Sep 13 '21 at 12:03
  • I posted an answer explaining this, I hope it is helpful. – Iamblichus Sep 14 '21 at 11:59

2 Answers2

2

As in plain JavaScript, you can retrieve current function via arguments.callee, and then retrieve Function's property name:

callee is a property of the arguments object. It can be used to refer to the currently executing function inside the function body of that function.

function myFunctionName() {
  const functionName = arguments.callee.name;
  // ...
}

Important note:

Since ES5, arguments.callee is forbidden in strict mode. This can cause an error in cases where the script is using strict mode behind the scenes (see, for example, this):

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

I've noticed this happening, for example, when using default parameters or rest parameters.

Unfortunately, there's not an easy alternative to arguments.callee:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
2

There's a nice way to get all function names and lines called in using:

(new Error()).stack

Please try:

function stack2lines_(stack) {
  var re1 = new RegExp('at (.*) .*\\:(\\d+)\:\\d*\\)$', 'gm');
  var re2 = new RegExp('\\{.*\\}', 'gm');
  var res1 = stack.replace(re1, '{"$1": $2}');
  var res2 = res1.replace(re1, '{"$1": $2}');
  var res3 = res2.match(re2);
  var result = res3.map(function(elt) {
        return JSON.parse(elt)
      });
  return result;
}

The result:

[ '{"f_": 6}', '{"test": 2}' ]

Source:

https://script.google.com/u/0/home/projects/1BbaDeja4ObWMwbVnhCJ29FHsNfQm-SfAPYvpqobh2B96TA5449GnGnHc/edit

enter image description here

Max Makhrov
  • 17,309
  • 5
  • 55
  • 81