Getting the stack trace or current file from Appcelerator Titanium?

128 Views Asked by At

I want to make a logging mechanism that also includes the file that the log statement was written from.

So for instance, if I have a file:

//foo.js
log("stuff");

Then I want the log function to be able to include the file name "foo.js" in the logs.

Is this possible? I haven't been able to find anything about it anywhere.

3

There are 3 best solutions below

0
Leon Braun On BEST ANSWER

You can trigger a new error and catch it internally. That way, you can access the stack trace using myError.stack, which will print a list of files that have led to your execution point. Example:

try {
    throw new Error('Trace!');
} catch (err) {
    console.log(err.trace);
}

If you have more questions, let me know!

0
Torbinho On

As an alternative, you could throw the error and handle it in the uncaughtException event. Note that this should only be done for development purposes.

0
mukesh.kumar On

Apart from triggering a new error, one other possible solution is to create and maintain your own global array that contains the name of all the controller you have opened.