Skip to content

Commit 2dbd1e7

Browse files
committed
Exposed the ability to setup custom log formating for a logger. To support non traditional log messages for cmd_bio using the same log engine
1 parent 5a8d315 commit 2dbd1e7

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

projects/biogears/libBiogears/include/biogears/cdm/utils/Logger.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class BIOGEARS_API Logger {
8383
virtual ~Logger();
8484

8585
void LogToConsole(bool log_to_console);
86+
void FormatMessages(bool format_messages);
8687

8788
void ResetLogFile(const std::string& logFilename = Loggable::empty, const std::string& working_dir = Loggable::empty);
8889
void ResetLogFile(const char* logFilename , const char* working_dir = Loggable::empty_cStr);
@@ -93,6 +94,9 @@ class BIOGEARS_API Logger {
9394

9495
virtual void SetLogTime(const SEScalarTime* time);
9596

97+
void SetsetConversionPattern(const std::string&);
98+
void SetConsolesetConversionPattern(const std::string&);
99+
96100
virtual void SetForward(LoggerForward* forward);
97101
virtual bool HasForward();
98102

@@ -126,5 +130,6 @@ class BIOGEARS_API Logger {
126130
log4cpp::Appender* m_ConsoleAppender;
127131
const SEScalarTime* m_time;
128132
std::stringstream m_ss;
133+
bool m_FormatMessages;
129134
};
130135
}

projects/biogears/libBiogears/src/cdm/utils/Logger.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ const char* Loggable::empty_cStr("");
2929
Logger::Logger(const std::string& logFilename, const std::string& working_dir)
3030
: m_Forward(nullptr)
3131
, m_time(nullptr)
32+
, m_FormatMessages(true)
3233
{
3334
ResetLogFile(logFilename, working_dir);
3435
}
3536

3637
Logger::Logger(const char* logFilename, const char* working_dir)
3738
: m_Forward(nullptr)
3839
, m_time(nullptr)
40+
, m_FormatMessages(true)
3941
{
4042
ResetLogFile(logFilename, working_dir);
4143
}
@@ -49,9 +51,16 @@ void Logger::LogToConsole(bool log_to_console)
4951
}
5052
}
5153

54+
void Logger::FormatMessages(bool format_messages)
55+
{
56+
m_FormatMessages = format_messages;
57+
}
5258
void Logger::ResetLogFile(const std::string& logFilename, const std::string& working_dir)
5359
{
54-
std::string key = logFilename; if( logFilename.empty()) { key = "biogears_logger"; }
60+
std::string key = logFilename;
61+
if (logFilename.empty()) {
62+
key = "biogears_logger";
63+
}
5564
log4cpp::Category& category = log4cpp::Category::getInstance(key);
5665
m_Log = &category;
5766
m_Log->removeAllAppenders();
@@ -115,7 +124,18 @@ void Logger::SetConsoleLogLevel(log4cpp::Priority::Value priority)
115124
m_ConsoleAppender->setThreshold(priority);
116125
}
117126
}
118-
127+
void Logger::SetsetConversionPattern(const std::string& layout)
128+
{
129+
log4cpp::PatternLayout* cLayout = new log4cpp::PatternLayout();
130+
cLayout->setConversionPattern(layout);
131+
m_FileAppender->setLayout(cLayout);
132+
}
133+
void Logger::SetConsolesetConversionPattern(const std::string& layout)
134+
{
135+
log4cpp::PatternLayout* cLayout = new log4cpp::PatternLayout();
136+
cLayout->setConversionPattern(layout);
137+
m_ConsoleAppender->setLayout(cLayout);
138+
}
119139
// This function will return the priority of the logger
120140
log4cpp::Priority::Value Logger::GetLogLevel()
121141
{
@@ -129,15 +149,19 @@ bool Logger::HasForward() { return m_Forward == nullptr ? false : true; }
129149
std::string Logger::FormatLogMessage(const std::string& msg,
130150
const std::string& origin)
131151
{
132-
m_ss.str("");
133-
m_ss.clear();
134-
if (m_time != nullptr && m_time->IsValid())
135-
m_ss << "[" << *m_time << "] " << msg;
136-
else
137-
m_ss << msg;
138-
if (msg.empty())
139-
return origin;
140-
return origin + " : " + m_ss.str();
152+
if (m_FormatMessages) {
153+
m_ss.str("");
154+
m_ss.clear();
155+
if (m_time != nullptr && m_time->IsValid())
156+
m_ss << "[" << *m_time << "] " << msg;
157+
else
158+
m_ss << msg;
159+
if (msg.empty())
160+
return origin;
161+
return origin + " : " + m_ss.str();
162+
} else {
163+
return msg;
164+
}
141165
}
142166

143167
void Logger::Debug(const std::string& msg, const std::string& origin)

projects/cmd_bio/src/exec/Driver.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ void Driver::queue_Scenario(Executor exec)
146146
}
147147
}
148148

149+
if (multi_patient_run) {
150+
ex.Name(ex.Name() + "-" + patient_no_extension);
151+
}
149152
std::string base_file_name = (multi_patient_run) ? scenario_no_extension + "-" + patient_no_extension : scenario_no_extension;
150153
std::string console_file = base_file_name + ".log";
151154
std::string log_file = base_file_name + "Results.log";
@@ -156,6 +159,10 @@ void Driver::queue_Scenario(Executor exec)
156159
Logger file_logger(ex.Computed() + parent_dir + console_file);
157160
try {
158161
file_logger.SetConsoleLogLevel(log4cpp::Priority::WARN);
162+
file_logger.SetConsolesetConversionPattern("%d{%H:%M} [%p] " + ex.Name() + " %m%n");
163+
console_logger.SetConsolesetConversionPattern("%d{%H:%M} [%p] %m%n");
164+
console_logger.FormatMessages(false);
165+
159166
eng = CreateBioGearsEngine(&file_logger);
160167
} catch (std::exception e) {
161168
std::cout << e.what();
@@ -168,14 +175,13 @@ void Driver::queue_Scenario(Executor exec)
168175

169176
//NOTE:Assuming a Patient File
170177
sce.GetInitialParameters().SetPatientFile(ex.Patient());
171-
console_logger.Info("Starting Scenario: " + trimed_scenario_path);
172-
try
173-
{
174-
BioGearsScenarioExec bse{*eng};
178+
console_logger.Info("Starting "+ ex.Name());
179+
try {
180+
BioGearsScenarioExec bse{ *eng };
175181
bse.Execute(sce, ex.Computed() + parent_dir + results_file, nullptr);
176-
console_logger.Info("Completed Scenario: " + trimed_scenario_path);
177-
} catch (...){
178-
console_logger.Info("Scenario: " + trimed_scenario_path + " Failed.");
182+
console_logger.Info("Completed "+ ex.Name());
183+
} catch (...) {
184+
console_logger.Error("Failed "+ ex.Name());
179185
}
180186
};
181187

@@ -188,19 +194,16 @@ void Driver::queue_Scenario(Executor exec)
188194
}
189195
}
190196
std::unique_ptr<mil::tatrc::physiology::datamodel::ScenarioData> scenario;
191-
try
192-
{
197+
try {
193198
xml_schema::flags xml_flags;
194199
xml_schema::properties xml_properties;
195200

196-
xml_properties.schema_location("uri:/mil/tatrc/physiology/datamodel","xsd/BioGearsDataModel.xsd");
197-
scenario = mil::tatrc::physiology::datamodel::Scenario(ifs,xml_flags, xml_properties);
198-
} catch ( std::runtime_error e)
199-
{
201+
xml_properties.schema_location("uri:/mil/tatrc/physiology/datamodel", "xsd/BioGearsDataModel.xsd");
202+
scenario = mil::tatrc::physiology::datamodel::Scenario(ifs, xml_flags, xml_properties);
203+
} catch (std::runtime_error e) {
200204
std::cout << e.what();
201205
return;
202-
} catch (xsd::cxx::tree::parsing<char> e )
203-
{
206+
} catch (xsd::cxx::tree::parsing<char> e) {
204207
std::cout << e;
205208
return;
206209
}

0 commit comments

Comments
 (0)