简体   繁体   English

读取系统

[英]Reading System.Out

So, what I want to do is basically filter everything that is passed through System.out and System.err , is it possible to apply a filter of some sort or to create my own OutputStream to divert System.out with, and then process it normally? 因此,我要做的基本上是过滤通过System.outSystem.err传递的所有内容,是否可以应用某种过滤器或创建我自己的OutputStream来转移System.out ,然后对其进行处理。一般?

Edit for clarity: To clarify, I need to read what goes out of System.out from other programs and manipulate it how I see fit, so a logger is not really an option as I do not have control over what the other program will use. 为清晰起见,进行编辑:为了明确起见,我需要从其他程序中读取System.out中的内容,并按照自己认为合适的方式进行操作,因此记录器并不是真正的选择,因为我无法控制其他程序将使用的内容。

Edit for more clarity: I am creating a plugin for a larger program that needs to read everything written to System.out from other plugins. 编辑以便更清晰:我正在为一个较大的程序创建一个插件,该程序需要从其他插件读取写入System.out的所有内容。 Because it is a plugin-based system the process my plugin is running on will always be the same one other plugins are running on. 因为它是一个基于插件的系统,所以我的插件运行的过程将始终与其他插件运行的过程相同。

Something I am not clear: you have mentioned that you want to "read what goes out of System.out from other programs ". 我不清楚:您已经提到要“ 从其他程序中读取System.out中的内容”。 Are your application creating the process and want to monitor its standard out/err, or you want to monitor the standard out/err of your process itself? 您的应用程序是在创建流程并想要监视其标准输出/错误,还是要监视流程本身的标准输出/错误?

For former case, after you created the process, you can get its output , input and error stream. 对于前一种情况,在创建流程之后,可以获得其输出输入错误流。

For latter case, you can replace the standard out of your Java process by System.setOut(yourOwnOutputStream); 对于后一种情况,可以用System.setOut(yourOwnOutputStream);替换Java进程中的标准System.setOut(yourOwnOutputStream);

However, if you are trying to deal with streams of totally irrelevant process, I believe there is no way doing so without having the caller pipe the stdout/stderr to your process (unless through some platform specific methods) 但是,如果您要处理完全不相关的流,我相信没有调用者将stdout / stderr传递给您的进程是不可能的(除非通过某些平台特定的方法)


Update: 更新:

In order to "intercept" the standard out, it is nothing different from all those classic "filter outputstreams". 为了“拦截”标准,它与所有经典的“过滤器输出流”没有什么不同。 What you can do is something like this: 您可以执行以下操作:

class AnalyzingOutputStream extends FilterOutputStream {

    public AnalyzingOutputStream (OutputStream out) {
        super(out);
    }

    @Override
    public void write(int b) {
          // do whatever analysis you want
          super.write(b);  // delegate to super class' write, which will 
                           // delegate to the filtered outputstream
    }
    // other overrides
}

In order to use it, your main logic should do something like: 为了使用它,您的主要逻辑应做以下事情:

AnalyzingOutputStream analyzingOutputStream = new AnalyzingOutputStream(System.out);
System.setOut(analyzingOutputStream );

// then you can call your methods of AnalyzingOutputStream to do whatever you want

You should use a logger but if you don't want to you van create your own PrintStream which holds the System.out. 您应该使用记录器,但是如果不想,可以创建自己的PrintStream来保存System.out。 Then call System.setOut(yourPrintStream). 然后调用System.setOut(yourPrintStream)。

To capture all output from another process in Java, the simplest way is to launch the process yourself using Runtime.exec which will give you a Process object which has getOutputStream . 要捕获Java中另一个进程的所有输出,最简单的方法是使用Runtime.exec自己启动该进程,这将为您提供一个具有getOutputStreamProcess对象。 Reading off that stream will give you the stdout for the launched process. 阅读该流将为您提供启动过程的标准输出。

If you're trying to capture the output of an arbitrary process, you've got a bigger task ahead of you - and it's probably not a good idea. 如果您试图捕获任意过程的输出,那么您前面还有一个更大的任务-这可能不是一个好主意。 I have no idea how it works on Windows. 我不知道它如何在Windows上工作。 Under Linux, you'll need to be running under the same user as the process you want to investigate or (more likely) root. 在Linux下,您需要以与要调查的进程相同的用户身份运行(或更有可能是root用户)。 You can look at the file descriptors for any given process under /proc/<process id>/fd/<file descriptor> . 您可以在/proc/<process id>/fd/<file descriptor>下查看任何给定进程的/proc/<process id>/fd/<file descriptor> The file descriptors for stdin, stdout and stderr are 0, 1 and 2 respectively. stdin,stdout和stderr的文件描述符分别为0、1和2。 I would recommend you stop at this point and re-think what you're trying to do. 我建议您在这一点上停下来,然后重新考虑您要做什么。

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

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