Add debug logging for configuration loading

This commit is contained in:
Phoenix616 2020-04-17 15:10:27 +01:00
parent 2426aef969
commit 781e017ae9
3 changed files with 33 additions and 4 deletions

View File

@ -11,8 +11,11 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.Acrobot.Breeze.Configuration.Annotations.Parser;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -44,8 +47,24 @@ public class Configuration {
* @param clazz Class to modify
*/
public static void pairFileAndClass(File file, Class<?> clazz) {
pairFileAndClass(file, clazz, Bukkit.getLogger());
}
/**
* Loads a YAML-formatted file into a class and modifies the file if some of class's fields are missing
*
* @param file File to load
* @param clazz Class to modify
* @param logger The logger to use to log some information about the pairing
*/
public static void pairFileAndClass(File file, Class<?> clazz, Logger logger) {
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
boolean debug = config.getBoolean("DEBUG", false);
if (debug) {
logger.log(Level.INFO, "Loading configuration " + file.getName());
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
@ -55,6 +74,9 @@ public class Configuration {
for (Field field : clazz.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || !Modifier.isPublic(field.getModifiers())) {
if (debug) {
logger.log(Level.WARNING, "Field " + field.getName() + " is private, transient or not static!");
}
continue;
}
@ -73,15 +95,18 @@ public class Configuration {
writer.write(FieldParser.parse(field));
writer.newLine();
}
if (debug) {
logger.log(Level.INFO, field.getName() + ": " + Configuration.getParser(field).parseToYAML(field.get(null)));
}
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, "Error while loading field " + field.getName() + " in configuration " + file.getName(), e);
}
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
logger.log(Level.SEVERE, "Error while loading configuration " + file.getName(), e);
}
}

View File

@ -153,8 +153,8 @@ public class ChestShop extends JavaPlugin {
}
public void loadConfig() {
Configuration.pairFileAndClass(loadFile("config.yml"), Properties.class);
Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class);
Configuration.pairFileAndClass(loadFile("config.yml"), Properties.class, getBukkitLogger());
Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class, getBukkitLogger());
NameManager.load();

View File

@ -77,6 +77,10 @@ public class Properties {
});
}
@ConfigurationComment("Should the plugin log some messages that are useful for debugging?")
public static boolean DEBUG = false;
@PrecededBySpace
@ConfigurationComment("Do you want to turn off the automatic updates of ChestShop?")
public static boolean TURN_OFF_UPDATES = false;