From b44df59d55ac5511d4b51da877aa0969b7a869e5 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 28 Jan 2017 19:07:12 +0800 Subject: [PATCH] Use internal logging for --log-file instead of copying stdio/stderr to file (which doesn't work for native code or sub-process console output anyway) --- source/net/filebot/Logging.java | 17 ++++++++++++++++- source/net/filebot/Main.java | 24 +++--------------------- source/net/filebot/cli/ArgumentBean.java | 12 ++++++++++++ 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/source/net/filebot/Logging.java b/source/net/filebot/Logging.java index 24d17230..ec46e1d7 100644 --- a/source/net/filebot/Logging.java +++ b/source/net/filebot/Logging.java @@ -9,6 +9,7 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.channels.FileChannel; +import java.nio.file.StandardOpenOption; import java.util.function.Supplier; import java.util.logging.Formatter; import java.util.logging.Handler; @@ -48,7 +49,21 @@ public final class Logging { return handler; } - public static StreamHandler createLogFileHandler(FileChannel channel, Level level) throws IOException { + public static StreamHandler createLogFileHandler(File file, boolean lock, Level level) throws IOException { + if (!file.exists() && !file.getParentFile().mkdirs() && !file.createNewFile()) { + throw new IOException("Failed to create log file: " + file); + } + + // open file channel and lock + FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND); + if (lock) { + try { + channel.lock(); + } catch (Exception e) { + throw new IOException("Failed to acquire lock: " + file, e); + } + } + StreamHandler handler = new StreamHandler(newOutputStream(channel), new ConsoleFormatter(anonymizePattern.get(), false)); handler.setEncoding("UTF-8"); handler.setLevel(level); diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index c0b32960..ac81d2b4 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -14,8 +14,6 @@ import java.awt.Dialog.ModalityType; import java.io.File; import java.io.IOException; import java.net.URL; -import java.nio.channels.FileChannel; -import java.nio.file.StandardOpenOption; import java.security.CodeSource; import java.security.Permission; import java.security.PermissionCollection; @@ -392,28 +390,12 @@ public class Main { } } - // tee stdout and stderr to log file if set + // tee stdout and stderr to log file if --log-file is set if (args.logFile != null) { - File logFile = new File(args.logFile); - if (!logFile.isAbsolute()) { - logFile = new File(ApplicationFolder.AppData.resolve("logs"), logFile.getPath()).getAbsoluteFile(); // by default resolve relative paths against {applicationFolder}/logs/{logFile} - } - if (!logFile.exists() && !logFile.getParentFile().mkdirs() && !logFile.createNewFile()) { - throw new IOException("Failed to create log file: " + logFile); - } - - // open file channel and lock - FileChannel logChannel = FileChannel.open(logFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.APPEND); if (args.logLock) { - try { - log.config("Locking " + logFile); - logChannel.lock(); - } catch (Exception e) { - throw new IOException("Failed to acquire lock: " + logFile, e); - } + log.config("Locking " + log); } - - Handler logFileHandler = createLogFileHandler(logChannel, Level.ALL); + Handler logFileHandler = createLogFileHandler(args.getLogFile(), args.logLock, Level.ALL); log.addHandler(logFileHandler); debug.addHandler(logFileHandler); } diff --git a/source/net/filebot/cli/ArgumentBean.java b/source/net/filebot/cli/ArgumentBean.java index 03c18384..ef7ba4f0 100644 --- a/source/net/filebot/cli/ArgumentBean.java +++ b/source/net/filebot/cli/ArgumentBean.java @@ -26,6 +26,7 @@ import org.kohsuke.args4j.Option; import org.kohsuke.args4j.ParserProperties; import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler; +import net.filebot.ApplicationFolder; import net.filebot.Language; import net.filebot.StandardRenameAction; import net.filebot.WebServices; @@ -263,6 +264,17 @@ public class ArgumentBean { return optional(lang).map(Language::findLanguage).orElseThrow(error("Illegal language code", lang)); } + public File getLogFile() { + File file = new File(logFile); + + if (file.isAbsolute()) { + return file; + } + + // by default resolve relative paths against {applicationFolder}/logs/{logFile} + return ApplicationFolder.AppData.resolve("logs/" + logFile); + } + public boolean isStrict() { return !nonStrict; }