Skip to content

Commit

Permalink
Add: Added support for log files of the format "Example: 1724314618.1…
Browse files Browse the repository at this point in the history
…46484723 [INFO] [namespace.Node::Fuction]: The actual log msg".

Refactor: Removed nested if's
  • Loading branch information
rasmusan committed Sep 3, 2024
1 parent a255574 commit da2507c
Showing 1 changed file with 75 additions and 66 deletions.
141 changes: 75 additions & 66 deletions src/rosout_log_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,73 +169,82 @@ namespace swri_console
// 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;
num_parsed = sscanf(line.c_str(), log_msg_fmt3, &ansi_color, level, &secs, &nsecs, msg);
if (num_parsed == 5)
{
// Populate new log message
file[0] = 0;
function[0] = 0;
line_num = 0;
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: [ WARN] [1512051107.153917534, 1507066358.521849475]: Offset change exceeds limit! reduce from 0.814476 to 0.500000
char log_msg_fmt4[] = "\x1b[%dm[ %[^]]] [%d.%d, %d.%d]: %[^\n\x1b]";
int secs2;
int nsecs2;
msg[0] = 0;
num_parsed = sscanf(line.c_str(), log_msg_fmt4, &ansi_color, level, &secs, &nsecs, &secs2, &nsecs2, msg);
if (num_parsed == 7)
{
// Populate new log message
file[0] = 0;
function[0] = 0;
line_num = 0;
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 // Couldn't parse with known formats
{
if (line.length() < MIN_MSG_SIZE)
{
return -1;
}
log->file = std::string("");
log->function = std::string("");
// log->header.seq = seq;
// stamp.sec = 0;
// stamp.nsec = 0;
stamp = rclcpp::Time(0, 0);
log->stamp = stamp;
log->level = level_string_to_level_type(std::string("DEBUG"));
log->line = 0;
log->msg = line;
log->name = log->name + "-unparsed";
}
}
}
}
int ansi_color;
msg[0] = 0;
num_parsed = sscanf(line.c_str(), log_msg_fmt3, &ansi_color, level, &secs, &nsecs, msg);
if (num_parsed == 5) {
// Populate new log message
file[0] = 0;
function[0] = 0;
line_num = 0;
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;
}

// Example: [ WARN] [1512051107.153917534, 1507066358.521849475]: Offset change exceeds limit! reduce from 0.814476 to 0.500000
char log_msg_fmt4[] = "\x1b[%dm[ %[^]]] [%d.%d, %d.%d]: %[^\n\x1b]";
int secs2;
int nsecs2;
msg[0] = 0;
num_parsed = sscanf(line.c_str(), log_msg_fmt4, &ansi_color, level, &secs, &nsecs, &secs2, &nsecs2, msg);
if (num_parsed == 7) {
// Populate new log message
file[0] = 0;
function[0] = 0;
line_num = 0;
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;
}

// Example: 1724314618.146484723 [INFO] [namespace.Node::Fuction]: The actual log msg
char log_msg_fmt5[] = "%d.%d [%[^]]] [%49[^:]::%127[^]]]: %[^\n\x1b]";
num_parsed = sscanf(line.c_str(), log_msg_fmt5, &secs, &nsecs, level, name, function, msg);

if (num_parsed == 6) {
// Populate new log message
log->name = name; // override 'name' with node_fqn
log->function = function;
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;
}

// Couldn't parse with known formats
if (line.length() < MIN_MSG_SIZE) {
return -1;
}
log->file = std::string("");
log->function = std::string("");
// log->header.seq = seq;
// stamp.sec = 0;
// stamp.nsec = 0;
stamp = rclcpp::Time(0, 0);
log->stamp = stamp;
log->level = level_string_to_level_type(std::string("DEBUG"));
log->line = 0;
log->msg = line;
log->name = log->name + "-unparsed";
return 0;
}

Expand Down

0 comments on commit da2507c

Please sign in to comment.