简体   繁体   English

从Appcelerator Titanium获取堆栈跟踪或当前文件?

[英]Getting the stack trace or current file from Appcelerator Titanium?

I want to make a logging mechanism that also includes the file that the log statement was written from. 我想建立一个日志机制,其中还包括从中写入log语句的文件。

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. 然后,我希望log功能能够在log中包含文件名"foo.js"

Is this possible? 这可能吗? I haven't been able to find anything about it anywhere. 我到处都找不到任何东西。

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. 这样,您可以使用myError.stack访问堆栈跟踪,这将打印出导致执行点的文件列表。 Example: 例:

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

If you have more questions, let me know! 如果您还有其他问题,请告诉我!

As an alternative, you could throw the error and handle it in the uncaughtException event. 或者,您可以抛出错误并在uncaughtException事件中处理它。 Note that this should only be done for development purposes. 请注意,仅应出于开发目的进行此操作。

除了触发新错误外,另一种可能的解决方案是创建并维护自己的全局数组,该数组包含已打开的所有控制器的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM