Skip to content

Commit

Permalink
Rely on rcutils_logging_console_output_handler
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Jul 1, 2024
1 parent 85ab442 commit c20ee54
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 85 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ jobs:
- name: uncrustify
run: /ros_entrypoint.sh ament_uncrustify rmw_zenoh_cpp/
- name: cpplint
# Ignore logging.hpp since the fmt::sprintf gets incorrectly flagged by ament_cpplint
# as a runtime/printf error.
run: /ros_entrypoint.sh ament_cpplint rmw_zenoh_cpp/ --exclude rmw_zenoh_cpp/src/detail/logging.hpp
run: /ros_entrypoint.sh ament_cpplint rmw_zenoh_cpp/
4 changes: 1 addition & 3 deletions rmw_zenoh_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ find_package(ament_cmake REQUIRED)

find_package(ament_index_cpp REQUIRED)
find_package(fastcdr CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosidl_typesupport_fastrtps_c REQUIRED)
Expand Down Expand Up @@ -61,7 +60,6 @@ target_link_libraries(rmw_zenoh_cpp
PRIVATE
ament_index_cpp::ament_index_cpp
fastcdr
fmt::fmt
rcpputils::rcpputils
rcutils::rcutils
rosidl_typesupport_fastrtps_c::rosidl_typesupport_fastrtps_c
Expand Down Expand Up @@ -114,7 +112,7 @@ add_executable(rmw_zenohd
target_link_libraries(rmw_zenohd
PRIVATE
ament_index_cpp::ament_index_cpp
fmt::fmt
rcutils::rcutils
rcpputils::rcpputils
rmw::rmw
zenohc::lib
Expand Down
1 change: 0 additions & 1 deletion rmw_zenoh_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

<depend>ament_index_cpp</depend>
<depend>fastcdr</depend>
<depend>fmt</depend>
<depend>rcpputils</depend>
<depend>rcutils</depend>
<depend>rosidl_typesupport_fastrtps_c</depend>
Expand Down
40 changes: 3 additions & 37 deletions rmw_zenoh_cpp/src/detail/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,17 @@ namespace rmw_zenoh_cpp
///=============================================================================
Logger & Logger::get()
{
static Logger logger(RMW_ZENOH_LOG_LEVEL_INFO);
static Logger logger(RCUTILS_LOG_SEVERITY_INFO);
return logger;
}

///=============================================================================
Logger::Logger(LogLevel threshold_level)
Logger::Logger(RCUTILS_LOG_SEVERITY threshold_level)
: threshold_level_(threshold_level)
{}

///=============================================================================
fmt::color Logger::level_to_color(LogLevel level) const
{
switch (level) {
case RMW_ZENOH_LOG_LEVEL_DEBUG:
return fmt::color::light_green;
case RMW_ZENOH_LOG_LEVEL_ERROR:
return fmt::color::indian_red;
case RMW_ZENOH_LOG_LEVEL_INFO:
return fmt::color::light_blue;
case RMW_ZENOH_LOG_LEVEL_WARN:
return fmt::color::light_yellow;
default:
return fmt::color::white;
}
}

///=============================================================================
std::string Logger::level_to_string(LogLevel level) const
{
switch (level) {
case RMW_ZENOH_LOG_LEVEL_DEBUG:
return "DEBUG";
case RMW_ZENOH_LOG_LEVEL_ERROR:
return "ERROR";
case RMW_ZENOH_LOG_LEVEL_INFO:
return "INFO";
case RMW_ZENOH_LOG_LEVEL_WARN:
return "WARN";
default:
return "UNKOWN";
}
}

///=============================================================================
void Logger::set_log_level(LogLevel new_level)
void Logger::set_log_level(RCUTILS_LOG_SEVERITY new_level)
{
threshold_level_ = new_level;
}
Expand Down
59 changes: 23 additions & 36 deletions rmw_zenoh_cpp/src/detail/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#ifndef DETAIL__LOGGING_HPP_
#define DETAIL__LOGGING_HPP_

#include <fmt/chrono.h>
#include <fmt/color.h>
#include <fmt/printf.h>
#include <rcutils/logging.h>

#include <iostream>
#include <mutex>
Expand All @@ -26,15 +24,6 @@

namespace rmw_zenoh_cpp
{
///=============================================================================
enum LogLevel
{
RMW_ZENOH_LOG_LEVEL_DEBUG,
RMW_ZENOH_LOG_LEVEL_INFO,
RMW_ZENOH_LOG_LEVEL_WARN,
RMW_ZENOH_LOG_LEVEL_ERROR,
};

///=============================================================================
class Logger
{
Expand All @@ -43,42 +32,40 @@ class Logger
static Logger & get();

// Set the threshold log level.
void set_log_level(LogLevel new_level);
void set_log_level(RCUTILS_LOG_SEVERITY new_level);

// Log to the console.
template<typename ... Args>
void log_named(
LogLevel level,
const std::string & name,
const std::string & message,
const Args &... args) const
RCUTILS_LOG_SEVERITY level,
const char * name,
const char * message,
...) const
{
if (level >= threshold_level_) {
const auto time_now = std::chrono::system_clock::now().time_since_epoch();
const auto seconds =
std::chrono::duration_cast<std::chrono::seconds>(time_now).count();
auto nanoseconds =
std::chrono::duration_cast<std::chrono::nanoseconds>(
time_now - std::chrono::seconds(seconds)).count();
fmt::print(
fmt::fg(level_to_color(level)),
"[{}] [{}.{}] [{}]: {}\n",
level_to_string(level),
seconds,
nanoseconds,
rcutils_time_point_value_t now;
rcutils_ret_t ret = rcutils_system_time_now(&now);
if (ret != RCUTILS_RET_OK) {
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to get timestamp while doing a console logging.\n");
return;
}
static rcutils_log_location_t log_location = {__func__, __FILE__, __LINE__};
va_list args;
va_start(args, message);
rcutils_logging_console_output_handler(
&log_location,
level,
name,
fmt::sprintf(message, args ...)
now,
message,
&args
);
}
}

private:
LogLevel threshold_level_;
explicit Logger(LogLevel threshold_level);
fmt::color level_to_color(LogLevel level) const;
std::string level_to_string(LogLevel level) const;
RCUTILS_LOG_SEVERITY threshold_level_;
explicit Logger(RCUTILS_LOG_SEVERITY threshold_level);
};

} // namespace rmw_zenoh_cpp

#endif // DETAIL__LOGGING_HPP_
8 changes: 4 additions & 4 deletions rmw_zenoh_cpp/src/detail/logging_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
// Note: Consider adding a set_rmw_log_level() API to rmw which can override the
// default log level at the RMW layer.
#define RMW_ZENOH_LOG_DEBUG_NAMED(...) {rmw_zenoh_cpp::Logger::get().log_named( \
rmw_zenoh_cpp::RMW_ZENOH_LOG_LEVEL_DEBUG, __VA_ARGS__);}
RCUTILS_LOG_SEVERITY_DEBUG, __VA_ARGS__);}
#define RMW_ZENOH_LOG_ERROR_NAMED(...) {rmw_zenoh_cpp::Logger::get().log_named( \
rmw_zenoh_cpp::RMW_ZENOH_LOG_LEVEL_ERROR, __VA_ARGS__);}
RCUTILS_LOG_SEVERITY_ERROR, __VA_ARGS__);}
#define RMW_ZENOH_LOG_INFO_NAMED(...) {rmw_zenoh_cpp::Logger::get().log_named( \
rmw_zenoh_cpp::RMW_ZENOH_LOG_LEVEL_INFO, __VA_ARGS__);}
RCUTILS_LOG_SEVERITY_INFO, __VA_ARGS__);}
#define RMW_ZENOH_LOG_WARN_NAMED(...) {rmw_zenoh_cpp::Logger::get().log_named( \
rmw_zenoh_cpp::RMW_ZENOH_LOG_LEVEL_WARN, __VA_ARGS__);}
RCUTILS_LOG_SEVERITY_WARN, __VA_ARGS__);}

#endif // DETAIL__LOGGING_MACROS_HPP_
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/detail/rmw_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ void client_data_handler(z_owned_reply_t * reply, void * data)
"z_reply_is_ok returned False for keyexpr %s. Reason: %.*s",
z_loan(keystr),
(int)err.payload.len,
fmt::ptr(err.payload.start));
err.payload.start);
z_drop(z_move(keystr));

return;
Expand Down

0 comments on commit c20ee54

Please sign in to comment.