Skip to content
Snippets Groups Projects
Unverified Commit acca39e2 authored by Martin Christoph Hierholzer's avatar Martin Christoph Hierholzer Committed by GitHub
Browse files

Merge pull request #117 from ChimeraTK/fix_logging_issues

Fix #114 and #115
parents e65a9752 b008ed84
No related branches found
No related tags found
No related merge requests found
......@@ -189,12 +189,17 @@ namespace logging {
private:
ctk::VariableNetworkNode getAccessorPair(const std::string& sender);
/** Map key is the feeding module */
std::map<std::string, ctk::ScalarPushInput<std::string> > msg_list;
/** Find the Message that was updated.
*/
std::map<std::string, ctk::ScalarPushInput<std::string> >::iterator FindSender(const ChimeraTK::TransferElementID& id);
struct MessageSource{
ctk::ScalarPushInput<std::string> msg;
std::string sendingModule;
MessageSource(const std::string &moduleName, Module* module):
msg{module, moduleName + "Msg", "", ""},sendingModule(moduleName){};
};
/** List of senders. */
std::vector<MessageSource > sources;
/** Map key is the transfer id of the ScalarPushInput variable pointed to */
std::map<ChimeraTK::TransferElementID, MessageSource* > id_list;
/** Number of messages stored in the tail */
size_t messageCounter;
......
......@@ -100,25 +100,44 @@ void LoggingModule::mainLoop() {
file.reset(new std::ofstream());
messageCounter = 0;
std::stringstream greeter;
greeter << getName() << " " << getTime() << "There are " << msg_list.size()
greeter << getName() << " " << getTime() << "There are " << sources.size()
<< " modules registered for logging:" << std::endl;
broadcastMessage(greeter.str());
for(auto& module : msg_list) {
broadcastMessage(std::string("\t - ") + module.first);
for(auto module = sources.begin(); module != sources.end();module++) {
broadcastMessage(std::string("\t - ") + module->sendingModule);
id_list[module->msg.getId()] = &(*module);
}
auto group = readAnyGroup();
while(1) {
auto id = group.readAny();
auto sender = FindSender(id);
if(id_list.count(id) == 0){
throw ChimeraTK::logic_error("Cannot find element id"
"when updating logging variables.");
}
std::string msg;
MessageSource* currentSender;
LogLevel level;
try{
currentSender = id_list.at(id);
msg = (std::string)(currentSender->msg);
} catch (std::out_of_range &e){
throw ChimeraTK::logic_error("Cannot find element id"
"when updating logging variables.");
}
try{
level = static_cast<LogLevel>(std::strtoul(&msg.at(0),NULL, 0));
} catch (std::out_of_range &e){
throw ChimeraTK::logic_error("Cannot find message level"
"when updating logging variables.");
}
if(targetStream == 3) continue;
auto strlevel = ((std::string)(sender->second))[0];
LogLevel level = static_cast<LogLevel>(std::strtoul(&strlevel,NULL, 0));
LogLevel setLevel = static_cast<LogLevel>((uint)logLevel);
std::string tmpStr = (std::string)(sender->second);
std::string tmpStr = msg;
// remove message level
tmpStr = tmpStr.substr(1,tmpStr.size());
std::stringstream ss;
ss << level << getName() << "/" << sender->first << " " << getTime() << tmpStr;
ss << level << getName() << "/" << currentSender->sendingModule << " " << getTime() << tmpStr;
if(targetStream == 0 || targetStream == 1) {
if(!((std::string)logFile).empty() && !file->is_open()) {
std::stringstream ss_file;
......@@ -151,24 +170,16 @@ void LoggingModule::addSource(boost::shared_ptr<Logger> logger) {
ctk::VariableNetworkNode LoggingModule::getAccessorPair(
const std::string& sender) {
if(msg_list.count(sender) == 0) {
msg_list.emplace(std::piecewise_construct, std::make_tuple(sender),
std::make_tuple(ctk::ScalarPushInput<std::string>{this, sender + "Msg", "", ""}));
auto it = std::find_if(sources.begin(), sources.end(),
boost::bind(&MessageSource::sendingModule, _1) == sender);
if(it == sources.end()) {
sources.emplace_back(MessageSource{sender,this});
}
else {
throw ChimeraTK::logic_error(
"Cannot add logging for module " + sender + " since logging was already added for this module.");
}
return msg_list[sender];
}
std::map<std::string, ctk::ScalarPushInput<std::string> >::iterator LoggingModule::FindSender(const ChimeraTK::TransferElementID& id) {
for(auto it = msg_list.begin(), iend = msg_list.end(); it != iend; it++) {
if(it->second.getId() == id)
return it;
}
throw ChimeraTK::logic_error("Cannot find element id"
"when updating logging variables.");
return sources.back().msg;
}
void LoggingModule::terminate() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment