Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit a556ede

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Connect Cpp Logger to FMI2 callback function
1 parent edbd85c commit a556ede

File tree

6 files changed

+126
-20
lines changed

6 files changed

+126
-20
lines changed

SimulationRuntime/cpp/Core/Utils/extension/logger.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ void Logger::initialize(LogSettings settings)
3737
}
3838
}
3939

40-
void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl, bool)
40+
void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
41+
LogStructure ls)
4142
{
42-
if (msg != "")
43+
if (ls != LS_END)
4344
std::cerr << getPrefix(cat, lvl) << ": " << msg << std::endl;
4445
}
4546

@@ -119,18 +120,18 @@ LoggerXML::~LoggerXML()
119120
}
120121

121122
void LoggerXML::writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
122-
bool ready)
123+
LogStructure ls)
123124
{
124-
if (msg != "") {
125+
if (ls != LS_END) {
125126
std::cout << "<message stream=\"" << getCategory(cat) << "\" "
126127
<< "type=\"" << getLevel(lvl) << "\" "
127128
<< "text=\"" << msg << "\"";
128-
if (ready)
129-
std::cout << " />" << std::endl;
130-
else
129+
if (ls == LS_BEGIN)
131130
std::cout << " >" << std::endl;
131+
else
132+
std::cout << " />" << std::endl;
132133
}
133-
else if (ready) {
134+
else {
134135
std::cout << "</message>" << std::endl;
135136
}
136137
}

SimulationRuntime/cpp/FMU/FMULogger.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ FMULogger::~FMULogger()
2323
{
2424
}
2525

26-
void FMULogger::writeInternal(std::string errorMsg, LogCategory cat, LogLevel lvl)
26+
void FMULogger::writeInternal(std::string errorMsg, LogCategory cat, LogLevel lvl, LogStructure ls)
2727
{
28+
if (ls == LS_END)
29+
return;
30+
2831
switch(lvl)
2932
{
3033
case(LL_ERROR):

SimulationRuntime/cpp/Include/Core/Utils/extension/logger.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
4949
static inline void write(std::string msg, LogCategory cat, LogLevel lvl)
5050
{
5151
if (instance && instance->isSet(cat, lvl))
52-
instance->writeInternal(msg, cat, lvl, true);
52+
instance->writeInternal(msg, cat, lvl, LS_NONE);
5353
}
5454

5555
static inline void writeBegin(std::string msg, LogCategory cat, LogLevel lvl)
5656
{
5757
if (instance && instance->isSet(cat, lvl))
58-
instance->writeInternal(msg, cat, lvl, false);
58+
instance->writeInternal(msg, cat, lvl, LS_BEGIN);
5959
}
6060

6161
static inline void writeEnd(LogCategory cat, LogLevel lvl)
6262
{
6363
if (instance && instance->isSet(cat, lvl))
64-
instance->writeInternal("", cat, lvl, true);
64+
instance->writeInternal("", cat, lvl, LS_END);
6565
}
6666

6767
static inline void write(std::string msg, std::pair<LogCategory,LogLevel> mode)
@@ -84,6 +84,16 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
8484
return std::pair<LogCategory, LogLevel>(cat, lvl);
8585
}
8686

87+
void setAll(LogLevel lvl)
88+
{
89+
_settings.setAll(lvl);
90+
}
91+
92+
void set(LogCategory cat, LogLevel lvl)
93+
{
94+
_settings.modes[cat] = lvl;
95+
}
96+
8797
bool isSet(LogCategory cat, LogLevel lvl) const
8898
{
8999
return _isEnabled && _settings.modes[cat] >= lvl;
@@ -99,8 +109,10 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
99109

100110
Logger(bool enabled);
101111

112+
enum LogStructure {LS_NONE, LS_BEGIN, LS_END};
113+
102114
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
103-
bool ready);
115+
LogStructure ls);
104116

105117
virtual void setEnabledInternal(bool enabled);
106118
virtual bool isEnabledInternal();
@@ -127,7 +139,7 @@ class BOOST_EXTENSION_LOGGER_DECL LoggerXML: Logger
127139
LoggerXML(LogSettings settings, bool enabled);
128140

129141
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
130-
bool ready);
142+
LogStructure ls);
131143
};
132144

133145
#endif /* LOGGER_HPP_ */

SimulationRuntime/cpp/Include/FMU/FMULogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BOOST_EXTENSION_EXPORT_DECL FMULogger : Logger
2828
}
2929

3030
protected:
31-
virtual void writeInternal(std::string errorMsg, LogCategory cat, LogLevel lvl);
31+
virtual void writeInternal(std::string errorMsg, LogCategory cat, LogLevel lvl, LogStructure ls);
3232
private:
3333
fmiCallbackLogger callbackLogger;
3434
fmiComponent component;

SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.cpp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,71 @@ fmi2String FMU2Wrapper::LogCategoryFMUName(LogCategoryFMU category) {
5353
return _LogCategoryFMUNames[category];
5454
}
5555

56+
FMU2Logger::FMU2Logger(FMU2Wrapper *wrapper,
57+
LogSettings &logSettings, bool enabled) :
58+
Logger(logSettings, enabled),
59+
_wrapper(wrapper)
60+
{
61+
if (instance != NULL)
62+
delete instance;
63+
instance = this;
64+
}
65+
66+
void FMU2Logger::writeInternal(string msg, LogCategory cat, LogLevel lvl,
67+
LogStructure ls)
68+
{
69+
LogCategoryFMU category;
70+
fmi2Status status;
71+
72+
if (ls == LS_END)
73+
return;
74+
75+
// determine FMI status and category from LogLevel
76+
switch (lvl) {
77+
case LL_ERROR:
78+
status = fmi2Error;
79+
category = logStatusError;
80+
break;
81+
case LL_WARNING:
82+
status = fmi2Warning;
83+
category = logStatusWarning;
84+
break;
85+
default:
86+
status = fmi2OK;
87+
category = logStatusWarning;
88+
}
89+
90+
// override FMU category with matching LogCategory
91+
switch (cat) {
92+
case LC_NLS:
93+
category = logNonlinearSystems;
94+
break;
95+
case LC_EVT:
96+
category = logEvents;
97+
break;
98+
}
99+
100+
// call FMU log function
101+
FMU2_LOG(_wrapper, status, category, msg.c_str());
102+
}
103+
56104
FMU2Wrapper::FMU2Wrapper(fmi2String instanceName, fmi2String GUID,
57105
const fmi2CallbackFunctions *functions,
58106
fmi2Boolean loggingOn) :
59107
_global_settings(),
60108
_functions(*functions),
61-
logger(_functions.logger)
109+
callbackLogger(_functions.logger)
62110
{
63111
_instanceName = instanceName;
64112
_GUID = GUID;
113+
114+
// setup logger
65115
_logCategories = loggingOn? 0xFFFF: 0x0000;
116+
LogSettings logSettings = _global_settings.getLogSettings();
117+
logSettings.setAll(loggingOn? LL_DEBUG: LL_ERROR);
118+
_logger = new FMU2Logger(this, logSettings, loggingOn);
119+
120+
// setup model
66121
_model = createSystemFMU(&_global_settings);
67122
_model->initializeMemory();
68123
_model->initializeFreeVariables();
@@ -76,20 +131,25 @@ FMU2Wrapper::~FMU2Wrapper()
76131
{
77132
delete [] _clock_buffer;
78133
delete _model;
134+
delete _logger;
79135
}
80136

81137
fmi2Status FMU2Wrapper::setDebugLogging(fmi2Boolean loggingOn,
82138
size_t nCategories,
83139
const fmi2String categories[])
84140
{
85141
fmi2Status ret = fmi2OK;
86-
if (nCategories == 0)
142+
_logger->setEnabled(loggingOn);
143+
if (nCategories == 0) {
87144
_logCategories = loggingOn? 0xFFFF: 0x0000;
145+
_logger->setAll(loggingOn? LL_DEBUG: LL_ERROR);
146+
}
88147
else {
89148
int i, j, nSupported = sizeof(_LogCategoryFMUNames) / sizeof(fmi2String);
90149
for (i = 0; i < nCategories; i++) {
91150
if (strcmp(categories[i], "logAll") == 0) {
92151
_logCategories = loggingOn? 0xFFFF: 0x0000;
152+
_logger->setAll(loggingOn? LL_DEBUG: LL_ERROR);
93153
continue;
94154
}
95155
for (j = 0; j < nSupported; j++) {
@@ -98,6 +158,14 @@ fmi2Status FMU2Wrapper::setDebugLogging(fmi2Boolean loggingOn,
98158
_logCategories |= (1 << j);
99159
else
100160
_logCategories &= ~(1 << j);
161+
switch (j) {
162+
case logEvents:
163+
_logger->set(LC_EVT, loggingOn? LL_DEBUG: LL_ERROR);
164+
break;
165+
case logNonlinearSystems:
166+
_logger->set(LC_NLS, loggingOn? LL_DEBUG: LL_ERROR);
167+
break;
168+
}
101169
break;
102170
}
103171
}
@@ -379,4 +447,5 @@ fmi2Status FMU2Wrapper::getNominalsOfContinuousStates(fmi2Real x_nominal[], size
379447
x_nominal[i] = 1.0; // TODO
380448
return fmi2OK;
381449
}
450+
382451
/** @} */ // end of fmu2

SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
// define logger as macro that passes through variadic args
4747
#define FMU2_LOG(w, status, category, ...) \
4848
if ((w)->logCategories() & (1 << (category))) \
49-
(w)->logger((w)->componentEnvironment(), (w)->instanceName(), \
50-
status, (w)->LogCategoryFMUName(category), __VA_ARGS__)
49+
(w)->callbackLogger((w)->componentEnvironment(), (w)->instanceName(), \
50+
status, (w)->LogCategoryFMUName(category), __VA_ARGS__)
5151

5252
enum LogCategoryFMU {
5353
logEvents = 0,
@@ -62,6 +62,25 @@ enum LogCategoryFMU {
6262
logFmi2Call
6363
};
6464

65+
class FMU2Wrapper;
66+
67+
/**
68+
* Forward Logger messages to FMI callback function
69+
*/
70+
class FMU2Logger: public Logger
71+
{
72+
public:
73+
FMU2Logger(FMU2Wrapper *wrapper, LogSettings &logSettings, bool enabled);
74+
75+
protected:
76+
virtual void writeInternal(string msg, LogCategory cat, LogLevel lvl,
77+
LogStructure ls);
78+
FMU2Wrapper *_wrapper;
79+
};
80+
81+
/**
82+
* Wrap a model and a logger for FMI2
83+
*/
6584
class FMU2Wrapper
6685
{
6786
public:
@@ -74,7 +93,7 @@ class FMU2Wrapper
7493
virtual fmi2Status setDebugLogging(fmi2Boolean loggingOn,
7594
size_t nCategories,
7695
const fmi2String categories[]);
77-
const fmi2CallbackLogger &logger;
96+
const fmi2CallbackLogger &callbackLogger;
7897
unsigned int logCategories() {
7998
return _logCategories;
8099
}
@@ -142,6 +161,7 @@ class FMU2Wrapper
142161

143162
private:
144163
FMU2GlobalSettings _global_settings;
164+
FMU2Logger *_logger;
145165
MODEL_CLASS *_model;
146166
std::vector<string> _string_buffer;
147167
bool *_clock_buffer;
@@ -165,4 +185,5 @@ class FMU2Wrapper
165185
fmi2CallbackFunctions _functions;
166186
ModelState _state;
167187
};
188+
168189
/** @} */ // end of fmu2

0 commit comments

Comments
 (0)