diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CommandRunner.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CommandRunner.java index 4539d08..c9f2827 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CommandRunner.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/CommandRunner.java @@ -6,6 +6,10 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.ProjectUtil; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.RollingFileAppender; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Configurator; import picocli.CommandLine; @@ -39,7 +43,14 @@ public class CommandRunner implements Runnable { System.out.println("Safemode active. No files will be changed!"); } - LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler("./last-executions.yml") : null; + String userLocal = getLogDirectory(); + if (userLocal == null) { + log.error("Could not load log4j2 log info"); + System.out.println("Could not load log4j2 log info"); + System.exit(1); + } + + LastExecutionHandler lastExecutionHandler = config.isOnlyNewFiles() ? new LastExecutionHandler(userLocal) : null; FileFilter fileFilter = new FileFilter(config.getExcluded(), config.getIncludePattern(), config.getFilterDate(), lastExecutionHandler); FileProcessor fileProcessor = new CachedFileProcessor(new MkvFileProcessor(config.getMkvToolNix(), fileFilter)); AttributeChangeProcessor attributeChangeProcessor = new AttributeChangeProcessor(config.getPreferredSubtitles().toArray(new String[0]), config.getForcedKeywords(), config.getCommentaryKeywords(), config.getHearingImpaired()); @@ -49,4 +60,19 @@ public class CommandRunner implements Runnable { : new SingleFileAttributeUpdater(config, fileProcessor, attributeChangeProcessor, lastExecutionHandler); kernel.execute(); } + + public String getLogDirectory() { + LoggerContext context = (LoggerContext) LogManager.getContext(false); + Configuration config = context.getConfiguration(); + + for (org.apache.logging.log4j.core.Appender appender : config.getAppenders().values()) { + if (appender instanceof RollingFileAppender rollingFileAppender) { + String fileName = rollingFileAppender.getFileName(); + return new java.io.File(fileName).getParentFile().getParent(); + } + } + + log.warn("No file appender found in configuration"); + return null; + } } diff --git a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandler.java b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandler.java index 5ac8f53..16d4472 100644 --- a/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandler.java +++ b/src/main/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandler.java @@ -11,11 +11,12 @@ import java.util.Properties; @Slf4j public class LastExecutionHandler { + private static final String FILE_NAME = "last-execution.properties"; private final File file; private final Properties lastFileExecution; public LastExecutionHandler(String path) { - file = new File(path); + file = new File(path + "/" + FILE_NAME); lastFileExecution = loadLastFileExecution(file); } @@ -24,7 +25,7 @@ public class LastExecutionHandler { try (FileInputStream in = new FileInputStream(file)) { properties.load(in); } catch (IOException e) { - log.warn("Couldn't find or read {}", file.getPath(), e); + log.info("Couldn't find or read {}", file.getPath(), e); } return properties; } diff --git a/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandlerTest.java b/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandlerTest.java index 70b8702..51e002c 100644 --- a/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandlerTest.java +++ b/src/test/java/at/pcgamingfreaks/mkvaudiosubtitlechanger/impl/LastExecutionHandlerTest.java @@ -11,62 +11,63 @@ import java.time.Instant; import static org.junit.jupiter.api.Assertions.*; class LastExecutionHandlerTest { - private static final String LAST_EXECUTION_YML = "./last-execution.properties"; + private static final String LAST_EXECUTION_PATH = "."; + private static final String LAST_EXECUTION_FILE = "./last-execution.properties"; private static final String TEST_MKV_FILE = "/arst/file.mkv"; @AfterEach void destruct() { - File file = new File(LAST_EXECUTION_YML); + File file = new File(LAST_EXECUTION_FILE); if (file.exists()) file.delete(); } @Test void missingFile() throws IOException { - LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML); + LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH); assertNull(underTest.get(TEST_MKV_FILE)); underTest.update(TEST_MKV_FILE); assertNotNull(underTest.get(TEST_MKV_FILE)); underTest.persist(); - File file = new File(LAST_EXECUTION_YML); + File file = new File(LAST_EXECUTION_FILE); assertTrue(file.exists()); assertTrue(Files.readString(file.toPath()).contains(TEST_MKV_FILE + "=")); } @Test void emptyFile() throws IOException { - File file = new File(LAST_EXECUTION_YML); + File file = new File(LAST_EXECUTION_FILE); file.createNewFile(); missingFile(); // does the checks needed for empty file case } @Test void existingFileNoChanges() throws IOException { - File file = new File(LAST_EXECUTION_YML); + File file = new File(LAST_EXECUTION_FILE); file.createNewFile(); Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now()); String expected = Files.readString(file.toPath()).replace(":", "\\:"); - LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML); + LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH); assertNotNull(underTest.get(TEST_MKV_FILE)); underTest.persist(); - File file1 = new File(LAST_EXECUTION_YML); + File file1 = new File(LAST_EXECUTION_FILE); assertTrue(file1.exists()); assertTrue(Files.readString(file.toPath()).contains(expected), "File contains expected value"); } @Test void existingFileWithChanges() throws IOException { - File file = new File(LAST_EXECUTION_YML); + File file = new File(LAST_EXECUTION_FILE); file.createNewFile(); Files.writeString(file.toPath(), TEST_MKV_FILE + "=" + Instant.now()); String expected = Files.readString(file.toPath()); - LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_YML); + LastExecutionHandler underTest = new LastExecutionHandler(LAST_EXECUTION_PATH); assertNotNull(underTest.get(TEST_MKV_FILE)); underTest.update(TEST_MKV_FILE); assertNotNull(underTest.get(TEST_MKV_FILE)); underTest.persist(); - File file1 = new File(LAST_EXECUTION_YML); + File file1 = new File(LAST_EXECUTION_FILE); assertTrue(file1.exists()); assertNotEquals(expected, Files.readString(file.toPath())); }