diff --git a/libuavcan/include/uavcan/build_config.hpp b/libuavcan/include/uavcan/build_config.hpp index f9d6c65934..06a771c2cc 100644 --- a/libuavcan/include/uavcan/build_config.hpp +++ b/libuavcan/include/uavcan/build_config.hpp @@ -58,6 +58,22 @@ # endif #endif +/** + * This specification is used by some error reporting functions like in the Logger class. + * The default can be overriden by defining the macro UAVCAN_NOEXCEPT explicitly, e.g. via compiler options. + */ +#ifndef UAVCAN_NOEXCEPT +# if UAVCAN_EXCEPTIONS +# if UAVCAN_CPP_VERSION >= UAVCAN_CPP11 +# define UAVCAN_NOEXCEPT noexcept +# else +# define UAVCAN_NOEXCEPT throw() +# endif +# else +# define UAVCAN_NOEXCEPT +# endif +#endif + /** * Struct layout control. * Set UAVCAN_PACK_STRUCTS=1 and define UAVCAN_PACKED_BEGIN and UAVCAN_PACKED_END to reduce memory usage. diff --git a/libuavcan/include/uavcan/protocol/logger.hpp b/libuavcan/include/uavcan/protocol/logger.hpp index e7d606a630..9001dcf65f 100644 --- a/libuavcan/include/uavcan/protocol/logger.hpp +++ b/libuavcan/include/uavcan/protocol/logger.hpp @@ -148,52 +148,52 @@ public: #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11 template - int log(LogLevel level, const char* source, const char* format, Args... args); + int log(LogLevel level, const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT; template - inline int logDebug(const char* source, const char* format, Args... args) + inline int logDebug(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::DEBUG, source, format, args...); } template - inline int logInfo(const char* source, const char* format, Args... args) + inline int logInfo(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::INFO, source, format, args...); } template - inline int logWarning(const char* source, const char* format, Args... args) + inline int logWarning(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::WARNING, source, format, args...); } template - inline int logError(const char* source, const char* format, Args... args) + inline int logError(const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::ERROR, source, format, args...); } #else - int log(LogLevel level, const char* source, const char* text); + int log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT; - int logDebug(const char* source, const char* text) + int logDebug(const char* source, const char* text) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::DEBUG, source, text); } - int logInfo(const char* source, const char* text) + int logInfo(const char* source, const char* text) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::INFO, source, text); } - int logWarning(const char* source, const char* text) + int logWarning(const char* source, const char* text) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::WARNING, source, text); } - int logError(const char* source, const char* text) + int logError(const char* source, const char* text) UAVCAN_NOEXCEPT { return log(protocol::debug::LogLevel::ERROR, source, text); } @@ -207,18 +207,29 @@ public: #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11 template -int Logger::log(LogLevel level, const char* source, const char* format, Args... args) +int Logger::log(LogLevel level, const char* source, const char* format, Args... args) UAVCAN_NOEXCEPT { - if (level >= level_ || level >= getExternalSinkLevel()) +#if UAVCAN_EXCEPTIONS + try +#endif { - msg_buf_.level.value = level; - msg_buf_.source = source; - msg_buf_.text.clear(); - CharArrayFormatter formatter(msg_buf_.text); - formatter.write(format, args...); - return log(msg_buf_); + if (level >= level_ || level >= getExternalSinkLevel()) + { + msg_buf_.level.value = level; + msg_buf_.source = source; + msg_buf_.text.clear(); + CharArrayFormatter formatter(msg_buf_.text); + formatter.write(format, args...); + return log(msg_buf_); + } + return 0; } - return 0; +#if UAVCAN_EXCEPTIONS + catch (...) + { + return -ErrFailure; + } +#endif } #endif diff --git a/libuavcan/src/protocol/uc_logger.cpp b/libuavcan/src/protocol/uc_logger.cpp index a0e900b285..3c6661e306 100644 --- a/libuavcan/src/protocol/uc_logger.cpp +++ b/libuavcan/src/protocol/uc_logger.cpp @@ -36,16 +36,27 @@ int Logger::log(const protocol::debug::LogMessage& message) #if UAVCAN_CPP_VERSION < UAVCAN_CPP11 -int Logger::log(LogLevel level, const char* source, const char* text) +int Logger::log(LogLevel level, const char* source, const char* text) UAVCAN_NOEXCEPT { - if (level >= level_ || level >= getExternalSinkLevel()) +#if UAVCAN_EXCEPTIONS + try +#endif { - msg_buf_.level.value = level; - msg_buf_.source = source; - msg_buf_.text = text; - return log(msg_buf_); + if (level >= level_ || level >= getExternalSinkLevel()) + { + msg_buf_.level.value = level; + msg_buf_.source = source; + msg_buf_.text = text; + return log(msg_buf_); + } + return 0; } - return 0; +#if UAVCAN_EXCEPTIONS + catch (...) + { + return -ErrFailure; + } +#endif } #endif