Added some logging to configuration saving/loading

This commit is contained in:
Jindra Petřík
2025-07-27 18:59:57 +02:00
parent fa0f9a1fa8
commit 0fa8701cd9
2 changed files with 19 additions and 9 deletions

View File

@@ -1457,15 +1457,18 @@ public final class Configuration {
* Save configuration to file
*/
public static void saveConfig() {
Logger.getLogger(Configuration.class.getName()).fine("Saving configuration...");
try {
storage.saveToFile(getConfigFile());
Logger.getLogger(Configuration.class.getName()).fine("TOML configuration saved.");
} catch (IOException ex) {
// ignore
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Cannot save TOML configuration", ex);
}
try {
legacyStorage.saveToFile(getLegacyConfigFile());
Logger.getLogger(Configuration.class.getName()).fine("Legacy configuration saved.");
} catch (IOException ex) {
// ignore
Logger.getLogger(Configuration.class.getName()).log(Level.WARNING, "Cannot save legacy configuration", ex);
}
}

View File

@@ -54,6 +54,7 @@ import org.tomlj.TomlTable;
*/
public class TomlConfigurationStorage implements ConfigurationStorage {
@Override
public String getConfigName() {
return "config.toml";
}
@@ -65,14 +66,17 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
try {
tomlResult = Toml.parse(Paths.get(file));
} catch (IOException ex) {
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.SEVERE, "Error reading TOML file", ex);
return result;
}
if (!tomlResult.errors().isEmpty()) {
System.err.println("Error parsing configuration file:");
StringBuilder sb = new StringBuilder();
sb.append("Error parsing configuration file:\r\n");
for (TomlParseError error : tomlResult.errors()) {
System.err.println("- " + error);
sb.append("- ").append(error).append("\r\n");
}
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.SEVERE, sb.toString());
}
TomlTable configurationTable = tomlResult.getTable("configuration");
@@ -200,11 +204,12 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
result.put(name, value);
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Cannot load TOML configuration", ex);
} catch (Exception ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Exception during loading TOML configuration", ex);
}
}
return result;
}
private static List<String> wordWrap(String text, int maxLineLength) {
@@ -260,7 +265,7 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
}
} catch (IOException ex) {
//ignore
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.WARNING, "Cannot load showComments/modifiedOnly flags from previous TOML file", ex);
}
}
if (showComments == null) {
@@ -558,11 +563,13 @@ public class TomlConfigurationStorage implements ConfigurationStorage {
pw.println();
}
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, "Cannot get configuration field to save", ex);
}
}
} catch (IOException ex) {
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.SEVERE, "Cannot write TOML configuration", ex);
} catch (Exception ex) {
Logger.getLogger(TomlConfigurationStorage.class.getName()).log(Level.SEVERE, "Exception during saving configuration", ex);
}
}
}