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

Commit f001583

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Implement XML logger for Cpp runtime in OMEdit
1 parent 1e16904 commit f001583

File tree

5 files changed

+324
-115
lines changed

5 files changed

+324
-115
lines changed

Compiler/Template/CodegenCpp.tpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,6 @@ int _tmain(int argc, const _TCHAR* argv[])
16481648
//SimController to start simulation
16491649

16501650
std::pair<shared_ptr<ISimController>, SimSettings> simulation = _factory->createSimulation(argc, argv, opts);
1651-
//Logger::initialize(simulation.second.logSettings);
16521651

16531652
//create Modelica system
16541653
shared_ptr<ISimObjects> simObjects= simulation.first->getSimObjects();
@@ -2328,7 +2327,6 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
23282327
//SimController to start simulation
23292328

23302329
std::pair<shared_ptr<ISimController>, SimSettings> simulation = _factory->createSimulation(argc, argv, opts);
2331-
Logger::initialize(simulation.second.logSettings);
23322330

23332331
//create Modelica system
23342332
shared_ptr<ISimObjects> simObjects= simulation.first->getSimObjects();

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

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* logger.cpp
33
*
44
* Created on: 04.06.2015
5-
* Author: marcus
5+
* Author: marcus and rfranke
66
*/
77
#include <Core/ModelicaDefine.h>
88
#include <Core/Modelica.h>
@@ -23,35 +23,36 @@ Logger::~Logger()
2323
{
2424
}
2525

26-
void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl)
26+
void Logger::initialize(LogSettings settings)
2727
{
28-
if(isOutput(cat, lvl))
29-
{
30-
std::cerr << getPrefix(cat,lvl) << msg << std::endl;
31-
}
32-
}
28+
if (instance != NULL)
29+
delete instance;
3330

34-
void Logger::setEnabledInternal(bool enabled)
35-
{
36-
_isEnabled = enabled;
31+
switch (settings.format) {
32+
case LF_XML:
33+
instance = new LoggerXML(settings, true);
34+
break;
35+
default:
36+
instance = new Logger(settings, true);
37+
}
3738
}
3839

39-
bool Logger::isEnabledInternal()
40+
void Logger::writeInternal(std::string msg, LogCategory cat, LogLevel lvl, bool)
4041
{
41-
return _isEnabled;
42+
if (msg != "")
43+
std::cerr << getPrefix(cat, lvl) << ": " << msg << std::endl;
4244
}
4345

44-
bool Logger::isOutput(LogCategory cat, LogLevel lvl) const
46+
void Logger::setEnabledInternal(bool enabled)
4547
{
46-
return _settings.modes[cat] >= lvl && _isEnabled;
48+
_isEnabled = enabled;
4749
}
4850

49-
bool Logger::isOutput(std::pair<LogCategory,LogLevel> mode) const
51+
bool Logger::isEnabledInternal()
5052
{
51-
return isOutput(mode.first, mode.second);
53+
return _isEnabled;
5254
}
5355

54-
5556
std::string Logger::getPrefix(LogCategory cat, LogLevel lvl) const
5657
{
5758
switch(lvl)
@@ -69,3 +70,67 @@ std::string Logger::getPrefix(LogCategory cat, LogLevel lvl) const
6970

7071
}
7172
}
73+
74+
std::string Logger::getCategory(LogCategory cat) const
75+
{
76+
switch (cat) {
77+
case(LC_INIT):
78+
return "init";
79+
case(LC_NLS):
80+
return "nls";
81+
case(LC_LS):
82+
return "ls";
83+
case(LC_SOLV):
84+
return "solver";
85+
case(LC_OUT):
86+
return "output";
87+
case(LC_EVT):
88+
return "events";
89+
case(LC_MOD):
90+
return "model";
91+
case(LC_OTHER):
92+
default:
93+
return "other";
94+
}
95+
}
96+
97+
std::string Logger::getLevel(LogLevel lvl) const
98+
{
99+
switch(lvl) {
100+
case(LL_ERROR):
101+
return "error";
102+
case(LL_WARNING):
103+
return "warning";
104+
case(LL_DEBUG):
105+
//return "debug"; // avoid red color in OMEdit
106+
case(LL_INFO):
107+
default:
108+
return "info";
109+
}
110+
}
111+
112+
LoggerXML::LoggerXML(LogSettings settings, bool enabled)
113+
: Logger(settings, enabled)
114+
{
115+
}
116+
117+
LoggerXML::~LoggerXML()
118+
{
119+
}
120+
121+
void LoggerXML::writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
122+
bool ready)
123+
{
124+
if (msg != "") {
125+
std::cout << "<message stream=\"" << getCategory(cat) << "\" "
126+
<< "type=\"" << getLevel(lvl) << "\" "
127+
<< "text=\"" << msg << "\"";
128+
if (ready)
129+
std::cout << " />" << std::endl;
130+
else
131+
std::cout << " >" << std::endl;
132+
}
133+
else if (ready) {
134+
std::cout << "</message>" << std::endl;
135+
}
136+
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@ using std::string;
2727

2828
enum LogCategory {LC_INIT = 0, LC_NLS = 1, LC_LS = 2, LC_SOLV = 3, LC_OUT = 4, LC_EVT = 5, LC_OTHER = 6, LC_MOD = 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};
31+
enum LogOMEdit {LOG_EVENTS = 0, LOG_INIT, LOG_LS, LOG_NLS, LOG_SOLVER, LOG_STATS};
3032
enum OutputPointType {OPT_ALL, OPT_STEP, OPT_NONE};
3133
enum OutputFormat {CSV, MAT, BUFFER, EMPTY};
3234
enum EmitResults {EMIT_ALL, EMIT_PUBLIC, EMIT_NONE};
35+
3336
struct LogSettings
3437
{
35-
std::vector<LogLevel> modes;
38+
std::vector<LogLevel> modes;
39+
LogFormat format;
3640

37-
LogSettings()
38-
{
39-
modes = std::vector<LogLevel>(8,LL_WARNING);
40-
}
41+
LogSettings()
42+
{
43+
modes = std::vector<LogLevel>(8, LL_ERROR);
44+
format = LF_TXT;
45+
}
4146

42-
void setAll(LogLevel l)
43-
{
44-
for(unsigned i = 0; i < modes.size() ; ++i)
45-
modes[i] = l;
46-
}
47+
void setAll(LogLevel l)
48+
{
49+
for (unsigned i = 0; i < modes.size() ; ++i)
50+
modes[i] = l;
51+
}
4752
};
4853

4954
class IGlobalSettings

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

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
* logger.hpp
33
*
44
* Created on: 04.06.2015
5-
* Author: marcus
5+
* Author: marcus and rfranke
66
*/
77

88
#ifndef LOGGER_HPP_
99
#define LOGGER_HPP_
1010

1111
#ifdef USE_LOGGER
12-
#define LOGGER_WRITE(x,y,z) Logger::write(x,y,z)
13-
#define LOGGER_WRITE_TUPLE(x,y) Logger::write(x,y)
12+
#define LOGGER_WRITE(msg, cat, lvl) Logger::write(msg, cat, lvl)
13+
#define LOGGER_WRITE_TUPLE(msg, mode) Logger::write(msg, mode)
14+
#define LOGGER_WRITE_BEGIN(msg, cat, lvl) Logger::writeBegin(msg, cat, lvl)
15+
#define LOGGER_WRITE_END(cat, lvl) Logger::writeEnd(cat, lvl)
1416
#else
15-
#define LOGGER_WRITE(x,y,z)
16-
#define LOGGER_WRITE_TUPLE(x,y)
17+
#define LOGGER_WRITE(msg, cat, lvl)
18+
#define LOGGER_WRITE_TUPLE(msg, mode)
19+
#define LOGGER_WRITE_BEGIN(msg, cat, lvl)
20+
#define LOGGER_WRITE_END(cat, lvl)
1721
#endif //USE_LOGGER
1822

1923
class BOOST_EXTENSION_LOGGER_DECL Logger
@@ -23,19 +27,13 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
2327

2428
static Logger* getInstance()
2529
{
26-
if(instance == NULL)
30+
if (instance == NULL)
2731
initialize(LogSettings());
2832

2933
return instance;
3034
}
3135

32-
static void initialize(LogSettings settings)
33-
{
34-
if(instance != NULL)
35-
delete instance;
36-
37-
instance = new Logger(settings, true);
38-
}
36+
static void initialize(LogSettings settings);
3937

4038
static void initialize()
4139
{
@@ -44,9 +42,20 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
4442

4543
static inline void write(std::string msg, LogCategory cat, LogLevel lvl)
4644
{
47-
Logger* instance = getInstance();
48-
if(instance && instance->isEnabled())
49-
instance->writeInternal(msg, cat, lvl);
45+
if (instance && instance->isOutput(cat, lvl))
46+
instance->writeInternal(msg, cat, lvl, true);
47+
}
48+
49+
static inline void writeBegin(std::string msg, LogCategory cat, LogLevel lvl)
50+
{
51+
if (instance && instance->isOutput(cat, lvl))
52+
instance->writeInternal(msg, cat, lvl, false);
53+
}
54+
55+
static inline void writeEnd(LogCategory cat, LogLevel lvl)
56+
{
57+
if (instance && instance->isOutput(cat, lvl))
58+
instance->writeInternal("", cat, lvl, true);
5059
}
5160

5261
static inline void write(std::string msg, std::pair<LogCategory,LogLevel> mode)
@@ -61,29 +70,38 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
6170

6271
static bool isEnabled()
6372
{
64-
return getInstance()->isEnabledInternal();
73+
return instance != NULL && instance->isEnabledInternal();
6574
}
6675

6776
static std::pair<LogCategory,LogLevel> getLogMode(LogCategory cat, LogLevel lvl)
6877
{
69-
return std::pair<LogCategory, LogLevel>(cat, lvl);
78+
return std::pair<LogCategory, LogLevel>(cat, lvl);
7079
}
7180

72-
bool isOutput(LogCategory cat, LogLevel lvl) const;
81+
bool isOutput(LogCategory cat, LogLevel lvl) const
82+
{
83+
return _isEnabled && _settings.modes[cat] >= lvl;
84+
}
7385

74-
bool isOutput(std::pair<LogCategory,LogLevel> mode) const;
86+
bool isOutput(std::pair<LogCategory,LogLevel> mode) const
87+
{
88+
return isOutput(mode.first, mode.second);
89+
}
7590

7691
protected:
7792
Logger(LogSettings settings, bool enabled);
7893

7994
Logger(bool enabled);
8095

81-
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl);
96+
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
97+
bool ready);
98+
8299
virtual void setEnabledInternal(bool enabled);
83100
virtual bool isEnabledInternal();
84101

85102
std::string getPrefix(LogCategory cat, LogLevel lvl) const;
86-
103+
std::string getCategory(LogCategory cat) const;
104+
std::string getLevel(LogLevel lvl) const;
87105

88106
static Logger* instance;
89107

@@ -92,4 +110,18 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
92110
bool _isEnabled;
93111
};
94112

113+
class BOOST_EXTENSION_LOGGER_DECL LoggerXML: Logger
114+
{
115+
friend class Logger;
116+
117+
public:
118+
virtual ~LoggerXML();
119+
120+
protected:
121+
LoggerXML(LogSettings settings, bool enabled);
122+
123+
virtual void writeInternal(std::string msg, LogCategory cat, LogLevel lvl,
124+
bool ready);
125+
};
126+
95127
#endif /* LOGGER_HPP_ */

0 commit comments

Comments
 (0)