33 void LogEntry::clear_logEntries()
40 void LogEntry::setLogEntryMaxSize(
int size)
42 entry_list_max_size = size;
43 entry_list.reserve(entry_list_max_size);
47 void LogEntry::setLogEntryMaxSizeEnabled (
bool enable)
49 entry_list_max_size_enabled=enable;
54 if (logInfo.logsize >= entry_list_max_size && entry_list_max_size_enabled)
59 entry_list.push_back(entry);
64 void LogEntryInfo::clear()
82 void LogEntryInfo::clearLastError()
95 if (level>highest_error) highest_error=level;
98 void LoggerEngine::discover (std::list<std::string>& ports)
101 string logger_portname = log_updater->getPortName();
102 p.
open(logger_portname+
"/discover");
108 p.
write(cmd,response);
109 printf (
"%s\n\n", response.
toString().c_str());
110 int size = response.
size();
111 for (
int i=1; i<size; i++)
119 char* log_off =
nullptr;
120 char* yarprun_log_off =
nullptr;
121 log_off = std::strstr((
char*)(n2->
get(1).
toString().c_str()),
"/log/");
125 printf (
"%s\n", logport.c_str());
126 ports.push_back(logport);
128 yarprun_log_off = std::strstr((
char*)(n2->
get(1).
toString().c_str()),
"/yarprunlog/");
132 printf (
"%s\n", logport.c_str());
133 ports.push_back(logport);
139 std::list<std::string>::iterator ports_it;
140 for (ports_it=ports.begin(); ports_it!=ports.end(); ports_it++)
153 std::istringstream iss(*ports_it);
155 getline(iss, token,
'/');
156 getline(iss, token,
'/');
161 std::list<LogEntry>::iterator it;
162 this->log_updater->mutex.lock();
164 for (it = this->log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
173 log_updater->log_list.push_back(entry);
175 this->log_updater->mutex.unlock();
179 void LoggerEngine::connect (
const std::list<std::string>& ports)
185 std::list<std::string>::const_iterator it;
186 for (it = ports.begin(); it != ports.end(); it++)
199 std::string LoggerEngine::logger_thread::getPortName()
201 return logger_portName;
206 logger_portName = _portname;
207 log_list_max_size = _log_list_max_size;
208 log_list_max_size_enabled =
true;
209 listen_to_LOGLEVEL_UNDEFINED =
true;
210 listen_to_LOGLEVEL_TRACE =
true;
211 listen_to_LOGLEVEL_DEBUG =
true;
212 listen_to_LOGLEVEL_INFO =
true;
213 listen_to_LOGLEVEL_WARNING =
true;
214 listen_to_LOGLEVEL_ERROR =
true;
215 listen_to_LOGLEVEL_FATAL =
true;
216 listen_to_YARP_MESSAGES =
true;
217 listen_to_YARPRUN_MESSAGES =
true;
218 unknown_format_received = 0;
221 void LoggerEngine::logger_thread::run()
231 int bufferport_size = logger_port.getPendingReads();
233 while (bufferport_size>0)
235 std::time_t machine_current_time = std::time(
nullptr);
236 char machine_current_time_c [50];
240 sprintf(machine_current_time_c,
"%f",d_time);
241 string machine_current_time_s = string(machine_current_time_c);
244 bufferport_size = logger_port.getPendingReads();
248 fprintf (stderr,
"ERROR: something strange happened here, bufferport_size = %d!\n",bufferport_size);
254 fprintf (stderr,
"ERROR: unknown log format!\n");
255 unknown_format_received++;
259 std::string bottlestring = b->
toString();
268 fprintf(stderr,
"ERROR: unknown log format!\n");
269 unknown_format_received++;
277 sprintf(ttstr,
"%d",count++);
289 fprintf(stderr,
"ERROR: unknown log format!\n");
290 unknown_format_received++;
296 if (p.check(
"level")) {
297 body.
text = p.find(
"message").toString();
299 auto level = p.find(
"level").toString();
300 if (level ==
"TRACE") {
302 }
else if (level ==
"DEBUG") {
304 }
else if (level ==
"INFO") {
306 }
else if (level ==
"WARNING") {
308 }
else if (level ==
"ERROR") {
310 }
else if (level ==
"FATAL") {
316 if (p.check(
"filename")) {
317 body.
filename = p.find(
"filename").asString();
322 if (p.check(
"line")) {
323 body.
line =
static_cast<uint32_t
>(p.find(
"line").asInt32());
328 if (p.check(
"function")) {
329 body.
function = p.find(
"function").asString();
334 if (p.check(
"hostname")) {
335 body.
hostname = p.find(
"hostname").asString();
340 if (p.check(
"pid")) {
341 body.
pid = p.find(
"pid").asInt32();
346 if (p.check(
"cmd")) {
347 body.
cmd = p.find(
"cmd").asString();
352 if (p.check(
"args")) {
353 body.
args = p.find(
"args").asString();
358 if (p.check(
"thread_id")) {
359 body.
thread_id = p.find(
"thread_id").asInt64();
364 if (p.check(
"component")) {
365 body.
component = p.find(
"component").asString();
370 if (p.check(
"systemtime")) {
371 body.
systemtime = p.find(
"systemtime").asFloat64();
376 if (p.check(
"networktime")) {
377 body.
networktime = p.find(
"networktime").asFloat64();
383 if (p.check(
"externaltime")) {
389 if (p.check(
"backtrace")) {
390 body.
backtrace = p.find(
"backtrace").asString();
401 size_t str = s.find(
'[',0);
402 size_t end = s.find(
']',0);
403 if (str==std::string::npos || end==std::string::npos )
409 std::string level = s.substr(str,end+1);
417 body.
text = s.substr(end+1);
438 std::istringstream iss(header);
440 getline(iss, token,
'/');
446 if (entry.
logInfo.
port_system ==
"yarprunlog" && listen_to_YARPRUN_MESSAGES==
false)
continue;
448 std::list<LogEntry>::iterator it;
449 for (it = log_list.begin(); it != log_list.end(); it++)
453 if (it->logging_enabled)
455 it->logInfo.setNewError(body.
level);
456 it->logInfo.last_update=machine_current_time;
457 it->append_logEntry(body);
466 if (it == log_list.end())
468 if (log_list.size() < log_list_max_size || log_list_max_size_enabled==
false )
482 log_list.push_back(entry);
490 this->mutex.unlock();
501 fprintf(stderr,
"logger is already running, listening on port %s\n", log_updater->getPortName().c_str());
507 fprintf(stderr,
"Unable to start logger: port %s is unavailable because another instance of the logger is already running (or address conflict)\n", log_updater->getPortName().c_str());
511 if (log_updater->logger_port.open(log_updater->getPortName()))
513 fprintf(stdout,
"Logger successfully started, listening on port %s\n", log_updater->getPortName().c_str());
517 fprintf(stderr,
"Unable to start logger: port %s is unavailable\n", log_updater->getPortName().c_str());
520 log_updater->logger_port.resume();
521 log_updater->logger_port.setStrict();
523 log_updater->start();
527 void LoggerEngine::logger_thread::threadRelease()
529 logger_port.interrupt();
535 if (log_updater ==
nullptr)
return false;
538 if (log_updater->isRunning()==
true) log_updater->stop();
544 if (log_updater ==
nullptr)
return;
546 log_updater->start();
557 log_updater=
new logger_thread (portName, 0.01);
565 if (log_updater!=
nullptr)
568 log_updater =
nullptr;
574 if (log_updater ==
nullptr)
return 0;
576 return log_updater->logger_port.getInputCount();
581 if (log_updater ==
nullptr)
return;
583 log_updater->mutex.lock();
584 std::list<LogEntry>::iterator it;
585 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
587 infos.push_back(it->logInfo);
589 log_updater->mutex.unlock();
594 if (log_updater ==
nullptr)
return;
596 log_updater->mutex.lock();
597 std::list<LogEntry>::iterator it;
598 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
600 messages.insert(messages.end(), it->entry_list.begin(), it->entry_list.end());
602 log_updater->mutex.unlock();
607 if (log_updater ==
nullptr)
return;
609 log_updater->mutex.lock();
610 std::list<LogEntry>::iterator it;
611 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
613 if (it->logInfo.port_prefix == port)
616 if (it->last_read_message==-1)
620 if (from_beginning==
true)
622 it->last_read_message = 0;
624 int i=it->last_read_message;
625 for (; i<(int)it->entry_list.size(); i++)
627 messages.push_back(it->entry_list[i]);
629 it->last_read_message=i;
633 log_updater->mutex.unlock();
638 if (log_updater ==
nullptr)
return;
640 log_updater->mutex.lock();
641 std::list<LogEntry>::iterator it;
642 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
644 if (it->logInfo.port_complete == port)
646 it->clear_logEntries();
650 log_updater->mutex.unlock();
655 if (log_updater ==
nullptr)
return;
657 log_updater->mutex.lock();
658 std::list<LogEntry>::iterator it;
659 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
661 if (it->logInfo.port_complete == port)
664 if (it->last_read_message==-1)
668 if (from_beginning==
true)
670 it->last_read_message = 0;
672 int i=it->last_read_message;
673 int size = (int)it->entry_list.size();
676 messages.push_back(it->entry_list[i]);
678 it->last_read_message=i;
682 log_updater->mutex.unlock();
687 if (log_updater ==
nullptr)
return;
689 log_updater->mutex.lock();
690 std::list<LogEntry>::iterator it;
691 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
693 if (it->logInfo.process_name == process)
696 if (it->last_read_message==-1)
700 if (from_beginning==
true)
702 it->last_read_message = 0;
704 int i=it->last_read_message;
705 for (; i<(int)it->entry_list.size(); i++)
707 messages.push_back(it->entry_list[i]);
709 it->last_read_message=i;
713 log_updater->mutex.unlock();
718 if (log_updater ==
nullptr)
return;
720 log_updater->mutex.lock();
721 std::list<LogEntry>::iterator it;
722 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
724 if (it->logInfo.process_pid == pid)
727 if (it->last_read_message==-1)
731 if (from_beginning==
true)
733 it->last_read_message = 0;
735 int i=it->last_read_message;
736 for (; i<(int)it->entry_list.size(); i++)
738 messages.push_back(it->entry_list[i]);
740 it->last_read_message=i;
744 log_updater->mutex.unlock();
747 const std::list<MessageEntry>
filter_by_level (
int level,
const std::list<MessageEntry>& messages)
749 std::list<MessageEntry>
ret;
750 std::list<MessageEntry>::const_iterator it;
751 for (it = messages.begin(); it != messages.end(); it++)
754 if (llevel.
toInt() == level)
762 if (log_updater ==
nullptr)
return;
763 log_updater->mutex.lock();
764 if (logLevel ==
LOGLEVEL_UNDEFINED) {log_updater->listen_to_LOGLEVEL_UNDEFINED=enable;}
765 else if (logLevel ==
LOGLEVEL_TRACE) {log_updater->listen_to_LOGLEVEL_TRACE=enable;}
766 else if (logLevel ==
LOGLEVEL_INFO) {log_updater->listen_to_LOGLEVEL_INFO=enable;}
767 else if (logLevel ==
LOGLEVEL_DEBUG) {log_updater->listen_to_LOGLEVEL_DEBUG=enable;}
768 else if (logLevel ==
LOGLEVEL_WARNING) {log_updater->listen_to_LOGLEVEL_WARNING=enable;}
769 else if (logLevel ==
LOGLEVEL_ERROR) {log_updater->listen_to_LOGLEVEL_ERROR=enable;}
770 else if (logLevel ==
LOGLEVEL_FATAL) {log_updater->listen_to_LOGLEVEL_FATAL=enable;}
771 log_updater->mutex.unlock();
776 if (log_updater ==
nullptr)
return false;
777 if (logLevel ==
LOGLEVEL_UNDEFINED) {
return log_updater->listen_to_LOGLEVEL_UNDEFINED;}
778 if (logLevel ==
LOGLEVEL_TRACE) {
return log_updater->listen_to_LOGLEVEL_TRACE;}
779 if (logLevel ==
LOGLEVEL_DEBUG) {
return log_updater->listen_to_LOGLEVEL_DEBUG;}
780 if (logLevel ==
LOGLEVEL_INFO) {
return log_updater->listen_to_LOGLEVEL_INFO;}
781 if (logLevel ==
LOGLEVEL_WARNING) {
return log_updater->listen_to_LOGLEVEL_WARNING;}
782 if (logLevel ==
LOGLEVEL_ERROR) {
return log_updater->listen_to_LOGLEVEL_ERROR;}
783 if (logLevel ==
LOGLEVEL_FATAL) {
return log_updater->listen_to_LOGLEVEL_FATAL;}
789 if (log_updater ==
nullptr)
return;
790 log_updater->mutex.lock();
791 log_updater->mutex.unlock();
801 if (log_updater ==
nullptr)
return;
802 log_updater->mutex.lock();
803 if (logSystem ==
LOGSYSTEM_YARP) {log_updater->listen_to_YARP_MESSAGES=enable;}
804 else if (logSystem ==
LOGSYSTEM_YARPRUN) {log_updater->listen_to_YARPRUN_MESSAGES=enable;}
805 log_updater->mutex.unlock();
810 if (log_updater ==
nullptr)
return false;
811 if (logSystem ==
LOGSYSTEM_YARP) {
return log_updater->listen_to_YARP_MESSAGES;}
812 if (logSystem ==
LOGSYSTEM_YARPRUN) {
return log_updater->listen_to_YARPRUN_MESSAGES;}
818 if (log_updater ==
nullptr)
return false;
819 if (filename.size() == 0)
return false;
821 log_updater->mutex.lock();
822 std::list<LogEntry>::iterator it;
823 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
825 if (it->logInfo.port_complete == portname)
828 file1.open(filename.c_str());
829 if (file1.is_open() ==
false) {log_updater->mutex.unlock();
return false;}
830 std::vector<MessageEntry>::iterator it1;
831 for (it1 = it->entry_list.begin(); it1 != it->entry_list.end(); it1++)
833 file1 << it1->yarprun_timestamp <<
" " << it1->local_timestamp <<
" " << it1->level.toString() <<
" " << it1->text <<
" " << std::endl;
838 log_updater->mutex.unlock();
844 string start_string =
"<#STRING_START#>";
845 string end_string =
"<#STRING_END#>";
847 if (log_updater ==
nullptr)
return false;
848 if (filename.size() == 0)
return false;
850 const int LOGFILE_VERSION = 1;
853 file1.open(filename.c_str());
854 if (file1.is_open() ==
false)
return false;
856 bool wasRunning = log_updater->isRunning();
857 if (wasRunning) log_updater->stop();
858 std::list<LogEntry>::iterator it;
859 file1 << LOGFILE_VERSION << std::endl;
860 file1 << log_updater->log_list.size() << std::endl;
861 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
863 file1 << it->logInfo.ip_address << std::endl;
864 file1 << it->logInfo.port_complete << std::endl;
865 file1 << it->logInfo.port_prefix << std::endl;
866 file1 << it->logInfo.port_system << std::endl;
867 file1 << it->logInfo.process_name << std::endl;
868 file1 << it->logInfo.process_pid << std::endl;
869 file1 << it->logInfo.get_number_of_traces() << std::endl;
870 file1 << it->logInfo.get_number_of_debugs() << std::endl;
871 file1 << it->logInfo.get_number_of_infos() << std::endl;
872 file1 << it->logInfo.get_number_of_warnings() << std::endl;
873 file1 << it->logInfo.get_number_of_errors() << std::endl;
874 file1 << it->logInfo.get_number_of_fatals() << std::endl;
875 file1 << it->logInfo.logsize << std::endl;
876 file1 << it->entry_list.size() << std::endl;
877 std::vector<MessageEntry>::iterator it1;
878 for (it1 = it->entry_list.begin(); it1 != it->entry_list.end(); it1++)
880 file1 << it1->yarprun_timestamp << std::endl;
881 file1 << it1->local_timestamp << std::endl;
882 file1 << it1->level.toInt() << std::endl;
883 file1 << start_string;
884 for (
char s : it1->text) file1.put(s);
885 file1 << end_string <<endl;
889 if (wasRunning) log_updater->start();
893 std::streamoff
get_tag(ifstream& file,
const char* tag)
895 std::streamoff pos=file.tellg();
896 int tag_size=strlen(tag);
897 char* buff =
new char[tag_size+2];
898 for(
int i=0; i<tag_size+2;i++) buff[i]=0;
899 std::streamoff off=0;
903 file.read(buff,tag_size);
904 if (file.good()==
false)
909 if (strcmp(buff,tag)==0)
break;
917 string start_string =
"<#STRING_START#>";
918 int start_string_size=strlen(start_string.c_str());
919 string end_string =
"<#STRING_END#>";
920 int end_string_size=strlen(end_string.c_str());
922 if (log_updater ==
nullptr)
return false;
923 if (filename.size() == 0)
return false;
925 const int LOGFILE_VERSION = 1;
928 file1.open(filename.c_str(),std::ifstream::binary);
929 if (file1.is_open() ==
false)
return false;
931 int log_file_version;
932 bool wasRunning = log_updater->isRunning();
933 if (wasRunning) log_updater->stop();
934 file1 >> log_file_version;
935 if (log_file_version == LOGFILE_VERSION)
938 file1 >> size_log_list;
939 log_updater->log_list.clear();
940 for (
int i=0; i< size_log_list; i++)
958 file1 >> size_entry_list;
959 for (
int j=0; j< size_entry_list; j++)
967 std::streamoff start_p =
get_tag(file1, start_string.c_str());
968 std::streamoff end_p =
get_tag(file1, end_string.c_str());
970 if (start_p<0 || end_p<0 || end_p-start_p+start_string_size<=0)
return false;
971 char *buff =
new char[(
unsigned int)(end_p-start_p+start_string_size)];
973 file1.seekg(start_p+start_string_size);
974 if (end_p-start_p-start_string_size!=1)
976 file1.get(buff,end_p-start_p-start_string_size);
982 file1.seekg(end_p+end_string_size);
987 log_updater->log_list.push_back(l_tmp);
991 if (wasRunning) log_updater->start();
997 if (log_updater ==
nullptr)
return;
998 log_updater->mutex.lock();
1000 std::list<LogEntry>::iterator it;
1001 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
1003 it->setLogEntryMaxSize(new_size);
1004 it->setLogEntryMaxSizeEnabled(enabled);
1007 log_updater->mutex.unlock();
1013 if (log_updater ==
nullptr)
return;
1014 log_updater->mutex.lock();
1015 log_updater->log_list_max_size_enabled = enabled;
1016 log_updater->log_list_max_size = new_size;
1017 log_updater->mutex.unlock();
1022 if (log_updater ==
nullptr)
return;
1023 if (log_updater->log_list.empty() ==
true)
return;
1024 log_updater->mutex.lock();
1025 auto it = log_updater->log_list.begin();
1026 current_size = it->getLogEntryMaxSize();
1027 enabled = it->getLogEntryMaxSizeEnabled();
1028 log_updater->mutex.unlock();
1033 if (log_updater ==
nullptr)
return;
1034 log_updater->mutex.lock();
1035 enabled=log_updater->log_list_max_size_enabled;
1036 current_size=log_updater->log_list_max_size;
1037 log_updater->mutex.unlock();
1042 if (log_updater ==
nullptr)
return false;
1043 log_updater->mutex.lock();
1044 log_updater->log_list.clear();
1045 log_updater->mutex.unlock();
1051 if (log_updater ==
nullptr)
return;
1053 log_updater->mutex.lock();
1054 std::list<LogEntry>::iterator it;
1055 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
1057 if (it->logInfo.port_complete == port)
1059 it->logging_enabled=enable;
1063 log_updater->mutex.unlock();
1068 if (log_updater ==
nullptr)
return false;
1071 log_updater->mutex.lock();
1072 std::list<LogEntry>::iterator it;
1073 for (it = log_updater->log_list.begin(); it != log_updater->log_list.end(); it++)
1075 if (it->logInfo.port_complete == port)
1077 enabled=it->logging_enabled;
1081 log_updater->mutex.unlock();