Skip to content

Commit

Permalink
Add --log-level option to fmusim (#565)
Browse files Browse the repository at this point in the history
fixes #563
  • Loading branch information
t-sommer authored Aug 20, 2024
1 parent cb4a572 commit 3596ae9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 38 deletions.
10 changes: 10 additions & 0 deletions fmusim-gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,16 @@ void MainWindow::simulate() {

simulationThread->logFMICalls = ui->logFMICallsCheckBox->isChecked();

const QString logLevel = ui->logLevelComboBox->currentText();

if (logLevel == "Warning") {
simulationThread->logLevel = FMIWarning;
} else if (logLevel == "Error") {
simulationThread->logLevel = FMIError;
} else {
simulationThread->logLevel = FMIOK;
}

FMISimulationSettings* settings = simulationThread->settings;

memset(settings, 0, sizeof(FMISimulationSettings));
Expand Down
98 changes: 62 additions & 36 deletions fmusim-gui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,17 @@
<property name="horizontalSpacing">
<number>10</number>
</property>
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="logFMICallsCheckBox">
<property name="text">
<string>Start Time</string>
<string>Log FMI Calls</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="relativeToleranceLineEdit">
<item row="4" column="1">
<widget class="QLineEdit" name="maxSamplesLineEdit">
<property name="text">
<string>1e-5</string>
<string>500</string>
</property>
</widget>
</item>
Expand All @@ -404,10 +404,17 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_13">
<item row="2" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Relative Tolerance</string>
<string>Start Time</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="debugLoggingCheckBox">
<property name="text">
<string>Enable FMU Logging</string>
</property>
</widget>
</item>
Expand All @@ -418,6 +425,16 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QRadioButton" name="maxSamplesRadioButton">
<property name="text">
<string>Max. Samples</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="outputIntervalLineEdit">
<property name="enabled">
Expand All @@ -428,6 +445,20 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="relativeToleranceLineEdit">
<property name="text">
<string>1e-5</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_13">
<property name="text">
<string>Relative Tolerance</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout_11">
Expand Down Expand Up @@ -472,44 +503,39 @@
</layout>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="maxSamplesLineEdit">
<property name="text">
<string>500</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QRadioButton" name="maxSamplesRadioButton">
<property name="text">
<string>Max. Samples</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="logFMICallsCheckBox">
<property name="text">
<string>Log FMI Calls</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="startTimeLineEdit">
<property name="text">
<string>0.0</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="debugLoggingCheckBox">
<item row="9" column="0">
<widget class="QLabel" name="label_14">
<property name="text">
<string>Enable FMU Logging</string>
<string>Log Level</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QComboBox" name="logLevelComboBox">
<item>
<property name="text">
<string>OK</string>
</property>
</item>
<item>
<property name="text">
<string>Warning</string>
</property>
</item>
<item>
<property name="text">
<string>Error</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
4 changes: 3 additions & 1 deletion fmusim-gui/SimulationThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@ void SimulationThread::logMessage(FMIInstance* instance, FMIStatus status, const

SimulationThread *simulationThread = (SimulationThread*)instance->userData;

simulationThread->messages.append(message);
if (status >= simulationThread->logLevel) {
simulationThread->messages.append(message);
}
}
1 change: 1 addition & 0 deletions fmusim-gui/SimulationThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SimulationThread : public QThread
Q_OBJECT

public:
FMIStatus logLevel = FMIOK;
bool logFMICalls = false;
FMIInterfaceType interfaceType;
const char* modelIdentifier = nullptr;
Expand Down
21 changes: 20 additions & 1 deletion fmusim/fmusim.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@

#define PROGNAME "fmusim"


#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)

static FMIStatus logLevel = FMIOK;

static void logMessage(FMIInstance* instance, FMIStatus status, const char* category, const char* message) {

if (status < logLevel) {
return;
}

switch (status) {
case FMIOK:
printf("[OK] ");
Expand Down Expand Up @@ -108,6 +112,7 @@ void printUsage() {
" --input-file [FILE] read input from a CSV file\n"
" --output-file [FILE] write output to a CSV file\n"
" --logging-on enable FMU logging\n"
" --log-level [ok|warning|error] set the log level\n"
" --log-fmi-calls log FMI calls\n"
" --fmi-log-file [FILE] set the FMI log file\n"
" --solver [euler|cvode] the solver to use\n"
Expand Down Expand Up @@ -185,6 +190,20 @@ int main(int argc, const char* argv[]) {

if (!strcmp(v, "--logging-on")) {
loggingOn = true;
} else if (!strcmp(v, "--log-level")) {
if (!strcmp(argv[i + 1], "ok")) {
logLevel = FMIOK;
} else if (!strcmp(argv[i + 1], "warning")) {
logLevel = FMIWarning;
} else if (!strcmp(argv[i + 1], "error")) {
logLevel = FMIError;
} else {
printf(PROGNAME ": unrecognized log level '%s'\n", argv[i + 1]);
printf("Log level must be one of 'ok', 'warning', or 'error'.\n");
status = FMIError;
goto TERMINATE;
}
i++;
} else if (!strcmp(v, "--log-fmi-calls")) {
logFMICalls = true;
} else if (!strcmp(v, "--interface-type")) {
Expand Down

0 comments on commit 3596ae9

Please sign in to comment.