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

Commit ad7adca

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Unify treatment of Logger instance
1 parent 6b059e0 commit ad7adca

File tree

10 files changed

+74
-69
lines changed

10 files changed

+74
-69
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#include <Core/Utils/extension/FactoryExport.h>
1010
#include <Core/Utils/extension/logger.hpp>
1111

12-
Logger* Logger::instance = NULL;
12+
Logger* Logger::_instance = NULL;
1313

14-
Logger::Logger(LogSettings settings, bool enabled) : _settings(settings), _isEnabled(enabled)
15-
{
16-
}
17-
18-
Logger::Logger(bool enabled) : _settings(LogSettings()), _isEnabled(enabled)
14+
Logger::Logger(LogSettings settings, bool enabled)
15+
: _logSettings(settings)
16+
, _isEnabled(enabled)
1917
{
18+
if (_instance != NULL)
19+
delete _instance;
20+
_instance = NULL;
2021
}
2122

2223
Logger::~Logger()
@@ -25,15 +26,12 @@ Logger::~Logger()
2526

2627
void Logger::initialize(LogSettings settings)
2728
{
28-
if (instance != NULL)
29-
delete instance;
30-
3129
switch (settings.format) {
32-
case LF_XML:
33-
instance = new LoggerXML(settings, true);
30+
case LF_TXT:
31+
_instance = new Logger(settings, true);
3432
break;
3533
default:
36-
instance = new Logger(settings, true);
34+
_instance = new LoggerXML(settings, true);
3735
}
3836
}
3937

SimulationRuntime/cpp/FMU/FMULogger.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#include <FMU/FMULogger.h>
1212
/*
1313
#if defined(_MSC_VER) && !defined(RUNTIME_STATIC_LINKING)
14-
Logger* Logger::instance = 0;
14+
Logger* Logger::_instance = 0;
1515
#endif
1616
*/
17-
FMULogger::FMULogger(fmiCallbackLogger callbackLogger, fmiComponent component, fmiString instanceName) : Logger(false),
17+
FMULogger::FMULogger(fmiCallbackLogger callbackLogger, fmiComponent component, fmiString instanceName) : Logger(LogSettings(LF_FMI), false),
1818
callbackLogger(callbackLogger), component(component), instanceName(instanceName)
1919
{
2020
}

SimulationRuntime/cpp/Include/Core/SimulationSettings/IGlobalSettings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using std::string;
2727

2828
enum LogCategory {LC_INIT = 0, LC_NLS = 1, LC_LS = 2, LC_SOLVER = 3, LC_OUTPUT = 4, LC_EVENTS = 5, LC_OTHER = 6, LC_MODEL = 7};
2929
enum LogLevel {LL_ERROR = 0, LL_WARNING = 1, LL_INFO = 2, LL_DEBUG = 3};
30-
enum LogFormat {LF_TXT = 0, LF_XML = 1};
30+
enum LogFormat {LF_TXT = 0, LF_FMI = 1, LF_FMI2 = 2, LF_XML = 3, LF_XMLTCP = 4};
3131
enum LogOMEdit {LOG_EVENTS = 0, LOG_INIT, LOG_LS, LOG_NLS, LOG_SOLVER, LOG_STATS};
3232
enum OutputPointType {OPT_ALL, OPT_STEP, OPT_NONE};
3333
enum OutputFormat {CSV, MAT, BUFFER, EMPTY};
@@ -38,10 +38,10 @@ struct LogSettings
3838
std::vector<LogLevel> modes;
3939
LogFormat format;
4040

41-
LogSettings()
41+
LogSettings(LogFormat fmt = LF_TXT)
4242
{
4343
modes = std::vector<LogLevel>(8, LL_ERROR);
44-
format = LF_TXT;
44+
format = fmt;
4545
}
4646

4747
void setAll(LogLevel l)

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
3636

3737
static Logger* getInstance()
3838
{
39-
if (instance == NULL)
39+
if (_instance == NULL)
4040
initialize(LogSettings());
4141

42-
return instance;
42+
return _instance;
4343
}
4444

4545
static void initialize(LogSettings settings);
@@ -51,20 +51,20 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
5151

5252
static inline void write(std::string msg, LogCategory cat, LogLevel lvl)
5353
{
54-
if (instance && instance->isSet(cat, lvl))
55-
instance->writeInternal(msg, cat, lvl, LS_NONE);
54+
if (_instance && _instance->isSet(cat, lvl))
55+
_instance->writeInternal(msg, cat, lvl, LS_NONE);
5656
}
5757

5858
static inline void writeBegin(std::string msg, LogCategory cat, LogLevel lvl)
5959
{
60-
if (instance && instance->isSet(cat, lvl))
61-
instance->writeInternal(msg, cat, lvl, LS_BEGIN);
60+
if (_instance && _instance->isSet(cat, lvl))
61+
_instance->writeInternal(msg, cat, lvl, LS_BEGIN);
6262
}
6363

6464
static inline void writeEnd(LogCategory cat, LogLevel lvl)
6565
{
66-
if (instance && instance->isSet(cat, lvl))
67-
instance->writeInternal("", cat, lvl, LS_END);
66+
if (_instance && _instance->isSet(cat, lvl))
67+
_instance->writeInternal("", cat, lvl, LS_END);
6868
}
6969

7070
static inline void write(std::string msg, std::pair<LogCategory,LogLevel> mode)
@@ -92,7 +92,7 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
9292

9393
static bool isEnabled()
9494
{
95-
return instance != NULL && instance->isEnabledInternal();
95+
return _instance != NULL && _instance->isEnabledInternal();
9696
}
9797

9898
static std::pair<LogCategory,LogLevel> getLogMode(LogCategory cat, LogLevel lvl)
@@ -102,17 +102,17 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
102102

103103
void setAll(LogLevel lvl)
104104
{
105-
_settings.setAll(lvl);
105+
_logSettings.setAll(lvl);
106106
}
107107

108108
void set(LogCategory cat, LogLevel lvl)
109109
{
110-
_settings.modes[cat] = lvl;
110+
_logSettings.modes[cat] = lvl;
111111
}
112112

113113
bool isSet(LogCategory cat, LogLevel lvl) const
114114
{
115-
return _isEnabled && _settings.modes[cat] >= lvl;
115+
return _isEnabled && _logSettings.modes[cat] >= lvl;
116116
}
117117

118118
bool isSet(std::pair<LogCategory,LogLevel> mode) const
@@ -123,8 +123,6 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
123123
protected:
124124
Logger(LogSettings settings, bool enabled);
125125

126-
Logger(bool enabled);
127-
128126
enum LogStructure {LS_NONE, LS_BEGIN, LS_END};
129127

130128
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
@@ -137,10 +135,8 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
137135
std::string getCategory(LogCategory cat) const;
138136
std::string getLevel(LogLevel lvl) const;
139137

140-
static Logger* instance;
141-
142-
private:
143-
LogSettings _settings;
138+
static Logger* _instance;
139+
LogSettings _logSettings;
144140
bool _isEnabled;
145141
};
146142

SimulationRuntime/cpp/Include/FMU/FMUGlobalSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FMUGlobalSettings : public IGlobalSettings
3333
virtual bool getInfoOutput() { return false; }
3434
virtual void setInfoOutput(bool) {}
3535
virtual string getOutputPath() { return "./"; }
36-
virtual LogSettings getLogSettings() {return LogSettings();}
36+
virtual LogSettings getLogSettings() {return LogSettings(LF_FMI);}
3737
virtual void setLogSettings(LogSettings) {}
3838
virtual OutputPointType getOutputPointType() { return OPT_ALL; };
3939
virtual void setOutputPointType(OutputPointType) {};

SimulationRuntime/cpp/Include/FMU/FMULogger.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ class BOOST_EXTENSION_EXPORT_DECL FMULogger : Logger
2121

2222
static void initialize(fmiCallbackLogger callbackLogger, fmiComponent component, fmiString instanceName)
2323
{
24-
if(instance != NULL)
25-
delete instance;
26-
27-
instance = new FMULogger(callbackLogger, component, instanceName);
24+
_instance = new FMULogger(callbackLogger, component, instanceName);
2825
}
2926

3027
protected:

SimulationRuntime/cpp/Include/FMU2/FMU2GlobalSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class FMU2GlobalSettings : public IGlobalSettings
6565
virtual bool getInfoOutput() { return false; }
6666
virtual void setInfoOutput(bool) {}
6767
virtual string getOutputPath() { return "./"; }
68-
virtual LogSettings getLogSettings() { return LogSettings(); }
68+
virtual LogSettings getLogSettings() { return LogSettings(LF_FMI2); }
6969
virtual void setLogSettings(LogSettings) {}
7070
virtual OutputPointType getOutputPointType() { return OPT_ALL; };
7171
virtual void setOutputPointType(OutputPointType) {};

SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ FMU2Logger::FMU2Logger(FMU2Wrapper *wrapper,
5858
Logger(logSettings, enabled),
5959
_wrapper(wrapper)
6060
{
61-
if (instance != NULL)
62-
delete instance;
63-
instance = this;
61+
}
62+
63+
void FMU2Logger::initialize(FMU2Wrapper *wrapper, LogSettings &logSettings, bool enabled)
64+
{
65+
_instance = new FMU2Logger(wrapper, logSettings, enabled);
6466
}
6567

6668
void FMU2Logger::writeInternal(string msg, LogCategory cat, LogLevel lvl,
@@ -115,7 +117,8 @@ FMU2Wrapper::FMU2Wrapper(fmi2String instanceName, fmi2String GUID,
115117
_logCategories = loggingOn? 0xFFFF: 0x0000;
116118
LogSettings logSettings = _global_settings.getLogSettings();
117119
logSettings.setAll(loggingOn? LL_DEBUG: LL_ERROR);
118-
_logger = new FMU2Logger(this, logSettings, loggingOn);
120+
FMU2Logger::initialize(this, logSettings, loggingOn);
121+
_logger = Logger::getInstance();
119122

120123
// setup model
121124
_model = createSystemFMU(&_global_settings);
@@ -131,7 +134,6 @@ FMU2Wrapper::~FMU2Wrapper()
131134
{
132135
delete [] _clock_buffer;
133136
delete _model;
134-
delete _logger;
135137
}
136138

137139
fmi2Status FMU2Wrapper::setDebugLogging(fmi2Boolean loggingOn,

SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ class FMU2Wrapper;
7070
class FMU2Logger: public Logger
7171
{
7272
public:
73-
FMU2Logger(FMU2Wrapper *wrapper, LogSettings &logSettings, bool enabled);
73+
static void initialize(FMU2Wrapper *wrapper, LogSettings &logSettings, bool enabled);
7474

7575
protected:
76+
FMU2Logger(FMU2Wrapper *wrapper, LogSettings &logSettings, bool enabled);
77+
7678
virtual void writeInternal(string msg, LogCategory cat, LogLevel lvl,
7779
LogStructure ls);
7880
FMU2Wrapper *_wrapper;
@@ -161,7 +163,7 @@ class FMU2Wrapper
161163

162164
private:
163165
FMU2GlobalSettings _global_settings;
164-
FMU2Logger *_logger;
166+
Logger *_logger;
165167
MODEL_CLASS *_model;
166168
std::vector<string> _string_buffer;
167169
bool *_clock_buffer;

SimulationRuntime/cpp/SimCoreFactory/OMCFactory/OMCFactory.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,44 @@ namespace po = boost::program_options;
2828
class LoggerXMLTCP: public LoggerXML
2929
{
3030
public:
31-
LoggerXMLTCP(std::string host, int port, LogSettings &logSettings):
32-
LoggerXML(logSettings, true, _sstream),
33-
_endpoint(boost::asio::ip::address::from_string(host), port),
34-
_socket(_ios)
31+
virtual ~LoggerXMLTCP()
3532
{
36-
_socket.connect(_endpoint);
37-
if (instance != NULL)
38-
delete instance;
39-
instance = this;
33+
_socket.close();
4034
}
4135

42-
virtual ~LoggerXMLTCP()
36+
static void initialize(std::string host, int port, LogSettings &logSettings)
4337
{
44-
_socket.close();
38+
_instance = new LoggerXMLTCP(host, port, logSettings);
4539
}
4640

4741
protected:
48-
boost::asio::io_service _ios;
49-
boost::asio::ip::tcp::endpoint _endpoint;
50-
boost::asio::ip::tcp::socket _socket;
51-
stringstream _sstream;
42+
LoggerXMLTCP(std::string host, int port, LogSettings &logSettings)
43+
: LoggerXML(logSettings, true, _sstream)
44+
, _endpoint(boost::asio::ip::address::from_string(host), port)
45+
, _socket(_ios)
46+
{
47+
if (logSettings.format != LF_XML && logSettings.format != LF_XMLTCP) {
48+
throw ModelicaSimulationError(MODEL_FACTORY,
49+
"xmltcp logger requires log-format xml");
50+
}
51+
_socket.connect(_endpoint);
52+
}
5253

5354
virtual void writeInternal(string msg, LogCategory cat, LogLevel lvl,
5455
LogStructure ls)
5556
{
5657
_sstream.str("");
5758
LoggerXML::writeInternal(msg, cat, lvl, ls);
58-
_socket.send(boost::asio::buffer(_sstream.str()));
59+
if (_logSettings.format == LF_XMLTCP)
60+
_socket.send(boost::asio::buffer(_sstream.str()));
61+
else
62+
std::cout << _sstream.str();
5963
}
64+
65+
boost::asio::io_service _ios;
66+
boost::asio::ip::tcp::endpoint _endpoint;
67+
boost::asio::ip::tcp::socket _socket;
68+
std::stringstream _sstream;
6069
};
6170

6271
/**
@@ -123,7 +132,7 @@ static LogSettings initializeLogger(const po::variables_map& vm)
123132
"info", LL_INFO MAP_LIST_SEP "debug", LL_DEBUG MAP_LIST_END;
124133
map<string, LogFormat> logFormatMap = MAP_LIST_OF
125134
"txt", LF_TXT MAP_LIST_SEP "xml", LF_XML MAP_LIST_SEP
126-
"xmltcp", LF_XML MAP_LIST_END;
135+
"xmltcp", LF_XMLTCP MAP_LIST_END;
127136
map<string, LogOMEdit> logOMEditMap = MAP_LIST_OF
128137
"LOG_EVENTS", LOG_EVENTS MAP_LIST_SEP "LOG_INIT", LOG_INIT MAP_LIST_SEP
129138
"LOG_LS", LOG_LS MAP_LIST_SEP "LOG_NLS", LOG_NLS MAP_LIST_SEP
@@ -198,9 +207,8 @@ static LogSettings initializeLogger(const po::variables_map& vm)
198207
logSettings.modes[i] = LL_WARNING;
199208
}
200209

201-
string logFormat_str;
202210
if (vm.count("log-format")) {
203-
logFormat_str = vm["log-format"].as<string>();
211+
string logFormat_str = vm["log-format"].as<string>();
204212
if (logFormatMap.find(logFormat_str) != logFormatMap.end())
205213
logSettings.format = logFormatMap[logFormat_str];
206214
else
@@ -215,12 +223,14 @@ static LogSettings initializeLogger(const po::variables_map& vm)
215223
// initialize logger if it has been enabled
216224
if (Logger::isEnabled()) {
217225
int port = vm["log-port"].as<int>();
218-
if (port > 0 && logFormat_str == "xmltcp") {
226+
if (port > 0) {
219227
try {
220-
new LoggerXMLTCP("127.0.0.1", port, logSettings);
228+
LoggerXMLTCP::initialize("127.0.0.1", port, logSettings);
221229
}
222230
catch (std::exception &ex) {
223-
std::cerr << "Failed to start xmltcp logger: " << ex.what() << std::endl;
231+
throw ModelicaSimulationError(MODEL_FACTORY,
232+
"Failed to start logger with port " + to_string(port) + ": "
233+
+ ex.what() + '\n');
224234
}
225235
}
226236
else

0 commit comments

Comments
 (0)