From e5ffa017047525ed2e3ee41ea2294546ee64894c Mon Sep 17 00:00:00 2001 From: Stefan Vacek Date: Fri, 28 Aug 2015 16:59:52 +0200 Subject: Add C++ extension - header only implementation, uses variadic templates from C++ 11 - Enable installation and building tests with -DWITH_DLT_CXX11_EXT - allow logging in the form of DLT_LOG_CXX(context, level, param1, param2, param3), e.g. - allow logging of user types (if a function logToDlt for the given user-type is present), e.g. - Added pkg-config file automotive-dlt-c++.pc - Sample code is provided in tests/dlt-test-cpp-extension.cpp Signed-off-by: Stefan Vacek --- include/dlt/CMakeLists.txt | 7 ++ include/dlt/dlt_cpp_extension.hpp | 238 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 include/dlt/dlt_cpp_extension.hpp (limited to 'include') diff --git a/include/dlt/CMakeLists.txt b/include/dlt/CMakeLists.txt index a137206..057af1f 100644 --- a/include/dlt/CMakeLists.txt +++ b/include/dlt/CMakeLists.txt @@ -18,3 +18,10 @@ install(FILES dlt.h dlt_user.h dlt_user_macros.h dlt_client.h dlt_protocol.h dlt_common.h dlt_types.h dlt_version.h dlt_shm.h dlt_offline_trace.h dlt_filetransfer.h dlt_common_api.h DESTINATION include/dlt COMPONENT devel) + + +if(${WITH_DLT_CXX11_EXT}) + install(FILES dlt_cpp_extension.hpp + DESTINATION include/dlt + COMPONENT devel) +endif() \ No newline at end of file diff --git a/include/dlt/dlt_cpp_extension.hpp b/include/dlt/dlt_cpp_extension.hpp new file mode 100644 index 0000000..8949658 --- /dev/null +++ b/include/dlt/dlt_cpp_extension.hpp @@ -0,0 +1,238 @@ +/** + * @licence app begin@ + * Copyright (C) 2015 Intel Corporation + * + * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps. + * + * Contributions are licensed to the GENIVI Alliance under one or more + * Contribution License Agreements. + * + * \copyright + * This Source Code Form is subject to the terms of the + * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with + * this file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * \file dlt_cpp_extension.hpp + * For further information see http://www.genivi.org/. + * @licence end@ + */ + + +#ifndef DLT_CPP_EXTENSION_HPP +#define DLT_CPP_EXTENSION_HPP + +#include +#include +#include +#include +#include + + +template +int32_t logToDlt(DltContextData &log, T const &value) = delete; + + +template<> +inline int32_t logToDlt(DltContextData &log, int8_t const &value) +{ + return dlt_user_log_write_int8(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, int16_t const &value) +{ + return dlt_user_log_write_int16(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, int32_t const &value) +{ + return dlt_user_log_write_int32(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, int64_t const &value) +{ + return dlt_user_log_write_int64(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, uint8_t const &value) +{ + return dlt_user_log_write_uint8(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, uint16_t const &value) +{ + return dlt_user_log_write_uint16(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, uint32_t const &value) +{ + return dlt_user_log_write_uint32(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, uint64_t const &value) +{ + return dlt_user_log_write_uint64(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, float32_t const &value) +{ + return dlt_user_log_write_float32(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, double const &value) +{ + return dlt_user_log_write_float64(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, bool const &value) +{ + return dlt_user_log_write_bool(&log, value); +} + +static inline int32_t logToDlt(DltContextData &log, char const * const value) +{ + return dlt_user_log_write_utf8_string(&log, value); +} + +static inline int32_t logToDlt(DltContextData &log, char * const value) +{ + return dlt_user_log_write_utf8_string(&log, value); +} + +template<> +inline int32_t logToDlt(DltContextData &log, std::string const &value) +{ + return dlt_user_log_write_utf8_string(&log, value.c_str()); +} + + +/* stl types */ +template<> +int32_t logToDlt(DltContextData &log, std::string const &value); + +template> +static inline int32_t logToDlt(DltContextData &log, std::vector<_Tp, _Alloc > const & value) +{ + int result = 0; + + for (auto elem: value) + { + result += logToDlt(log, elem); + } + + if (result != 0) + { + result = -1; + } + return result; +} + +template> +static inline int32_t logToDlt(DltContextData &log, std::list<_Tp, _Alloc > const & value) +{ + int result = 0; + + for (auto elem: value) + { + result += logToDlt(log, elem); + } + + if (result != 0) + { + result = -1; + } + return result; +} + +template, + typename _Alloc = std::allocator >> +static inline int32_t logToDlt(DltContextData &log, std::map<_Key, _Tp, _Compare, _Alloc > const & value) +{ + int result = 0; + + for (auto elem: value) + { + result += logToDlt(log, elem.first); + result += logToDlt(log, elem.second); + } + + if (result != 0) + { + result = -1; + } + return result; +} + + +//variadic functions using C11 standard +template +static inline int32_t logToDltVariadic(DltContextData &log, First const &valueA) +{ + return logToDlt(log, valueA); +} + +template +static inline int32_t logToDltVariadic(DltContextData &log, First const &valueA, const Rest&... valueB) +{ + int result = logToDlt(log, valueA) + logToDltVariadic(log, valueB...); + if ( result != 0 ) + { + result = -1; + } + return result; +} + + +/** + * @brief macro to write a log message with variable number of arguments and without the need to specify the type of log data + * + * The macro can be used with any type that provides a logToDlt function. + * + * Example: + * DLT_LOG_CXX(dltContext, DLT_LV_X, "text", valueA, valueB, ...) + */ +#define DLT_LOG_CXX(CONTEXT, LOGLEVEL, ...)\ + do{ \ + DltContextData log; \ + if (dlt_user_log_write_start(&CONTEXT,&log,LOGLEVEL)>0) \ + { \ + logToDltVariadic(log, ##__VA_ARGS__); \ + dlt_user_log_write_finish(&log); \ + } \ + } while(0) + +/** + * @brief macro to write a log message with variable number of arguments and without the need to specify the type of log data. + * + * The macro can be used with any type that provides a logToDlt function. + * This includes all the types that are code generated. + * + * This macro is similar to \c DLT_LOG_CXX. However, it adds the current function name as the first log argument. + * + * Example: + * DLT_LOG_FCN_CXX(dltContext, DLT_LV_X, "text", valueA, valueB, ...) + */ +#define DLT_LOG_FCN_CXX(CONTEXT, LOGLEVEL, ...) \ + do { \ + DltContextData log; \ + if (dlt_user_log_write_start(&CONTEXT, &log, LOGLEVEL) > 0) \ + { \ + dlt_user_log_write_string(&log, __PRETTY_FUNCTION__); \ + logToDltVariadic(log, ##__VA_ARGS__); \ + dlt_user_log_write_finish(&log); \ + } \ + } while(0) + + +#endif /* DLT_CPP_EXTENSION_HPP */ + + -- cgit v1.2.1