+ allow single-instance mode via --log-lock yes

This commit is contained in:
Reinhard Pointner 2013-03-30 04:20:56 +00:00
parent 7542da7cf5
commit 86b7c9d482
2 changed files with 24 additions and 9 deletions

View File

@ -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();

View File

@ -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;
}