mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-24 16:58:51 -05:00
* simplistic output redirection for logging purposes
This commit is contained in:
parent
0c9c9a2633
commit
c54c0895c3
@ -15,6 +15,7 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
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.filebot.web.CachedResource;
|
||||||
import net.sourceforge.tuned.ByteBufferInputStream;
|
import net.sourceforge.tuned.ByteBufferInputStream;
|
||||||
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
|
import net.sourceforge.tuned.PreferencesMap.PreferencesEntry;
|
||||||
|
import net.sourceforge.tuned.TeePrintStream;
|
||||||
|
|
||||||
import org.kohsuke.args4j.CmdLineException;
|
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
|
||||||
@ -77,6 +78,13 @@ public class Main {
|
|||||||
final ArgumentProcessor cli = new ArgumentProcessor();
|
final ArgumentProcessor cli = new ArgumentProcessor();
|
||||||
final ArgumentBean args = cli.parse(arguments);
|
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())) {
|
if (args.printHelp() || args.printVersion() || (!args.runCLI() && isHeadless())) {
|
||||||
System.out.format("%s / %s%n%n", getApplicationIdentifier(), getJavaRuntimeIdentifier());
|
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);
|
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
|
// illegal arguments => just print CLI error message and stop
|
||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
|
@ -6,6 +6,7 @@ import static java.util.Collections.*;
|
|||||||
import static net.sourceforge.tuned.FileUtilities.*;
|
import static net.sourceforge.tuned.FileUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -84,6 +85,9 @@ public class ArgumentBean {
|
|||||||
@Option(name = "--log", usage = "Log level", metaVar = "[all, config, info, warning]")
|
@Option(name = "--log", usage = "Log level", metaVar = "[all, config, info, warning]")
|
||||||
public String log = "all";
|
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")
|
@Option(name = "-r", usage = "Resolve folders recursively")
|
||||||
public boolean recursive = false;
|
public boolean recursive = false;
|
||||||
|
|
||||||
@ -180,4 +184,13 @@ public class ArgumentBean {
|
|||||||
return Level.parse(log.toUpperCase());
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
56
source/net/sourceforge/tuned/TeePrintStream.java
Normal file
56
source/net/sourceforge/tuned/TeePrintStream.java
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user