From 73256f0ee1deab03bfa95cbf51654ab2176636d6 Mon Sep 17 00:00:00 2001 From: Craig Schardt Date: Sat, 16 Dec 2023 21:22:16 -0600 Subject: [PATCH] moved path information into PathManager --- .../common/configuration/ConfigManager.java | 9 +-- .../common/configuration/PathManager.java | 58 +++++++++++++++++++ .../configuration/SqlConfigProvider.java | 4 +- .../photonvision/common/logging/Logger.java | 9 +-- 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 photon-core/src/main/java/org/photonvision/common/configuration/PathManager.java diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/ConfigManager.java b/photon-core/src/main/java/org/photonvision/common/configuration/ConfigManager.java index 1046c3d6c6..e162f6aa16 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/ConfigManager.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/ConfigManager.java @@ -62,12 +62,13 @@ enum ConfigSaveStrategy { public static ConfigManager getInstance() { if (INSTANCE == null) { + Path rootFolder = PathManager.getInstance().getRootFolder(); switch (m_saveStrat) { case SQL: - INSTANCE = new ConfigManager(getRootFolder(), new SqlConfigProvider(getRootFolder())); + INSTANCE = new ConfigManager(rootFolder, new SqlConfigProvider(rootFolder)); break; case LEGACY: - INSTANCE = new ConfigManager(getRootFolder(), new LegacyConfigProvider(getRootFolder())); + INSTANCE = new ConfigManager(rootFolder, new LegacyConfigProvider(rootFolder)); break; case ATOMIC_ZIP: // not yet done, fall through @@ -78,7 +79,7 @@ public static ConfigManager getInstance() { return INSTANCE; } - private static final Logger logger = new Logger(ConfigManager.class, LogGroup.General); + private static final Logger logger = new Logger(ConfigManager.class, LogGroup.Config); private void translateLegacyIfPresent(Path folderPath) { if (!(m_provider instanceof SqlConfigProvider)) { @@ -167,7 +168,7 @@ public PhotonConfiguration getConfig() { } private static Path getRootFolder() { - return Path.of("photonvision_config"); + return PathManager.getInstance().getRootFolder(); } ConfigManager(Path configDirectory, ConfigProvider provider) { diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/PathManager.java b/photon-core/src/main/java/org/photonvision/common/configuration/PathManager.java new file mode 100644 index 0000000000..46e5173ada --- /dev/null +++ b/photon-core/src/main/java/org/photonvision/common/configuration/PathManager.java @@ -0,0 +1,58 @@ +package org.photonvision.common.configuration; + +import java.io.File; +import java.nio.file.Path; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; +import java.util.Date; + +public class PathManager { + private static PathManager INSTANCE; + + final File configDirectoryFile; + + public static PathManager getInstance() { + if (INSTANCE == null) { + INSTANCE = new PathManager(); + } + return INSTANCE; + } + + private PathManager() { + this.configDirectoryFile = new File(getRootFolder().toUri()); + } + + public Path getRootFolder() { + return Path.of("photonvision_config"); + } + + public Path getLogsDir() { + return Path.of(configDirectoryFile.toString(), "logs"); + } + + public static final String LOG_PREFIX = "photonvision-"; + public static final String LOG_EXT = ".log"; + public static final String LOG_DATE_TIME_FORMAT = "yyyy-M-d_hh-mm-ss"; + + public String taToLogFname(TemporalAccessor date) { + var dateString = DateTimeFormatter.ofPattern(LOG_DATE_TIME_FORMAT).format(date); + return LOG_PREFIX + dateString + LOG_EXT; + } + + public Path getLogPath() { + var logFile = Path.of(this.getLogsDir().toString(), taToLogFname(LocalDateTime.now())).toFile(); + if (!logFile.getParentFile().exists()) logFile.getParentFile().mkdirs(); + return logFile.toPath(); + } + + public Date logFnameToDate(String fname) throws ParseException { + // Strip away known unneeded portions of the log file name + fname = fname.replace(LOG_PREFIX, "").replace(LOG_EXT, ""); + DateFormat format = new SimpleDateFormat(LOG_DATE_TIME_FORMAT); + return format.parse(fname); + } +} diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java b/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java index 3f22e69522..e43fc843ac 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/SqlConfigProvider.java @@ -45,7 +45,7 @@ *

Global has one row per global config file (like hardware settings and network settings) */ public class SqlConfigProvider extends ConfigProvider { - private final Logger logger = new Logger(SqlConfigProvider.class, LogGroup.Config); + private static final Logger logger = new Logger(SqlConfigProvider.class, LogGroup.Config); static class TableKeys { static final String CAM_UNIQUE_NAME = "unique_name"; @@ -199,7 +199,7 @@ public boolean saveToDisk() { @Override public void load() { - logger.debug("Loading config..."); + logger.debug("Loading config... for CRS"); var conn = createConn(); if (conn == null) return; diff --git a/photon-core/src/main/java/org/photonvision/common/logging/Logger.java b/photon-core/src/main/java/org/photonvision/common/logging/Logger.java index b06fea13fa..151e3db6d5 100644 --- a/photon-core/src/main/java/org/photonvision/common/logging/Logger.java +++ b/photon-core/src/main/java/org/photonvision/common/logging/Logger.java @@ -24,7 +24,8 @@ import java.util.*; import java.util.function.Supplier; import org.apache.commons.lang3.tuple.Pair; -import org.photonvision.common.configuration.ConfigManager; +// import org.photonvision.common.configuration.ConfigManager; +import org.photonvision.common.configuration.PathManager; import org.photonvision.common.dataflow.DataChangeService; import org.photonvision.common.dataflow.events.OutgoingUIEvent; import org.photonvision.common.util.TimedTaskManager; @@ -103,8 +104,8 @@ public static String format( static { currentAppenders.add(new ConsoleLogAppender()); currentAppenders.add(uiLogAppender); - addFileAppender(ConfigManager.getInstance().getLogPath()); - cleanLogs(ConfigManager.getInstance().getLogsDir()); + addFileAppender(PathManager.getInstance().getLogPath()); + cleanLogs(PathManager.getInstance().getLogsDir()); } @SuppressWarnings("ResultOfMethodCallIgnored") @@ -134,7 +135,7 @@ public static void cleanLogs(Path folderToClean) { (File arg0) -> { try { logFileStartDateMap.put( - arg0, ConfigManager.getInstance().logFnameToDate(arg0.getName())); + arg0, PathManager.getInstance().logFnameToDate(arg0.getName())); return false; } catch (ParseException e) { return true;