From f7f9863e97c986bbd3ae001068aa2dbd83cca4ae Mon Sep 17 00:00:00 2001 From: lisrte Date: Fri, 24 May 2024 17:24:26 +0200 Subject: [PATCH] Add specific logs Signed-off-by: lisrte --- .../dynawaltz/DynaWaltzParameters.java | 34 +++++++++++++++++++ .../com/powsybl/dynawaltz/xml/JobsXml.java | 14 ++++++-- .../dynawaltz/DynaWaltzParametersTest.java | 16 ++++++--- .../powsybl/dynawaltz/xml/JobsXmlTest.java | 12 +++++++ .../test/resources/DynaWaltzParameters.json | 1 + .../test/resources/jobsWithSpecificLogs.xml | 27 +++++++++++++++ 6 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 dynawaltz/src/test/resources/jobsWithSpecificLogs.xml diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java index 240a2124e..f5b6f02c4 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzParameters.java @@ -80,6 +80,24 @@ public enum LogLevel { ERROR } + public enum SpecificLog { + NETWORK("network"), + MODELER("modeler"), + PARAMETERS("param"), + VARIABLES("variables"), + EQUATIONS("equations"); + + private final String fileName; + + SpecificLog(String fileName) { + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } + } + private Map modelsParameters = new LinkedHashMap<>(); private ParametersSet networkParameters; private ParametersSet solverParameters; @@ -91,6 +109,7 @@ public enum LogLevel { private double precision = DEFAULT_PRECISION; private ExportMode timelineExportMode = DEFAULT_TIMELINE_EXPORT_MODE; private LogLevel logLevelFilter = DEFAULT_LOG_LEVEL_FILTER; + private EnumSet specificLogs = EnumSet.noneOf(SpecificLog.class); /** * Loads parameters from the default platform configuration. @@ -132,6 +151,7 @@ public static DynaWaltzParameters load(PlatformConfig platformConfig, FileSystem c.getOptionalDoubleProperty("precision").ifPresent(parameters::setPrecision); c.getOptionalEnumProperty("timeline.exportMode", ExportMode.class).ifPresent(parameters::setTimelineExportMode); c.getOptionalEnumProperty("log.levelFilter", LogLevel.class).ifPresent(parameters::setLogLevelFilter); + c.getOptionalEnumSetProperty("log.specificLogs", SpecificLog.class).ifPresent(parameters::setSpecificLogs); }); return parameters; } @@ -263,4 +283,18 @@ public DynaWaltzParameters setLogLevelFilter(LogLevel logLevelFilter) { this.logLevelFilter = logLevelFilter; return this; } + + public Set getSpecificLogs() { + return specificLogs; + } + + public DynaWaltzParameters setSpecificLogs(Set specificLogs) { + this.specificLogs = EnumSet.copyOf(specificLogs); + return this; + } + + public DynaWaltzParameters addSpecificLog(SpecificLog specificLog) { + specificLogs.add(specificLog); + return this; + } } diff --git a/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java b/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java index 3ad2f34aa..717ee0403 100644 --- a/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java +++ b/dynawaltz/src/main/java/com/powsybl/dynawaltz/xml/JobsXml.java @@ -96,7 +96,7 @@ private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context writer.writeAttribute("global", Boolean.toString(false)); writer.writeEmptyElement(DYN_URI, "timeline"); - writer.writeAttribute("exportMode", context.getDynaWaltzParameters().getTimelineExportMode().name()); + writer.writeAttribute("exportMode", parameters.getTimelineExportMode().toString()); writer.writeEmptyElement(DYN_URI, "finalState"); writer.writeAttribute("exportIIDMFile", Boolean.toString(parameters.isWriteFinalState())); @@ -105,7 +105,7 @@ private static void writeOutput(XMLStreamWriter writer, DynaWaltzContext context if (context.withCurves()) { writer.writeEmptyElement(DYN_URI, "curves"); writer.writeAttribute("inputFile", DynaWaltzConstants.CRV_FILENAME); - writer.writeAttribute("exportMode", DynaWaltzParameters.ExportMode.CSV.name()); + writer.writeAttribute("exportMode", DynaWaltzParameters.ExportMode.CSV.toString()); } writer.writeStartElement(DYN_URI, "logs"); @@ -120,5 +120,15 @@ private static void writeAppender(XMLStreamWriter writer, DynaWaltzParameters pa writer.writeAttribute("tag", ""); writer.writeAttribute("file", "dynawaltz.log"); writer.writeAttribute("lvlFilter", parameters.getLogLevelFilter().toString()); + for (DynaWaltzParameters.SpecificLog log : parameters.getSpecificLogs()) { + writeSpecificAppender(writer, log); + } + } + + private static void writeSpecificAppender(XMLStreamWriter writer, DynaWaltzParameters.SpecificLog log) throws XMLStreamException { + writer.writeEmptyElement(DYN_URI, "appender"); + writer.writeAttribute("tag", log.toString()); + writer.writeAttribute("file", log.getFileName() + ".log"); + writer.writeAttribute("lvlFilter", DynaWaltzParameters.LogLevel.DEBUG.toString()); } } diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java index 79a8a07ab..61070f28b 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/DynaWaltzParametersTest.java @@ -15,6 +15,7 @@ import com.powsybl.dynawaltz.DynaWaltzParameters.ExportMode; import com.powsybl.dynawaltz.DynaWaltzParameters.LogLevel; import com.powsybl.dynawaltz.DynaWaltzParameters.SolverType; +import com.powsybl.dynawaltz.DynaWaltzParameters.SpecificLog; import com.powsybl.dynawaltz.parameters.Parameter; import com.powsybl.dynawaltz.parameters.ParameterType; import com.powsybl.dynawaltz.xml.ParametersXml; @@ -25,8 +26,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; -import java.util.Objects; +import java.util.*; import static org.junit.jupiter.api.Assertions.*; @@ -63,7 +63,8 @@ void checkParameters() throws IOException { double precision = 1e-8; ExportMode timelinExportMode = ExportMode.XML; LogLevel logLevel = LogLevel.WARN; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers, precision, timelinExportMode, logLevel); + Set specificLogs = EnumSet.of(SpecificLog.MODELER, SpecificLog.EQUATIONS); + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, useModelSimplifiers, precision, timelinExportMode, logLevel, specificLogs); DynaWaltzParameters parameters = DynaWaltzParameters.load(platformConfig, fileSystem); @@ -97,6 +98,7 @@ void checkParameters() throws IOException { assertEquals(precision, parameters.getPrecision()); assertEquals(timelinExportMode, parameters.getTimelineExportMode()); assertEquals(logLevel, parameters.getLogLevelFilter()); + assertEquals(specificLogs, parameters.getSpecificLogs()); } @Test @@ -119,7 +121,7 @@ void roundTripParametersSerializing() throws IOException { SolverType solverType = SolverType.IDA; String solverParametersId = "solverParametersId"; boolean mergeLoads = false; - initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false, 1e-7, ExportMode.TXT, LogLevel.INFO); + initPlatformConfig(networkParametersId, solverType, solverParametersId, mergeLoads, false, 1e-7, ExportMode.TXT, LogLevel.INFO, Set.of(SpecificLog.PARAMETERS, SpecificLog.VARIABLES)); DynamicSimulationParameters dynamicSimulationParameters = new DynamicSimulationParameters() .setStartTime(0) @@ -130,7 +132,9 @@ void roundTripParametersSerializing() throws IOException { JsonDynamicSimulationParameters::read, "/DynaWaltzParameters.json"); } - private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, boolean mergeLoads, boolean useModelSimplifiers, double precision, ExportMode timelineExportMode, DynaWaltzParameters.LogLevel logLevel) throws IOException { + private void initPlatformConfig(String networkParametersId, SolverType solverType, String solverParametersId, + boolean mergeLoads, boolean useModelSimplifiers, double precision, ExportMode timelineExportMode, + LogLevel logLevel, Set specificLogs) throws IOException { String parametersFile = USER_HOME + "parametersFile"; String networkParametersFile = USER_HOME + "networkParametersFile"; String solverParametersFile = USER_HOME + "solverParametersFile"; @@ -147,6 +151,8 @@ private void initPlatformConfig(String networkParametersId, SolverType solverTyp moduleConfig.setStringProperty("precision", Double.toString(precision)); moduleConfig.setStringProperty("timeline.exportMode", String.valueOf(timelineExportMode)); moduleConfig.setStringProperty("log.levelFilter", logLevel.toString()); + moduleConfig.setStringListProperty("log.specificLogs", specificLogs.stream().map(SpecificLog::toString).toList()); + Files.createDirectories(fileSystem.getPath(USER_HOME)); copyFile("/parametersSet/models.par", parametersFile); diff --git a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/JobsXmlTest.java b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/JobsXmlTest.java index 25a83d0bd..034326916 100644 --- a/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/JobsXmlTest.java +++ b/dynawaltz/src/test/java/com/powsybl/dynawaltz/xml/JobsXmlTest.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.util.EnumSet; /** * @author Marcos de Miguel {@literal } @@ -42,4 +43,15 @@ void writeJobWithDumpFile() throws SAXException, IOException { validate("jobs.xsd", "jobsWithDump.xml", tmpDir.resolve(DynaWaltzConstants.JOBS_FILENAME)); } + @Test + void writeJobWithSpecificLogs() throws SAXException, IOException { + DynamicSimulationParameters parameters = DynamicSimulationParameters.load(); + DynaWaltzParameters dynawoParameters = DynaWaltzParameters.load() + .setSpecificLogs(EnumSet.allOf(DynaWaltzParameters.SpecificLog.class)); + DynaWaltzContext context = new DynaWaltzContext(network, network.getVariantManager().getWorkingVariantId(), dynamicModels, eventModels, curves, parameters, dynawoParameters); + + JobsXml.write(tmpDir, context); + validate("jobs.xsd", "jobsWithSpecificLogs.xml", tmpDir.resolve(DynaWaltzConstants.JOBS_FILENAME)); + } + } diff --git a/dynawaltz/src/test/resources/DynaWaltzParameters.json b/dynawaltz/src/test/resources/DynaWaltzParameters.json index 475da85df..a111be48f 100644 --- a/dynawaltz/src/test/resources/DynaWaltzParameters.json +++ b/dynawaltz/src/test/resources/DynaWaltzParameters.json @@ -49,6 +49,7 @@ "precision" : 1.0E-7, "timelineExportMode" : "TXT", "logLevelFilter" : "INFO", + "specificLogs" : [ "PARAMETERS", "VARIABLES" ], "modelsParameters" : [ { "id" : "test", "parameters" : { diff --git a/dynawaltz/src/test/resources/jobsWithSpecificLogs.xml b/dynawaltz/src/test/resources/jobsWithSpecificLogs.xml new file mode 100644 index 000000000..70f226cd9 --- /dev/null +++ b/dynawaltz/src/test/resources/jobsWithSpecificLogs.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +