Logger moved to header

This commit is contained in:
Pavel Kirienko
2015-05-09 12:17:57 +03:00
parent fc7e242100
commit 6e19d1c7ad
2 changed files with 51 additions and 74 deletions
+51 -5
View File
@@ -6,9 +6,11 @@
#define UAVCAN_PROTOCOL_LOGGER_HPP_INCLUDED
#include <uavcan/time.hpp>
#include <uavcan/build_config.hpp>
#include <uavcan/protocol/debug/LogMessage.hpp>
#include <uavcan/marshal/char_array_formatter.hpp>
#include <uavcan/node/publisher.hpp>
#include <cstdlib>
#if !defined(UAVCAN_CPP_VERSION) || !defined(UAVCAN_CPP11)
# error UAVCAN_CPP_VERSION
@@ -62,7 +64,7 @@ public:
* This value is higher than any valid severity value.
* Use it to completely suppress the output.
*/
static const LogLevel LevelAboveAll = (1 << protocol::debug::LogLevel::FieldTypes::value::BitLen) - 1;
static LogLevel getLogLevelAboveAll() { return (1U << protocol::debug::LogLevel::FieldTypes::value::BitLen) - 1U; }
private:
enum { DefaultTxTimeoutMs = 2000 };
@@ -72,7 +74,10 @@ private:
LogLevel level_;
ILogSink* external_sink_;
LogLevel getExternalSinkLevel() const;
LogLevel getExternalSinkLevel() const
{
return (external_sink_ == NULL) ? getLogLevelAboveAll() : external_sink_->getLogLevel();
}
public:
explicit Logger(INode& node)
@@ -89,7 +94,15 @@ public:
* Must be called once before use.
* Returns negative error code.
*/
int init();
int init()
{
const int res = logmsg_pub_.init();
if (res < 0)
{
return res;
}
return logmsg_pub_.setPriority(TransferPriorityLow); // Fixed priority
}
/**
* Logs one message. Please consider using helper methods instead of this one.
@@ -102,7 +115,19 @@ public:
*
* Returns negative error code.
*/
int log(const protocol::debug::LogMessage& message);
int log(const protocol::debug::LogMessage& message)
{
int retval = 0;
if (message.level.value >= getExternalSinkLevel())
{
external_sink_->log(message);
}
if (message.level.value >= level_)
{
retval = logmsg_pub_.broadcast(message);
}
return retval;
}
/**
* Severity filter for UAVCAN broadcasting.
@@ -177,7 +202,28 @@ public:
#else
int log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT;
int log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT
{
#if UAVCAN_EXCEPTIONS
try
#endif
{
if (level >= level_ || level >= getExternalSinkLevel())
{
msg_buf_.level.value = level;
msg_buf_.source = source;
msg_buf_.text = text;
return log(msg_buf_);
}
return 0;
}
#if UAVCAN_EXCEPTIONS
catch (...)
{
return -ErrFailure;
}
#endif
}
int logDebug(const char* source, const char* text) UAVCAN_NOEXCEPT
{
-69
View File
@@ -1,69 +0,0 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <uavcan/protocol/logger.hpp>
#include <cstdlib>
namespace uavcan
{
const Logger::LogLevel Logger::LevelAboveAll;
Logger::LogLevel Logger::getExternalSinkLevel() const
{
return (external_sink_ == NULL) ? LevelAboveAll : external_sink_->getLogLevel();
}
int Logger::init()
{
const int res = logmsg_pub_.init();
if (res < 0)
{
return res;
}
return logmsg_pub_.setPriority(TransferPriorityLow); // Fixed priority
}
int Logger::log(const protocol::debug::LogMessage& message)
{
int retval = 0;
if (message.level.value >= getExternalSinkLevel())
{
external_sink_->log(message);
}
if (message.level.value >= level_)
{
retval = logmsg_pub_.broadcast(message);
}
return retval;
}
#if UAVCAN_CPP_VERSION < UAVCAN_CPP11
int Logger::log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT
{
#if UAVCAN_EXCEPTIONS
try
#endif
{
if (level >= level_ || level >= getExternalSinkLevel())
{
msg_buf_.level.value = level;
msg_buf_.source = source;
msg_buf_.text = text;
return log(msg_buf_);
}
return 0;
}
#if UAVCAN_EXCEPTIONS
catch (...)
{
return -ErrFailure;
}
#endif
}
#endif
}