java 如何在java中实现宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5367769/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:46:24  来源:igfitidea点击:

how to implement macros in java

javamacros

提问by Sophie

I need to be able to print in production to logger, but while developing i would like to print to the console. So in c++ i would simply use a macro for it, and that would be a change of 1 line, how can i do that in java? Thanks

我需要能够在生产中打印到记录器,但在开发时我想打印到控制台。所以在 C++ 中,我会简单地为它使用一个宏,这将是 1 行的变化,我怎么能在 Java 中做到这一点?谢谢

回答by GaryF

Macros aren't the right answer, using a good logging framework is. Look at SLF4J(or the older log4j). If you route all your logging calls through one of these facades, you'll be able to send your logged messages to the console, or a file, or any other appender that you like with a tiny bit of config.

宏不是正确的答案,使用好的日志框架才是。看看SLF4J(或旧的log4j)。如果您通过这些门面之一路由所有日志记录调用,您将能够将记录的消息发送到控制台、文件或任何其他您喜欢的附加程序,只需稍加配置。

回答by templatetypedef

Java doesn't have C/C++-like macros, and so there's no easy way to automatically disable all of your logging code. What you can do is something like this:

Java 没有类似 C/C++ 的宏,因此没有简单的方法可以自动禁用所有日志记录代码。你可以做的是这样的:

public class Utility {
    private Utility() {} // Singleton

    public static void log(String toLog) {
        System.out.println(toLog);
    }
    /* ... etc. ... */
}

Now, anywhere in your program that you want to log an event, you can just call

现在,在您想要记录事件的程序中的任何地方,您只需调用

Utility.log("Here's a message!");

In production builds, you can silence this output by just changing the implementation of logto be a no-op.

在生产版本中,您只需将 的实现更改log为无操作即可使此输出静音。

This is not as efficient as the C/C++-ism of just having your logging macro expand to nothingness at compile-time, but a good optimizer/JIT should be able to notice that the argument isn't getting used and optimize away the logic to set up those arguments.

这不如 C/C++ 主义在编译时将日志宏扩展为空的效率,但是一个好的优化器/JIT 应该能够注意到参数没有被使用并优化掉逻辑建立这些论点。

回答by Malcolm

Just define a static method and call it. In that method put a constant which is set to truewhen you're debugging and execute different code depending on whether you are debugging or not.

只需定义一个静态方法并调用它。在该方法中放置一个常量,该常量true在您调试时设置为,并根据您是否正在调试执行不同的代码。

回答by tpdi

In general, write a function that reads a system property to know where to print to.

通常,编写一个函数来读取系统属性以知道打印到哪里。

Or, for this specific case, use log4j that allows you to specify stuff like this in a config file.

或者,对于这种特定情况,使用允许您在配置文件中指定类似内容的 log4j。

回答by Thorbj?rn Ravn Andersen

Java does not directly support a preprocessor and hence does not support macros.

Java 不直接支持预处理器,因此不支持宏。

Logging, however, is a common need so there are several options to choose from. At the moment the best approach is to use slf4j (as the {}-constructs allow for good code), with a suitable backend. For starters the "slf4j-simple" backend in the slf4j distribution is nice.

然而,日志记录是一种常见需求,因此有多种选择可供选择。目前最好的方法是使用 slf4j(因为 {} 结构允许良好的代码)和合适的后端。对于初学者来说,slf4j 发行版中的“slf4j-simple”后端很好。

Note that the slf4j author strongly favors the logback backend. For now, you do not need that.

请注意,slf4j 作者强烈支持 logback 后端。目前,您不需要那个。

回答by user3894844

My answer is a bit late - 4 years! :)

我的回答有点晚了——4年!:)

But just for the record, if you must use macros just run the C++ preprocessor over the Java code before you compile. Of course this assumes you use the C++ macro syntax.

但只是为了记录,如果您必须使用宏,只需在编译之前在 Java 代码上运行 C++ 预处理器。当然,这假设您使用 C++ 宏语法。

If you don't recall what this does, the preprocessor just replaces #define'd items...

如果你不记得这是做什么的,预处理器只是替换 #define'd 的项目......

That said I agree with the above - use a decent framework (although IMHO it is a little silly not to have macros in Java).

也就是说,我同意上述观点 - 使用一个像样的框架(尽管恕我直言,在 Java 中没有宏有点愚蠢)。