当方法生成 stderr 时,如何在 STDERR 前面添加信息?
当一个方法生成对 STDERR 的写入时,有什么方法可以捕获? 我试图在每次 STDERR 写入时预先添加一些信息。
假设我们的方法是:
parser.thatGeneratesSTDERR();
它会生成类似
line 37:29 no溪流替代品在字符'a'
我可以用一些捕获要写入的stderr并预先挂起一些内容的东西来包装它吗 信息,所以它看起来像这样:
File: mybadfile.txt -- line 37:29 no溪流替代品在字符'a'
我知道我可能会挖掘执行STDERR的实际代码写,但我真的不想这样做——我宁愿在这个方法周围包装一些代码。
我应该注意的是,这个方法并不总是生成 STDERR ——我试图捕获它们的原因是它不应该生成任何 STDERR ——但是因为这是在相当多的文件(超过 40 个)上调用的,所以我永远不知道哪个文件正在生成错误消息——需要很长时间才能找到。
更新 所以是的,我们可以更改我们的 STDERR - 我只是忘记了我需要在新的 STDERR 中覆盖我的 println ...这是相关代码:
public static String errfile = "";
static {
final PrintStream currentErr = System.err;
PrintStream newErr = new PrintStream(currentErr)
{
public void println(String string) {
print("File: " + errfile + " --");
print(string);
super.println();
}
};
System.setErr(newErr);
}
然后对于我正在测试的每个文件,我所要做的就是更改 errfile文件名和我的输出是预期的......
非常感谢!
Is there some way I can catch when a method generates a write to STDERR?
I'm trying to pre-pend some information with each STDERR write.
let's say our method is:
parser.thatGeneratesSTDERR();
it'll generate something like
line 37:29 no viable alternative at character 'a'
can I just wrap it with something that catches the stderr to be written and pre-pend some
info so it'll look like this instead:
File: mybadfile.txt -- line 37:29 no viable alternative at character 'a'
I know I could probably dig through the actual code that does the STDERR writing but I really don't want to do that -- I'd rather just wrap some code around this method instead.
I should note that this method doesn't always generate STDERR -- the reason I'm trying to catch them is that it SHOULDN'T be generating any STDERR -- but since this is called on quite a few files (over 40) I never know which file is generating the error message -- it takes forever to find.
UPDATE
so yeh, we can change our STDERR -- I was just forgetting that I need to override my println in my new STDERR... here's the relevant code:
public static String errfile = "";
static {
final PrintStream currentErr = System.err;
PrintStream newErr = new PrintStream(currentErr)
{
public void println(String string) {
print("File: " + errfile + " --");
print(string);
super.println();
}
};
System.setErr(newErr);
}
then for each file that I'm testing all I have to do is change errfile to the filename and my output comes out expected...
thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于 System.setErr 的工作原理,我不确定这是否有效。
我将创建一个新类来扩展打印流并覆盖在前面添加注释的方法。 然后将错误流设置为新的打印流。 不要从这个新类中调用 System.err.printxxx。
ex...
我查看了sun的PrintStream impl,它看起来像所有printxxx委托write(String),它是私有的而不是受保护的。 我想你可以复制/粘贴代码。
编辑::
更好的解决方案是使用 Logging 而不是 System.err。 这允许您关闭和打开语句,并允许您创建自定义错误处理程序。 您的特定处理程序可以在打印到 System.err 之前将您希望的语句添加到字符串中。 这还有一个优点,即只有使用特定记录器的语句才会有前置文本,而对 System.err 的其他调用则不会。
I'm not sure if this will work, given how System.setErr works.
I would create a new class that extends print stream and overwrites the methods to prepend your comment. Then set the error stream to your new printstream. Do not call System.err.printxxx from this new class.
ex...
I looked into sun's PrintStream impl, and it looks like all printxxx delegate to write(String) which is private instead of protected. I guess you can copy/paste the code.
EDIT::
A much better solution is to use Logging instead of System.err. This allows you to turn off and on your statements and allows you to create a custom error handler. Your specific handler could prepend the statement you wish to the string before printing to System.err. This also has the advantage that only statements that use your specific logger would have the prepended text and other calls to System.err wouldn't.
听起来您可能想看看面向方面的编程(AOP)。 使用 AOP,您可以创建您正在调用的解析器的代理。 在调用 thatGeneratesSTDERR() 方法之前,您可以让它调用您所调用的方法。 从您的呼叫中您可以输出您想要的信息。
请注意,可能需要阅读一些内容才能理解 AOP 的工作原理。
AspectJ - http://www.eclipse.org/aspectj/
AOP 联盟 - http://sourceforge.net/projects/aopalliance
Sounds like you probably want to look at Aspect Oriented Programming (AOP). Using AOP, you can create a proxy to the parser you are calling. Before the method call thatGeneratesSTDERR() you can have it invoke you call. From your call you can output the information you want.
Note that it might take a bit of reading to understand how AOP works.
AspectJ - http://www.eclipse.org/aspectj/
AOP Alliance - http://sourceforge.net/projects/aopalliance
您可以使用 System.setErr 将 stderr 重定向到您自己的 PrintStream。
You can use System.setErr to redirect stderr to your own PrintStream.