From 6e19d1c7ad83ab0d54b61d698d541fa00653a49e Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 9 May 2015 12:17:57 +0300 Subject: [PATCH] Logger moved to header --- libuavcan/include/uavcan/protocol/logger.hpp | 56 ++++++++++++++-- libuavcan/src/protocol/uc_logger.cpp | 69 -------------------- 2 files changed, 51 insertions(+), 74 deletions(-) delete mode 100644 libuavcan/src/protocol/uc_logger.cpp diff --git a/libuavcan/include/uavcan/protocol/logger.hpp b/libuavcan/include/uavcan/protocol/logger.hpp index fcacfc4801..5856e07355 100644 --- a/libuavcan/include/uavcan/protocol/logger.hpp +++ b/libuavcan/include/uavcan/protocol/logger.hpp @@ -6,9 +6,11 @@ #define UAVCAN_PROTOCOL_LOGGER_HPP_INCLUDED #include +#include #include #include #include +#include #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 { diff --git a/libuavcan/src/protocol/uc_logger.cpp b/libuavcan/src/protocol/uc_logger.cpp deleted file mode 100644 index 241267ac50..0000000000 --- a/libuavcan/src/protocol/uc_logger.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2014 Pavel Kirienko - */ - -#include -#include - -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 - -}