servicelog.java

来自「First of all, the Applet-phone is a SIP 」· Java 代码 · 共 105 行

JAVA
105
字号
/* * ServiceLog.java * * Created on September 17, 2003, 11:45 AM */package gov.nist.security.log;import java.io.*;/** * This class is used to log all the output  (i.e. all the 'System.out.printXXX()')  * of the user's service to a file. Actually, We start the user's service in a * new Process, so we have to get the output of this process. If we don't, the Process * will be blocked until the output is read... * @author  DERUELLE Jean */public class ServiceLog implements Runnable {    /**the log thread*/    private Thread logThread = null;    /**process from which we will get the output */    private Process process=null;    /**the log file name and path where we will store the output*/    private String logPath=null;        /**      * Creates a new instance of ServiceLog     * @param process - process from which we will get the output      * @param logPath - the log file name and path where we will store the output     */    public ServiceLog(Process process, String logPath) {        this.process=process;        this.logPath=logPath;    }    /**     * start the logging thread     */    public void start() {        if (logThread == null) {            logThread = new Thread(this);            logThread.start();        }    }        /**     * @see java.lang.Runnable#run()     */    public void run() {         //Open the stream to write the output of the user's service to a file        FileOutputStream fileOutputStream=null;        try{            fileOutputStream=new FileOutputStream(logPath);                    }        catch(FileNotFoundException fnfe){            fnfe.printStackTrace();        }        int exitValue=-1;                while(exitValue==-1){                        // read the command output                            try{                                //read the user's service output (i.e. all the 'System.out.printXXX()')                //we need to read it like that because the readline method will blocked                //the thread until a complete line is in the buffer.                byte[] inputBuf = new byte[process.getInputStream().available()];                process.getInputStream().read(inputBuf, 0, inputBuf.length);                //read the user's service error output (i.e. all the 'System.err.printXXX()')                //we need to read it like that because the readline method will blocked                //the thread until a complete line is in the buffer.                                byte[] errorBuf = new byte[process.getErrorStream().available()];                process.getErrorStream().read(errorBuf, 0, errorBuf.length);                //Write the user's service output and error output to the log file                //if we succeeded to open the file                if(fileOutputStream!=null){                    fileOutputStream.write(inputBuf, 0, inputBuf.length);                    fileOutputStream.flush();                    fileOutputStream.write(errorBuf, 0, errorBuf.length);                    fileOutputStream.flush();                }                //or else on the standard output                else{                    System.out.write(inputBuf, 0, inputBuf.length);                    System.out.write(errorBuf, 0, errorBuf.length);                }                //We look if the process is terminated                //we stop the thread                try{                                            exitValue=process.exitValue();                                                                }                catch (IllegalThreadStateException itse) {                                    }                            }            catch(IOException ioe){                ioe.printStackTrace();            }                try{                logThread.sleep(0,1);            }            catch(InterruptedException ie){                ie.printStackTrace();            }        }               }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?