/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Alexander Benkwitz, Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.io;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.log4j.Logger;
/**
* Helper class to redirect stdout and stderr to a log file and/or a receiver
* component. The redirection also keeps the original streams for
* System.out and System.err but adds an
* additional stream.
*
* Usage Note:
* Redirection is initiated by calling the start method.
* Subsequent calls to start are allowed to change the file or
* logging component. The stop method cancels redirection.
*
* @author Alexander Benkwitz, Markus Kraetzig
*/
public final class RedirectOutput extends PrintStream {
private static final Logger log = Logger.getLogger(RedirectOutput.class);
private static RedirectReceiver redirectReceiver = null;
private static PrintStream logFile = null;
// Keep references of original streams.
private static PrintStream systemOut = System.out;
private static PrintStream systemErr = System.err;
/**
* SaveOutput constructor comment.
*
* @param ps
* java.io.PrintStream
*/
private RedirectOutput(PrintStream ps) {
super(ps);
}
/**
* Starts redirecting stdout and stderr to a log file.
*
* @param fileName
* the name of the log file
* @throws IllegalArgumentException
* if fileName == null
*/
public static void start(String fileName) {
if (fileName == null)
throw new IllegalArgumentException("File argument was null.");
// Keep standard outputs but add file redirection.
System.setOut(new RedirectOutput(systemOut));
System.setErr(new RedirectOutput(systemErr));
try {
// Create/Open logfile.
logFile = new PrintStream(new FileOutputStream(fileName, true));
// Start redirecting the output.
} catch (IOException e) {
log.error("Cannot create log file\n" + e.getMessage(), e);
}
}
/**
* Starts redirecting stdout and stderr to a logging component.
*
* @param receiver
* the name of the receiving component
* @throws IllegalArgumentException
* if receiver == null
*/
public static void start(RedirectReceiver receiver) {
if (receiver == null)
throw new IllegalArgumentException("Argument was null.");
// Keep standard outputs but add file redirection.
System.setOut(new RedirectOutput(systemOut));
System.setErr(new RedirectOutput(systemErr));
redirectReceiver = receiver;
}
/**
* Stops redirecting the output and restored previous settings.
*/
public static void stop() {
redirectReceiver = null;
logFile = null;
System.setOut(systemOut);
System.setErr(systemErr);
}
/**
* PrintStream overwrite.
*
* @param buf
* byte[]
* @param off
* int
* @param len
* int
*/
public void write(byte buf[], int off, int len) {
try {
// logFile.write(buf, off, len);
if (redirectReceiver != null)
redirectReceiver.append(new String(buf, off, len));
if (logFile != null)
logFile.write(buf, off, len);
} catch (Throwable e) {
log.error("Error redirecting", e);
setError();
}
super.write(buf, off, len);
}
/**
* PrintStream overwrite.
*
* @param b
* int
*/
public void write(int b) {
try {
// logFile.write(b);
if (redirectReceiver != null)
redirectReceiver.append(b + "");
if (logFile != null)
logFile.write(b);
} catch (Throwable e) {
log.error("Error redirecting", e);
setError();
}
super.write(b);
}
}
Ads help maintain this website.