Skip to content

Commit

Permalink
Add --log-file option to fmusim (#575)
Browse files Browse the repository at this point in the history
fixes #563
  • Loading branch information
t-sommer authored Sep 10, 2024
1 parent 7dc11ca commit 2f7cbf3
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions fmusim/fmusim.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,40 @@

static FMIStatus logLevel = FMIOK;

static FILE* s_logFile = NULL;

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

FILE* const stream = s_logFile ? s_logFile : stdout;

if (status < logLevel) {
return;
}

switch (status) {
case FMIOK:
printf("[OK] ");
fputs("[OK] ", stream);
break;
case FMIWarning:
printf("[Warning] ");
fputs("[Warning] ", stream);
break;
case FMIDiscard:
printf("[Discard] ");
fputs("[Discard] ", stream);
break;
case FMIError:
printf("[Error] ");
fputs("[Error] ", stream);
break;
case FMIFatal:
printf("[Fatal] ");
fputs("[Fatal] ", stream);
break;
case FMIPending:
printf("[Pending] ");
fputs("[Pending] ", stream);
break;
}

puts(message);
fputs(message, stream);

fputc('\n', stream);
}

static FILE* s_fmiLogFile = NULL;
Expand Down Expand Up @@ -112,6 +118,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-file [FILE] set the log file\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"
Expand All @@ -137,6 +144,7 @@ int main(int argc, const char* argv[]) {

const char* inputFile = NULL;
const char* outputFile = NULL;
const char* logFile = NULL;
const char* fmiLogFile = NULL;
const char* initialFMUStateFile = NULL;
const char* finalFMUStateFile = NULL;
Expand Down Expand Up @@ -232,6 +240,8 @@ int main(int argc, const char* argv[]) {
inputFile = argv[++i];
} else if (!strcmp(v, "--output-file")) {
outputFile = argv[++i];
} else if (!strcmp(v, "--log-file")) {
logFile = argv[++i];
} else if (!strcmp(v, "--fmi-log-file")) {
fmiLogFile = argv[++i];
} else if (!strcmp(v, "--tolerance")) {
Expand Down Expand Up @@ -266,6 +276,14 @@ int main(int argc, const char* argv[]) {

const char* fmuPath = argv[argc - 1];

if (logFile) {
s_logFile = fopen(logFile, "w");
if (!s_logFile) {
printf("Failed to open log file %s for writing.\n", logFile);
goto TERMINATE;
}
}

if (fmiLogFile) {
s_fmiLogFile = fopen(fmiLogFile, "w");
if (!s_fmiLogFile) {
Expand Down Expand Up @@ -523,6 +541,11 @@ int main(int argc, const char* argv[]) {
FMIFree((void**)&unzipdir);
}

if (s_logFile) {
fclose(s_logFile);
s_logFile = NULL;
}

if (s_fmiLogFile) {
fclose(s_fmiLogFile);
s_fmiLogFile = NULL;
Expand Down

0 comments on commit 2f7cbf3

Please sign in to comment.