|
| 1 | +#include "agnocast/node/agnocast_rosout.hpp" |
| 2 | + |
| 3 | +#include "agnocast/agnocast_publisher.hpp" |
| 4 | +#include "agnocast/node/agnocast_node.hpp" |
| 5 | + |
| 6 | +#include <rcl_interfaces/msg/log.hpp> |
| 7 | +#include <rcutils/logging.h> |
| 8 | + |
| 9 | +#include <atomic> |
| 10 | +#include <cstdarg> |
| 11 | +#include <cstdio> |
| 12 | +#include <mutex> |
| 13 | + |
| 14 | +namespace agnocast |
| 15 | +{ |
| 16 | + |
| 17 | +static Publisher<rcl_interfaces::msg::Log>::SharedPtr g_rosout_pub; |
| 18 | +static std::mutex g_rosout_mtx; |
| 19 | +static std::atomic<bool> g_in_handler{false}; |
| 20 | +static std::atomic<bool> g_rosout_initialized{false}; |
| 21 | +static rcutils_logging_output_handler_t g_original_handler = nullptr; |
| 22 | + |
| 23 | +static uint8_t severity_to_log_level(int severity) |
| 24 | +{ |
| 25 | + switch (severity) { |
| 26 | + case RCUTILS_LOG_SEVERITY_DEBUG: |
| 27 | + return rcl_interfaces::msg::Log::DEBUG; |
| 28 | + case RCUTILS_LOG_SEVERITY_INFO: |
| 29 | + return rcl_interfaces::msg::Log::INFO; |
| 30 | + case RCUTILS_LOG_SEVERITY_WARN: |
| 31 | + return rcl_interfaces::msg::Log::WARN; |
| 32 | + case RCUTILS_LOG_SEVERITY_ERROR: |
| 33 | + return rcl_interfaces::msg::Log::ERROR; |
| 34 | + case RCUTILS_LOG_SEVERITY_FATAL: |
| 35 | + return rcl_interfaces::msg::Log::FATAL; |
| 36 | + default: |
| 37 | + return rcl_interfaces::msg::Log::INFO; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void rosout_output_handler( |
| 42 | + const rcutils_log_location_t * location, int severity, const char * name, |
| 43 | + rcutils_time_point_value_t timestamp, const char * format, va_list * args) |
| 44 | +{ |
| 45 | + // Chain the original handler (console or noop, depending on --disable-stdout-logs). |
| 46 | + // Pass a copy because the handler may consume the va_list via vsnprintf. |
| 47 | + if (g_original_handler) { |
| 48 | + va_list args_for_original; |
| 49 | + va_copy(args_for_original, *args); |
| 50 | + g_original_handler(location, severity, name, timestamp, format, &args_for_original); |
| 51 | + va_end(args_for_original); |
| 52 | + } |
| 53 | + |
| 54 | + // Recursion guard: publishing may trigger log calls internally |
| 55 | + bool expected = false; |
| 56 | + if (!g_in_handler.compare_exchange_strong(expected, true)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + { |
| 61 | + std::lock_guard<std::mutex> lock(g_rosout_mtx); |
| 62 | + if (g_rosout_pub) { |
| 63 | + auto loaned_msg = g_rosout_pub->borrow_loaned_message(); |
| 64 | + |
| 65 | + loaned_msg->stamp.sec = static_cast<int32_t>(timestamp / 1000000000LL); |
| 66 | + loaned_msg->stamp.nanosec = static_cast<uint32_t>(timestamp % 1000000000LL); |
| 67 | + loaned_msg->level = severity_to_log_level(severity); |
| 68 | + loaned_msg->name = (name != nullptr) ? name : ""; |
| 69 | + |
| 70 | + // Format the message string from va_list |
| 71 | + va_list args_copy; |
| 72 | + va_copy(args_copy, *args); |
| 73 | + int len = vsnprintf(nullptr, 0, format, args_copy); |
| 74 | + va_end(args_copy); |
| 75 | + if (len >= 0) { |
| 76 | + std::string msg_str(static_cast<size_t>(len), '\0'); |
| 77 | + va_copy(args_copy, *args); |
| 78 | + vsnprintf(&msg_str[0], static_cast<size_t>(len) + 1, format, args_copy); |
| 79 | + va_end(args_copy); |
| 80 | + loaned_msg->msg = std::move(msg_str); |
| 81 | + } |
| 82 | + |
| 83 | + if (location != nullptr) { |
| 84 | + loaned_msg->file = (location->file_name != nullptr) ? location->file_name : ""; |
| 85 | + loaned_msg->function = (location->function_name != nullptr) ? location->function_name : ""; |
| 86 | + loaned_msg->line = location->line_number; |
| 87 | + } |
| 88 | + |
| 89 | + g_rosout_pub->publish(std::move(loaned_msg)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + g_in_handler.store(false); |
| 94 | +} |
| 95 | + |
| 96 | +void setup_rosout_handler(Node * node) |
| 97 | +{ |
| 98 | + // Only set up once per process, even with multiple nodes |
| 99 | + bool expected = false; |
| 100 | + if (!g_rosout_initialized.compare_exchange_strong(expected, true)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + auto pub = node->create_publisher<rcl_interfaces::msg::Log>( |
| 105 | + "/rosout", rclcpp::QoS(100).reliable().transient_local()); |
| 106 | + |
| 107 | + { |
| 108 | + std::lock_guard<std::mutex> lock(g_rosout_mtx); |
| 109 | + g_rosout_pub = pub; |
| 110 | + } |
| 111 | + |
| 112 | + // Capture the current handler (console or noop) before replacing |
| 113 | + g_original_handler = rcutils_logging_get_output_handler(); |
| 114 | + rcutils_logging_set_output_handler(rosout_output_handler); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace agnocast |
0 commit comments