Skip to content

Commit

Permalink
Updated write_log() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
samyarsadat committed Jun 25, 2024
1 parent 3875820 commit 1718b71
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ void uRosBridgeAgent::execute()
switch (current_uros_state)
{
case WAITING_FOR_AGENT:
write_log(__FUNCTION__, "Waiting for agent...", LOG_LVL_INFO);
write_log("Waiting for agent...", LOG_LVL_INFO, FUNCNAME_ONLY);
current_uros_state = ping_agent() ? AGENT_AVAILABLE:WAITING_FOR_AGENT;
break;

case AGENT_AVAILABLE:
write_log(__FUNCTION__, "Agent available!", LOG_LVL_INFO);
write_log("Agent available!", LOG_LVL_INFO, FUNCNAME_ONLY);
check_bool(init_func(), RT_HARD_CHECK);
current_uros_state = AGENT_CONNECTED;
last_exec_time = time_us_32() / 1000;
Expand All @@ -304,7 +304,7 @@ void uRosBridgeAgent::execute()
break;

case AGENT_DISCONNECTED:
write_log(__FUNCTION__, "Agent disconnected!", LOG_LVL_INFO);
write_log("Agent disconnected!", LOG_LVL_INFO, FUNCNAME_ONLY);
// TODO: Maybe wait for the agent to connect again?
uros_fini();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// ------- Enums -------
enum RT_CHECK_MODE {RT_HARD_CHECK, RT_SOFT_CHECK, RT_LOG_ONLY_CHECK};
enum LOG_LEVEL {LOG_LVL_INFO, LOG_LVL_WARN, LOG_LVL_ERROR, LOG_LVL_FATAL};
enum LOG_SOURCE_VERBOSITY {FUNCNAME_ONLY, FILENAME_LINE_ONLY, FILENAME_LINE_FUNCNAME};



Expand All @@ -57,7 +58,7 @@ bool check_bool(bool function, RT_CHECK_MODE mode);
void publish_diag_report(uint8_t level, std::string hw_name, std::string hw_id, std::string msg, std::vector<diagnostic_msgs__msg__KeyValue> key_values);

// ---- Logging functions ----
void write_log(const char *src_function, std::string msg, LOG_LEVEL lvl);
void write_log(std::string msg, LOG_LEVEL lvl, LOG_SOURCE_VERBOSITY src_verb);

// ---- Pings the MicroROS agent ----
bool ping_agent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void publish_diag_report(uint8_t level, std::string hw_name, std::string hw_id,


// ---- Logging function (outputs to STDIO UART) ----
void write_log(const char *src_function, std::string msg, LOG_LEVEL lvl)
void write_log(std::string msg, LOG_LEVEL lvl, LOG_SOURCE_VERBOSITY src_verb, const char *func=__builtin_FUNCTION(), const char *file=__builtin_FILE(), uint16_t line=__builtin_LINE())
{
uint32_t timestamp_sec = to_ms_since_boot(get_absolute_time()) / 1000;
uint16_t timestamp_millisec = to_ms_since_boot(get_absolute_time()) - (timestamp_sec * 1000);
Expand All @@ -167,8 +167,17 @@ void write_log(const char *src_function, std::string msg, LOG_LEVEL lvl)
else if (lvl == LOG_LVL_ERROR) { level = "ERROR"; }
else if (lvl == LOG_LVL_FATAL) { level = "FATAL"; }

// This is rather unfortunate, but its the easiest way.
std::string filename(file);
std::string funcname(func);

std::string src;
if (src_verb == FUNCNAME_ONLY) { src = func; }
else if (src_verb == FILENAME_LINE_ONLY) { src = filename + ":" + std::to_string(line); }
else if (src_verb == FILENAME_LINE_FUNCNAME) { src = funcname + "@" + filename + ":" + std::to_string(line); }

// This is quite ugly, but it works.
msg = "[" + std::to_string(timestamp_sec) + "." + std::to_string(timestamp_millisec) + "] [" + level + "] [" + src_function + "]: " + msg + "\r\n";
msg = "[" + std::to_string(timestamp_sec) + "." + std::to_string(timestamp_millisec) + "] [" + level + "] [" + src + "]: " + msg + "\r\n";
stdio_uart.out_chars(msg.c_str(), msg.length());
}

Expand Down

0 comments on commit 1718b71

Please sign in to comment.