Linux driver got default logger that dumps everything into stderr

This commit is contained in:
Pavel Kirienko
2014-04-11 19:02:24 +04:00
parent 3b0b0494b8
commit 6435c82d06
2 changed files with 22 additions and 15 deletions
@@ -7,10 +7,25 @@
#include <memory>
#include <string>
#include <vector>
#include <chrono>
#include <iostream>
#include <uavcan/uavcan.hpp>
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<NodeMemPoolSize>
{
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<NodeMemPoolSize>(can_driver, clock)
{ }
{
getLogger().setExternalSink(&log_sink_);
}
/**
* Takes ownership of the driver container.
@@ -60,7 +78,9 @@ public:
Node(DriverPackPtr driver_pack)
: uavcan::Node<NodeMemPoolSize>(driver_pack->can, driver_pack->clock)
, driver_pack_(driver_pack)
{ }
{
getLogger().setExternalSink(&log_sink_);
}
template <typename DataType>
std::shared_ptr<uavcan::Subscriber<DataType>>
@@ -7,18 +7,6 @@
#include <uavcan/protocol/node_status_monitor.hpp>
#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<std::string>& ifaces, uavcan::NodeID nid,
const std::string& name)
{
@@ -31,7 +19,6 @@ static uavcan_linux::NodePtr initNode(const std::vector<std::string>& 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.