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)

This commit is contained in:
Reinhard Pointner 2017-01-28 19:07:12 +08:00
parent 3fee8bed74
commit b44df59d55
3 changed files with 31 additions and 22 deletions

View File

@ -9,6 +9,7 @@ import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Formatter; import java.util.logging.Formatter;
import java.util.logging.Handler; import java.util.logging.Handler;
@ -48,7 +49,21 @@ public final class Logging {
return handler; 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)); StreamHandler handler = new StreamHandler(newOutputStream(channel), new ConsoleFormatter(anonymizePattern.get(), false));
handler.setEncoding("UTF-8"); handler.setEncoding("UTF-8");
handler.setLevel(level); handler.setLevel(level);

View File

@ -14,8 +14,6 @@ import java.awt.Dialog.ModalityType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.security.CodeSource; import java.security.CodeSource;
import java.security.Permission; import java.security.Permission;
import java.security.PermissionCollection; 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) { 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) { if (args.logLock) {
try { log.config("Locking " + log);
log.config("Locking " + logFile);
logChannel.lock();
} catch (Exception e) {
throw new IOException("Failed to acquire lock: " + logFile, e);
}
} }
Handler logFileHandler = createLogFileHandler(args.getLogFile(), args.logLock, Level.ALL);
Handler logFileHandler = createLogFileHandler(logChannel, Level.ALL);
log.addHandler(logFileHandler); log.addHandler(logFileHandler);
debug.addHandler(logFileHandler); debug.addHandler(logFileHandler);
} }

View File

@ -26,6 +26,7 @@ import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.ParserProperties; import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler; import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler;
import net.filebot.ApplicationFolder;
import net.filebot.Language; import net.filebot.Language;
import net.filebot.StandardRenameAction; import net.filebot.StandardRenameAction;
import net.filebot.WebServices; import net.filebot.WebServices;
@ -263,6 +264,17 @@ public class ArgumentBean {
return optional(lang).map(Language::findLanguage).orElseThrow(error("Illegal language code", lang)); 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() { public boolean isStrict() {
return !nonStrict; return !nonStrict;
} }