From 6435c82d067e72940b61902f1e424f0f6dbebe90 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Fri, 11 Apr 2014 19:02:24 +0400 Subject: [PATCH] Linux driver got default logger that dumps everything into stderr --- .../linux/include/uavcan_linux/helpers.hpp | 24 +++++++++++++++++-- libuavcan_drivers/linux/test/test_node.cpp | 13 ---------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/libuavcan_drivers/linux/include/uavcan_linux/helpers.hpp b/libuavcan_drivers/linux/include/uavcan_linux/helpers.hpp index 533a551a65..11ebdab444 100644 --- a/libuavcan_drivers/linux/include/uavcan_linux/helpers.hpp +++ b/libuavcan_drivers/linux/include/uavcan_linux/helpers.hpp @@ -7,10 +7,25 @@ #include #include #include +#include +#include #include namespace uavcan_linux { +/** + * Default log sink will dump everything into stderr + */ +class DefaultLogSink : public uavcan::ILogSink +{ + virtual void log(const uavcan::protocol::debug::LogMessage& message) + { + const auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + const auto tstr = std::ctime(&tt); + std::cerr << "### UAVCAN " << tstr << message << std::endl; + } +}; + /** * Contains all drivers needed for uavcan::Node. */ @@ -37,6 +52,7 @@ static constexpr std::size_t NodeMemPoolSize = 1024 * 512; // One size fits all class Node : public uavcan::Node { DriverPackPtr driver_pack_; + DefaultLogSink log_sink_; static void enforce(int error, const char* msg) { @@ -52,7 +68,9 @@ public: */ Node(uavcan::ICanDriver& can_driver, uavcan::ISystemClock& clock) : uavcan::Node(can_driver, clock) - { } + { + getLogger().setExternalSink(&log_sink_); + } /** * Takes ownership of the driver container. @@ -60,7 +78,9 @@ public: Node(DriverPackPtr driver_pack) : uavcan::Node(driver_pack->can, driver_pack->clock) , driver_pack_(driver_pack) - { } + { + getLogger().setExternalSink(&log_sink_); + } template std::shared_ptr> diff --git a/libuavcan_drivers/linux/test/test_node.cpp b/libuavcan_drivers/linux/test/test_node.cpp index af496e0e11..a32037eb46 100644 --- a/libuavcan_drivers/linux/test/test_node.cpp +++ b/libuavcan_drivers/linux/test/test_node.cpp @@ -7,18 +7,6 @@ #include #include "debug.hpp" -class LogSink : public uavcan::ILogSink -{ - virtual void log(const uavcan::protocol::debug::LogMessage& message) - { - std::cout << "--- LOCAL LOG ---\n" - << message << "\n" - << "-----------------" << std::endl; - } -}; - -static LogSink log_sink_; - static uavcan_linux::NodePtr initNode(const std::vector& ifaces, uavcan::NodeID nid, const std::string& name) { @@ -31,7 +19,6 @@ static uavcan_linux::NodePtr initNode(const std::vector& ifaces, ua node->setName(name.c_str()); node->getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG); - node->getLogger().setExternalSink(&log_sink_); /* * Starting the node. This may take a few seconds.