38 entry_list_max_size = size;
45 entry_list_max_size_enabled=enable;
50 if (
logInfo.
logsize >= entry_list_max_size && entry_list_max_size_enabled)
98 if (level > highest_error) {
99 highest_error = level;
113 p.
write(cmd,response);
115 int size = response.
size();
116 for (
int i=1; i<size; i++)
119 if (
n1 &&
n1->get(0).toString()==
"port")
122 if (
n2 &&
n2->get(0).toString()==
"name")
126 log_off = std::strstr((
char*)(
n2->get(1).toString().c_str()),
"/log/");
129 std::string
logport =
n2->get(1).toString();
133 yarprun_log_off = std::strstr((
char*)(
n2->get(1).toString().c_str()),
"/yarprunlog/");
136 std::string
logport =
n2->get(1).toString();
144 std::list<std::string>::iterator
ports_it;
147 LogEntry entry (log_updater->logs_max_lines_enabled, log_updater->logs_max_lines);
148 entry.logInfo.port_complete = (*ports_it);
156 printf(
"ERROR: invalid contact: %s\n",
entry.logInfo.port_complete.c_str());
166 std::list<LogEntry>::iterator
it;
167 this->log_updater->mutex.lock();
169 for (
it = this->log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
171 if (
it->logInfo.port_complete==
entry.logInfo.port_complete)
178 log_updater->log_list.push_back(
entry);
180 this->log_updater->mutex.unlock();
190 std::list<std::string>::const_iterator
it;
205std::string LoggerEngine::logger_thread::getPortName()
207 return logger_portName;
215 log_list_max_size_enabled =
true;
217 logs_max_lines_enabled =
true;
218 unknown_format_received = 0;
221void LoggerEngine::logger_thread::run()
261 unknown_format_received++;
275 unknown_format_received++;
296 unknown_format_received++;
302 if (p.check(
"level")) {
303 body.
text = p.find(
"message").toString();
305 auto level = p.find(
"level").toString();
306 if (level ==
"TRACE") {
308 }
else if (level ==
"DEBUG") {
310 }
else if (level ==
"INFO") {
312 }
else if (level ==
"WARNING") {
314 }
else if (level ==
"ERROR") {
316 }
else if (level ==
"FATAL") {
322 if (p.check(
"filename")) {
323 body.
filename = p.find(
"filename").asString();
328 if (p.check(
"line")) {
329 body.
line =
static_cast<uint32_t>(p.find(
"line").asInt32());
334 if (p.check(
"function")) {
335 body.
function = p.find(
"function").asString();
340 if (p.check(
"hostname")) {
341 body.
hostname = p.find(
"hostname").asString();
346 if (p.check(
"pid")) {
347 body.
pid = p.find(
"pid").asInt32();
352 if (p.check(
"cmd")) {
353 body.
cmd = p.find(
"cmd").asString();
358 if (p.check(
"args")) {
359 body.
args = p.find(
"args").asString();
364 if (p.check(
"thread_id")) {
365 body.
thread_id = p.find(
"thread_id").asInt64();
370 if (p.check(
"component")) {
371 body.
component = p.find(
"component").asString();
377 body.
id = p.find(
"id").asString();
382 if (p.check(
"systemtime")) {
383 body.
systemtime = p.find(
"systemtime").asFloat64();
388 if (p.check(
"networktime")) {
389 body.
networktime = p.find(
"networktime").asFloat64();
395 if (p.check(
"externaltime")) {
401 if (p.check(
"backtrace")) {
402 body.
backtrace = p.find(
"backtrace").asString();
413 size_t str = s.find(
'[',0);
414 size_t end = s.find(
']',0);
415 if (str==std::string::npos || end==std::string::npos )
421 std::string level = s.substr(str,end+1);
423 if (level.find(
"TRACE") != std::string::npos) {
425 }
else if (level.find(
"DEBUG") != std::string::npos) {
427 }
else if (level.find(
"INFO") != std::string::npos) {
429 }
else if (level.find(
"WARNING") != std::string::npos) {
431 }
else if (level.find(
"ERROR") != std::string::npos) {
433 }
else if (level.find(
"FATAL") != std::string::npos) {
436 body.
text = s.substr(end+1);
452 LogEntry entry (this->logs_max_lines_enabled, this->logs_max_lines);
453 entry.logInfo.port_complete = header;
454 entry.logInfo.port_complete.erase(0,1);
455 entry.logInfo.port_complete.erase(
entry.logInfo.port_complete.size()-1);
456 std::istringstream
iss(header);
463 if (
entry.logInfo.port_system ==
"log" && listen_to_YARP_MESSAGES ==
false) {
466 if (
entry.logInfo.port_system ==
"yarprunlog" && listen_to_YARPRUN_MESSAGES ==
false) {
473 std::list<LogEntry>::iterator
it;
474 for (
it = log_list.begin();
it != log_list.end();
it++)
476 if (
it->logInfo.port_complete==
entry.logInfo.port_complete)
478 if (
it->logging_enabled)
480 it->logInfo.setNewError(body.
level);
482 it->append_logEntry(body);
491 if (
it == log_list.end())
493 if (log_list.size() < log_list_max_size || log_list_max_size_enabled==
false )
503 printf(
"ERROR: invalid contact: %s\n",
entry.logInfo.port_complete.c_str());
505 entry.append_logEntry(body);
507 log_list.push_back(
entry);
514 this->mutex.unlock();
526 fprintf(
stderr,
"logger is already running, listening on port %s\n", log_updater->getPortName().c_str());
532 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());
536 if (log_updater->logger_port.open(log_updater->getPortName()))
538 fprintf(
stdout,
"Logger successfully started, listening on port %s\n", log_updater->getPortName().c_str());
542 fprintf(
stderr,
"Unable to start logger: port %s is unavailable\n", log_updater->getPortName().c_str());
545 log_updater->logger_port.resume();
546 log_updater->logger_port.setStrict();
548 log_updater->start();
552void LoggerEngine::logger_thread::threadRelease()
554 logger_port.interrupt();
560 if (log_updater ==
nullptr) {
566 if (log_updater->isRunning() ==
true) {
576 if (log_updater ==
nullptr) {
582 log_updater->start();
594 log_updater=
new logger_thread (portName, 0.01);
602 if (log_updater!=
nullptr)
605 log_updater =
nullptr;
611 if (log_updater ==
nullptr) {
615 return log_updater->logger_port.getInputCount();
620 if (log_updater ==
nullptr) {
624 log_updater->mutex.lock();
625 std::list<LogEntry>::iterator
it;
626 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
630 log_updater->mutex.unlock();
635 if (log_updater ==
nullptr) {
639 log_updater->mutex.lock();
640 std::list<LogEntry>::iterator
it;
641 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
643 messages.insert(messages.end(),
it->entry_list.begin(),
it->entry_list.end());
645 log_updater->mutex.unlock();
650 if (log_updater ==
nullptr) {
654 log_updater->mutex.lock();
655 std::list<LogEntry>::iterator
it;
656 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
658 if (
it->logInfo.port_prefix == port)
661 if (
it->last_read_message==-1)
667 it->last_read_message = 0;
669 int i=
it->last_read_message;
670 for (; i<(
int)
it->entry_list.size(); i++)
672 messages.push_back(
it->entry_list[i]);
674 it->last_read_message=i;
678 log_updater->mutex.unlock();
683 if (log_updater ==
nullptr) {
687 log_updater->mutex.lock();
688 std::list<LogEntry>::iterator
it;
689 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
691 if (
it->logInfo.port_complete == port)
693 it->clear_logEntries();
697 log_updater->mutex.unlock();
702 if (log_updater ==
nullptr) {
706 log_updater->mutex.lock();
707 std::list<LogEntry>::iterator
it;
708 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
710 if (
it->logInfo.port_complete == port)
713 if (
it->last_read_message==-1)
719 it->last_read_message = 0;
721 int i=
it->last_read_message;
722 int size = (
int)
it->entry_list.size();
725 messages.push_back(
it->entry_list[i]);
727 it->last_read_message=i;
731 log_updater->mutex.unlock();
736 if (log_updater ==
nullptr) {
740 log_updater->mutex.lock();
741 std::list<LogEntry>::iterator
it;
742 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
744 if (
it->logInfo.process_name == process)
747 if (
it->last_read_message==-1)
753 it->last_read_message = 0;
755 int i=
it->last_read_message;
756 for (; i<(
int)
it->entry_list.size(); i++)
758 messages.push_back(
it->entry_list[i]);
760 it->last_read_message=i;
764 log_updater->mutex.unlock();
769 if (log_updater ==
nullptr) {
773 log_updater->mutex.lock();
774 std::list<LogEntry>::iterator
it;
775 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
777 if (
it->logInfo.process_pid == pid)
780 if (
it->last_read_message==-1)
786 it->last_read_message = 0;
788 int i=
it->last_read_message;
789 for (; i<(
int)
it->entry_list.size(); i++)
791 messages.push_back(
it->entry_list[i]);
793 it->last_read_message=i;
797 log_updater->mutex.unlock();
800const std::list<MessageEntry>
filter_by_level (
int level,
const std::list<MessageEntry>& messages)
802 std::list<MessageEntry>
ret;
803 std::list<MessageEntry>::const_iterator
it;
804 for (
it = messages.begin();
it != messages.end();
it++)
807 if (
llevel.toInt() == level) {
816 if (log_updater ==
nullptr) {
819 log_updater->mutex.lock();
827 log_updater->mutex.unlock();
832 if (log_updater ==
nullptr) {
847 if (log_updater ==
nullptr) {
850 log_updater->mutex.lock();
851 log_updater->mutex.unlock();
861 if (log_updater ==
nullptr) {
864 log_updater->mutex.lock();
867 log_updater->mutex.unlock();
872 if (log_updater ==
nullptr) {
882 if (log_updater ==
nullptr) {
886 log_updater->mutex.lock();
887 log_updater->logs_max_lines_enabled = enabled;
888 log_updater->logs_max_lines =
new_size;
890 std::list<LogEntry>::iterator
it;
891 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
894 it->setLogEntryMaxSizeEnabled(enabled);
897 log_updater->mutex.unlock();
903 if (log_updater ==
nullptr) {
906 log_updater->mutex.lock();
907 log_updater->log_list_max_size_enabled = enabled;
908 log_updater->log_list_max_size =
new_size;
909 log_updater->mutex.unlock();
914 if (log_updater ==
nullptr) {
918 log_updater->mutex.lock();
919 enabled = log_updater->logs_max_lines_enabled;
921 log_updater->mutex.unlock();
926 if (log_updater ==
nullptr) {
929 log_updater->mutex.lock();
930 enabled=log_updater->log_list_max_size_enabled;
932 log_updater->mutex.unlock();
937 if (log_updater ==
nullptr) {
940 log_updater->mutex.lock();
941 log_updater->log_list.clear();
942 log_updater->mutex.unlock();
948 if (log_updater ==
nullptr) {
952 log_updater->mutex.lock();
953 std::list<LogEntry>::iterator
it;
954 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
956 if (
it->logInfo.port_complete == port)
958 it->logging_enabled=enable;
962 log_updater->mutex.unlock();
967 if (log_updater ==
nullptr) {
972 log_updater->mutex.lock();
973 std::list<LogEntry>::iterator
it;
974 for (
it = log_updater->log_list.begin();
it != log_updater->log_list.end();
it++)
976 if (
it->logInfo.port_complete == port)
978 enabled=
it->logging_enabled;
982 log_updater->mutex.unlock();
const std::list< MessageEntry > filter_by_level(int level, const std::list< MessageEntry > &messages)
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
A mini-server for performing network communication in the background.
int getPendingReads() override
Get the number of objects ready to be read.
static std::string getNameServerName()
Get the name of the port associated with the nameserver (usually "/root", but this can be overwritten...
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
static Contact queryName(const std::string &name)
Find out information about a registered name.
static bool exists(const std::string &port, bool quiet=true, bool checkVer=true)
Check for a port to be ready and responsive.
An abstraction for a periodic thread.
A class for storing options and configuration information.
A port that is specialized as an RPC client.
static double nowSystem()
virtual bool isString() const
Checks if value is a string.
virtual Bottle * asList() const
Get list value.
virtual std::string asString() const
Get string value.
void setNewError(LogLevel level)
yarp::yarpLogger::LogEntryInfo logInfo
void setLogEntryMaxSizeEnabled(bool enable)
bool append_logEntry(MessageEntry entry)
void setLogEntryMaxSize(int size)
std::vector< MessageEntry > entry_list
void set_log_list_max_size(bool enabled, int new_size)
bool get_listen_option(LogLevel logLevel)
void connect(const std::list< std::string > &ports)
void get_log_lines_max_size(bool &enabled, int ¤t_size)
void get_messages(std::list< MessageEntry > &messages)
bool get_log_enable_by_port_complete(std::string port)
void get_messages_by_port_prefix(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
LoggerEngine(std::string portName)
void clear_messages_by_port_complete(std::string port)
void get_messages_by_port_complete(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
void set_log_lines_max_size(bool enabled, int new_size)
void get_messages_by_process(std::string process, std::list< MessageEntry > &messages, bool from_beginning=false)
void discover(std::list< std::string > &ports)
void get_log_list_max_size(bool &enabled, int ¤t_size)
int get_num_of_processes()
void get_infos(std::list< LogEntryInfo > &infos)
void set_log_enable_by_port_complete(std::string port, bool enable)
void set_listen_option(LogLevel logLevel, bool enable)
void get_messages_by_pid(std::string pid, std::list< MessageEntry > &messages, bool from_beginning=false)
An interface to the operating system, including Port based communication.
std::string yarprun_timestamp
std::string local_timestamp