From 86b7c9d4822933d26779e455c36ce6fd4bcb0c75 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 30 Mar 2013 04:20:56 +0000 Subject: [PATCH] + allow single-instance mode via --log-lock yes --- source/net/sourceforge/filebot/Main.java | 25 +++++++++++++------ .../sourceforge/filebot/cli/ArgumentBean.java | 8 ++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/source/net/sourceforge/filebot/Main.java b/source/net/sourceforge/filebot/Main.java index 401fdd41..565d56eb 100644 --- a/source/net/sourceforge/filebot/Main.java +++ b/source/net/sourceforge/filebot/Main.java @@ -17,10 +17,13 @@ import java.awt.event.WindowEvent; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.RandomAccessFile; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.security.CodeSource; import java.security.Permission; @@ -78,13 +81,6 @@ 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()); @@ -96,6 +92,21 @@ public class Main { System.exit(0); } + // tee stdout and stderr to log file if set + if (args.logFile != null) { + File logFile = args.getLogFile(); + FileChannel logChannel = new FileOutputStream(logFile, true).getChannel(); + + if (args.logLock) { + System.out.println("Locking " + logFile); + logChannel.lock(); + } + + OutputStream out = Channels.newOutputStream(logChannel); + System.setOut(new TeePrintStream(out, true, "UTF-8", System.out)); + System.setErr(new TeePrintStream(out, true, "UTF-8", System.err)); + } + // make sure java.io.tmpdir exists File tmpdir = new File(System.getProperty("java.io.tmpdir")); tmpdir.mkdirs(); diff --git a/source/net/sourceforge/filebot/cli/ArgumentBean.java b/source/net/sourceforge/filebot/cli/ArgumentBean.java index 6cd3633f..cea824ea 100644 --- a/source/net/sourceforge/filebot/cli/ArgumentBean.java +++ b/source/net/sourceforge/filebot/cli/ArgumentBean.java @@ -18,6 +18,7 @@ import net.sourceforge.filebot.MediaTypes; import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; +import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler; public class ArgumentBean { @@ -88,6 +89,9 @@ public class ArgumentBean { @Option(name = "--log-file", usage = "Log file", metaVar = "path/to/log.txt") public String logFile = null; + @Option(name = "--log-lock", usage = "Lock log file", metaVar = "[yes, no]", handler = ExplicitBooleanOptionHandler.class) + public boolean logLock = true; + @Option(name = "-r", usage = "Resolve folders recursively") public boolean recursive = false; @@ -186,9 +190,9 @@ public class ArgumentBean { public File getLogFile() throws IOException { - File f = new File(logFile); + File f = new File(output, logFile).getAbsoluteFile(); if (!f.exists() && !f.getParentFile().mkdirs() && !f.createNewFile()) { - throw new IOException("Failed to create log file: " + f.getAbsolutePath()); + throw new IOException("Failed to create log file: " + f); } return f; }