1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

* simplistic output redirection for logging purposes

This commit is contained in:
Reinhard Pointner 2013-03-26 15:04:53 +00:00
parent 0c9c9a2633
commit c54c0895c3
3 changed files with 79 additions and 2 deletions

View File

@ -15,6 +15,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.InvocationTargetException;
@ -61,8 +62,8 @@ import net.sourceforge.filebot.ui.transfer.FileTransferable;
import net.sourceforge.filebot.web.CachedResource;
import net.sourceforge.tuned.ByteBufferInputStream;
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
import net.sourceforge.tuned.TeePrintStream;
import org.kohsuke.args4j.CmdLineException;
import org.w3c.dom.NodeList;
@ -77,6 +78,13 @@ public class Main {
final ArgumentProcessor cli = new ArgumentProcessor();
final ArgumentBean args = cli.parse(arguments);
// tee stdout and stderr to log file if set
if (args.logFile != null) {
FileOutputStream log = new FileOutputStream(args.getLogFile(), true);
System.setOut(new TeePrintStream(log, true, "UTF-8", System.out));
System.setErr(new TeePrintStream(log, true, "UTF-8", System.err));
}
if (args.printHelp() || args.printVersion() || (!args.runCLI() && isHeadless())) {
System.out.format("%s / %s%n%n", getApplicationIdentifier(), getJavaRuntimeIdentifier());
@ -203,7 +211,7 @@ public class Main {
Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Failed to check for updates", e);
}
}
} catch (CmdLineException e) {
} catch (Exception e) {
// illegal arguments => just print CLI error message and stop
System.err.println(e.getMessage());
System.exit(-1);

View File

@ -6,6 +6,7 @@ import static java.util.Collections.*;
import static net.sourceforge.tuned.FileUtilities.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@ -84,6 +85,9 @@ public class ArgumentBean {
@Option(name = "--log", usage = "Log level", metaVar = "[all, config, info, warning]")
public String log = "all";
@Option(name = "--log-file", usage = "Log file", metaVar = "path/to/log.txt")
public String logFile = null;
@Option(name = "-r", usage = "Resolve folders recursively")
public boolean recursive = false;
@ -180,4 +184,13 @@ public class ArgumentBean {
return Level.parse(log.toUpperCase());
}
public File getLogFile() throws IOException {
File f = new File(logFile);
if (!f.exists() && !f.getParentFile().mkdirs() && !f.createNewFile()) {
throw new IOException("Failed to create log file: " + f.getAbsolutePath());
}
return f;
}
}

View File

@ -0,0 +1,56 @@
package net.sourceforge.tuned;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class TeePrintStream extends PrintStream {
private final PrintStream cc;
public TeePrintStream(OutputStream out, boolean autoFlush, String encoding, PrintStream cc) throws UnsupportedEncodingException {
super(out, autoFlush, encoding);
this.cc = cc;
}
@Override
public void close() {
super.close();
cc.close();
}
@Override
public void flush() {
super.flush();
cc.flush();
}
@Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
cc.write(buf, off, len);
}
@Override
public void write(int b) {
super.write(b);
cc.write(b);
}
@Override
public void write(byte[] b) throws IOException {
super.write(b);
cc.write(b);
}
}