__LINE__ 等价于 Java 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1346391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
__LINE__ equivalent in Java?
提问by Steve Schnepp
I'm searching a way to include __LINE__as a compile-time constant in outputted messages.
我正在寻找一种__LINE__将编译时常量包含在输出消息中的方法。
Various solutions seem to exist, but with a big runtime penalty as suggested in __LINE__in JSand __LINE__in C#. They are usually always based upon a runtime object StackFrameas log4j.
似乎存在各种解决方案,但如__LINE__JS和__LINE__C# 中所建议的那样,运行时会受到很大的损失。它们通常总是基于一个运行时对象,StackFrame如 log4j。
Using the log4j possibility of enabling/disabling on a needed basis isn't an option either since usually when an error happens, it's too late to enable the line numbers, and inlined code doesn't seem to have any line numbers any more. . Would it be possible to either :
在需要的基础上使用 log4j 启用/禁用的可能性也不是一种选择,因为通常发生错误时,启用行号为时已晚,并且内联代码似乎不再有任何行号。. 是否有可能:
- Pre-process the java source files before compilation, ideally with something integrated with Eclipse1, so the it can be tested on the developpement platform also.
- Be able to pre-processthe class at loading time. The overhead would then be negligible by being amortized.
- 在编译之前对 java 源文件进行预处理,最好使用与 Eclipse 1集成的东西,这样它也可以在开发平台上进行测试。
- 能够在加载时对类进行预处理。通过摊销,开销将可以忽略不计。
1. Actually I do use an ugly hack: calling a custom preprocessor just after checkout on the build server via ANT tricks
1. 实际上,我确实使用了一个丑陋的 hack:在通过 ANT 技巧在构建服务器上结帐后调用自定义预处理器
采纳答案by Aaron Digulla
If you compile with the debug=lineoption, the line information exists in the class file but the VM can throw that away at runtime. This depends on some options which you can specify when you start the Java program. Usually, the performance penalty for running with debug infos is around 5% plus a bit more memory in the perm gen space. Since this information is so invaluable, I see no reason to strip this information.
如果使用该debug=line选项进行编译,则行信息存在于类文件中,但 VM 可以在运行时将其丢弃。这取决于您在启动 Java 程序时可以指定的一些选项。通常,使用调试信息运行的性能损失约为 5%,外加永久代空间中的更多内存。由于这些信息非常宝贵,我认为没有理由剥离这些信息。
Your comment (Util.java(Inlined Compiled Code)) suggest that you're using aggressive optimizations. If you can, turn those off. That way, you can simply:
您的评论 ( Util.java(Inlined Compiled Code)) 表明您正在使用积极的优化。如果可以,请关闭它们。这样,您可以简单地:
// For Java 1.4, use new Exception().getStackTrace()
int line = Thread.currentThread().getStackTrace()[0].getLineNumber();
when you need it (i.e. inside of catchor if (log.isInfoEnabled())).
当您需要它时(即在catch或 内if (log.isInfoEnabled()))。
As for __LINE__, the Java compiler has no support for this. Your only option is to use some hacks to filter the source. I suggest to define a variable int __LINE__somewhere and set that to some random value (0 being random enough) when you need it. Then write a filter that replaces the number in __LINE__ = \d+with the current line number. This allows to fix the line number in place.
至于__LINE__,Java 编译器不支持这一点。您唯一的选择是使用一些技巧来过滤源。我建议在int __LINE__某处定义一个变量,并在需要时将其设置为某个随机值(0 足够随机)。然后编写一个过滤器,用__LINE__ = \d+当前行号替换输入的数字。这允许将行号固定到位。
回答by ZZ Coder
It's impossible to get the effect of __LINE__in C which happens at compile time.
不可能__LINE__在编译时获得在 C 中发生的效果。
You can get line number by calling a function like this at runtime,
您可以通过在运行时调用这样的函数来获取行号,
public static int lineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
回答by William T. Mallard
I've found this class makes things easy if you're looking for "got to here, got to here" type logging. I have a file Here.java that I add to my projects that contains just the following class definition:
我发现如果您正在寻找“到这里,到这里”类型的日志记录,这个类会让事情变得简单。我有一个 Here.java 文件,我将它添加到我的项目中,其中仅包含以下类定义:
public class Here {
public static String at () {
StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
String where = ste.getClassName() + " " + ste.getMethodName() + " " + ste.getLineNumber() + " ";
return where;
}
}
To use it in its simplest form:
以最简单的形式使用它:
Log.v(TAG, Here.At());
Or, if you want to see other stuff:
或者,如果您想查看其他内容:
Log.v(TAG, Here.At() + String.format("interesting value = %d", my_val));
This would give "com.otherpackagenamestuff.MyClass myMethod 225" in the first case, and the same but with "interesting value = 123" appended in the second case.
这将在第一种情况下给出“com.otherpackagenamestuff.MyClass myMethod 225”,相同但在第二种情况下附加了“interesting value = 123”。
回答by Dominique
That true that out the box, these macros are not available. However, using instrumentation, and debug information you can implements it. check http://www.gallot.be/?p=85
确实,开箱即用,这些宏不可用。但是,您可以使用检测和调试信息来实现它。检查http://www.gallot.be/?p=85
回答by Thorbj?rn Ravn Andersen
I would use a logging framework for this, and let it deal with the messy details. Both java.util.logging and logback can easily show information about the caller similar to what LINEcan provide.
我会为此使用一个日志框架,让它处理混乱的细节。java.util.logging 和 logback 都可以轻松显示有关调用者的信息,类似于LINE可以提供的信息。
The major benefit is that the information is only computed if necessary, so not outputting means no overhead.
主要的好处是信息只在必要时计算,所以不输出意味着没有开销。
回答by djna
Out of the box I don't think Java offers what you need.
开箱即用,我认为 Java 不能提供您所需要的。
However if you were to write your own annotation processor and use the Java 6.0 compiler APIsI think you could solve this problem. I wouldn't care to guess how much work it would be to do so.
但是,如果您要编写自己的注释处理器并使用 Java 6.0编译器 API,我认为您可以解决这个问题。我不在乎猜测这样做需要多少工作。
Note that Eclipse allows you to include your own annotation processing in its build mechanism.
请注意,Eclipse 允许您在其构建机制中包含您自己的注释处理。
回答by Mnementh
I don't know about such a thing in the Java-compiler. But you could write a simple program, that replaces a token like __LINE__with the linenumber in the file and configure your buildprocess to execute this tool before compiling. Some sort of a preprocessor.
我不知道 Java 编译器中有这样的事情。但是您可以编写一个简单的程序,用__LINE__文件中的行号替换标记,并配置您的构建过程以在编译之前执行此工具。某种预处理器。

