From c54c0895c3377b22afb7b651eaeaa94c4ec6a980 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 26 Mar 2013 15:04:53 +0000 Subject: [PATCH] * simplistic output redirection for logging purposes --- source/net/sourceforge/filebot/Main.java | 12 +++- .../sourceforge/filebot/cli/ArgumentBean.java | 13 +++++ .../net/sourceforge/tuned/TeePrintStream.java | 56 +++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 source/net/sourceforge/tuned/TeePrintStream.java diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index d74b2a0a..401fdd41 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -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); diff --git a/source/net/sourceforge/filebot/cli/ArgumentBean.java b/source/net/sourceforge/filebot/cli/ArgumentBean.java index 7b754d7d..6cd3633f 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentBean.java +++ b/source/net/sourceforge/filebot/cli/ArgumentBean.java @@ -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; + } + } diff --git a/source/net/sourceforge/tuned/TeePrintStream.java b/source/net/sourceforge/tuned/TeePrintStream.java new file mode 100644 index 00000000..14b3300b --- /dev/null +++ b/source/net/sourceforge/tuned/TeePrintStream.java @@ -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); + } + +}