Fix Logger - ConfigManager circular reference (#1054)

This commit is contained in:
Craig Schardt
2023-12-18 11:59:14 -06:00
committed by GitHub
parent 36ba8b5263
commit 796b8e73d5
4 changed files with 86 additions and 10 deletions

View File

@@ -62,12 +62,13 @@ public class ConfigManager {
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 class ConfigManager {
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 class ConfigManager {
}
private static Path getRootFolder() {
return Path.of("photonvision_config");
return PathManager.getInstance().getRootFolder();
}
ConfigManager(Path configDirectory, ConfigProvider provider) {

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@@ -45,7 +45,7 @@ import org.photonvision.vision.pipeline.DriverModePipelineSettings;
* <p>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";

View File

@@ -24,7 +24,8 @@ import java.text.SimpleDateFormat;
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 class Logger {
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")
@@ -133,8 +134,7 @@ public class Logger {
logFileList.removeIf(
(File arg0) -> {
try {
logFileStartDateMap.put(
arg0, ConfigManager.getInstance().logFnameToDate(arg0.getName()));
logFileStartDateMap.put(arg0, PathManager.getInstance().logFnameToDate(arg0.getName()));
return false;
} catch (ParseException e) {
return true;