Java,如何跟踪调用的函数 [英] Java, how to trace functions called

查看:160
本文介绍了Java,如何跟踪调用的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想追查一下[&在Java中调用的函数的结尾,如下面的代码:

I want to trace the beginning [& ending] of functions called in Java, like the following code:

    public void foo() {
  System.out.println("begin of foo()");
  ...
  System.out.println("e-n-d of foo()");
 }

但保留转储代码 System.out。 println 是一件单调乏味且容易出错的事情,因为一个类中可能有数十个函数。

But maintaining of the dump code System.out.println is something tedious and error-prone, for there may be tens of thounds of function in an class.

任何好主意都可以缓解这个问题工作?我不想在整个文件中使用转储语句。

Any good idea can ease this work? I don't want dump statements all over the file.

执行两者或其中一个开头的&结束痕迹是最好的。


但是,如果不可能的话,调用该函数的记录也很有帮助。我的意思是不关心确切的开始和结束,只是告诉函数已被调用。

Implementation of both or one of the beginning & ending traces is perferd.
But, if impossible, recordings of that the function has been called is also helpful. I mean not care the exact beginnig and ending, just tell that the function has been called.

推荐答案

最简单的方法是你已经选择了。

The easiest approach is the one you've chose.

System.out 调用的简单替代就是使用日志框架。然后你可以根据选择的日志记录级别打开和关闭信息。

An easy replacement for the System.out calls would be using a logging framework. Then you could switch the information on and off according to a selected "logging level"

更复杂的解决方案将使用面向方面的编程技术(例如,由AspectJ提供),但是这会让你陷入陡峭的学习曲线。

More sophisticated solutions would use aspect oriented programming techniques (provide by AspectJ, for instance), but this puts you on a steep learning curve.

也许基于工具的方法可以满足您的需求:所谓的分析器可以检测您的代码并准确报告在运行期间调用了哪种方法。

Maybe a tool-based a approach fits your needs: so called "profilers" can "instrument" your code and report exactly which method was called during a run.

这篇关于Java,如何跟踪调用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆