From 0c4de20e3e1d58241f69f109e05e47d079a1328e Mon Sep 17 00:00:00 2001 From: Rasmus Skovgaard Andersen Date: Tue, 3 Sep 2024 12:04:03 +0200 Subject: [PATCH] Add: Added support for log files of the format "Example: 1724314618.146484723 [INFO] [namespace.Node::Fuction]: The actual log msg". Refactor: Removed nested if's --- src/rosout_log_loader.cpp | 128 +++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/src/rosout_log_loader.cpp b/src/rosout_log_loader.cpp index 8bdbed6..dae7d51 100644 --- a/src/rosout_log_loader.cpp +++ b/src/rosout_log_loader.cpp @@ -102,72 +102,72 @@ namespace swri_console log->level = level_string_to_level_type(std::string(level)); log->line = line_num; log->msg = msg; + return 0; + } + + // try another format + char log_msg_fmt1[] = "%d.%d %s [%[^:]:%u(%[^)])) [topics: %[^]]] %[^\n]s"; + num_parsed = sscanf(line.c_str(), log_msg_fmt1, &secs, &nsecs, level, file, &line_num, function, topics, msg); + if (num_parsed == 8) { + // Populate new log message + log->file = file; + log->function = function; + // log->header.seq = seq; + // stamp.sec = secs; + // stamp.nsec = nsecs; + stamp = rclcpp::Time(secs, nsecs); + log->stamp = stamp; + log->level = level_string_to_level_type(std::string(level)); + log->line = line_num; + log->msg = msg; + return 0; + } + + // try another format + // Example: [rospy.registration][INFO] 2017-11-30 08:11:39,231: registering subscriber topic [/tf] type [tf2_msgs/TFMessage] with master + char log_msg_fmt2[] = "[%[^]]][%[^]]] %d-%d-%d %d:%d:%d,%d: %[^\n]s"; + int year; + int month; + int day; + int hour; + int minute; + int msecs; + char name[1024]; + time_t rawtime; + struct tm * timeinfo; + + num_parsed = sscanf(line.c_str(), log_msg_fmt2, name, level, &year, &month, &day, &hour, &minute, &secs, &msecs, msg); + if (num_parsed == 10) { + // Populate new log message + file[0] = 0; + function[0] = 0; + line_num = 0; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + timeinfo->tm_year = year - 1900; + timeinfo->tm_mon = month - 1; + timeinfo->tm_mday = day; + timeinfo->tm_hour = hour; + timeinfo->tm_min = minute; + timeinfo->tm_sec = secs; + rawtime = mktime ( timeinfo ); + secs = rawtime; + nsecs = msecs * 1000000; + log->file = file; + log->function = function; + // log->header.seq = seq; + // stamp.sec = secs; + // stamp.nsec = nsecs; + stamp = rclcpp::Time(secs, nsecs); + log->stamp = stamp; + log->level = level_string_to_level_type(std::string(level)); + log->line = line_num; + log->msg = msg; + return 0; } - else // try another format - { - char log_msg_fmt1[] = "%d.%d %s [%[^:]:%u(%[^)])) [topics: %[^]]] %[^\n]s"; - num_parsed = sscanf(line.c_str(), log_msg_fmt1, &secs, &nsecs, level, file, &line_num, function, topics, msg); - if (num_parsed == 8 ) - { - // Populate new log message - log->file = file; - log->function = function; - // log->header.seq = seq; - // stamp.sec = secs; - // stamp.nsec = nsecs; - stamp = rclcpp::Time(secs, nsecs); - log->stamp = stamp; - log->level = level_string_to_level_type(std::string(level)); - log->line = line_num; - log->msg = msg; - } - else // try another format - { - // Example: [rospy.registration][INFO] 2017-11-30 08:11:39,231: registering subscriber topic [/tf] type [tf2_msgs/TFMessage] with master - char log_msg_fmt2[] = "[%[^]]][%[^]]] %d-%d-%d %d:%d:%d,%d: %[^\n]s"; - int year; - int month; - int day; - int hour; - int minute; - int msecs; - char name[1024]; - time_t rawtime; - struct tm * timeinfo; - num_parsed = sscanf(line.c_str(), log_msg_fmt2, name, level, &year, &month, &day, &hour, &minute, &secs, &msecs, msg); - if (num_parsed == 10) - { - // Populate new log message - file[0] = 0; - function[0] = 0; - line_num = 0; - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - timeinfo->tm_year = year - 1900; - timeinfo->tm_mon = month - 1; - timeinfo->tm_mday = day; - timeinfo->tm_hour = hour; - timeinfo->tm_min = minute; - timeinfo->tm_sec = secs; - rawtime = mktime ( timeinfo ); - secs = rawtime; - nsecs = msecs * 1000000; - log->file = file; - log->function = function; - // log->header.seq = seq; - // stamp.sec = secs; - // stamp.nsec = nsecs; - stamp = rclcpp::Time(secs, nsecs); - log->stamp = stamp; - log->level = level_string_to_level_type(std::string(level)); - log->line = line_num; - log->msg = msg; - } - else - { - // Example: [ INFO] [1512051098.518631473]: Read parameter lower_cost_threshold = 0.000000 - char log_msg_fmt3[] = "\x1b[%dm[ %[^]]] [%d.%d]: %[^\n\x1b]s"; + // Example: [ INFO] [1512051098.518631473]: Read parameter lower_cost_threshold = 0.000000 + char log_msg_fmt3[] = "\x1b[%dm[ %[^]]] [%d.%d]: %[^\n\x1b]s"; // char log_msg_fmt3[] = "\x1b[%dm[ %[^]]] [%d.%d]]%[^\n]"; int ansi_color; msg[0] = 0;