automotive-dlt
dlt_cpp_extension.hpp
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2015 Intel Corporation
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 #ifndef DLT_CPP_EXTENSION_HPP
28 #define DLT_CPP_EXTENSION_HPP
29 
30 #include <string>
31 #include <vector>
32 #include <list>
33 #include <map>
34 
35 #include "dlt.h"
36 
37 template<typename T>
38 int32_t logToDlt(DltContextData &log, T const &value) = delete;
39 
40 template<>
41 inline int32_t logToDlt(DltContextData &log, int8_t const &value)
42 {
43  return dlt_user_log_write_int8(&log, value);
44 }
45 
46 template<>
47 inline int32_t logToDlt(DltContextData &log, int16_t const &value)
48 {
49  return dlt_user_log_write_int16(&log, value);
50 }
51 
52 template<>
53 inline int32_t logToDlt(DltContextData &log, int32_t const &value)
54 {
55  return dlt_user_log_write_int32(&log, value);
56 }
57 
58 template<>
59 inline int32_t logToDlt(DltContextData &log, int64_t const &value)
60 {
61  return dlt_user_log_write_int64(&log, value);
62 }
63 
64 template<>
65 inline int32_t logToDlt(DltContextData &log, uint8_t const &value)
66 {
67  return dlt_user_log_write_uint8(&log, value);
68 }
69 
70 template<>
71 inline int32_t logToDlt(DltContextData &log, uint16_t const &value)
72 {
73  return dlt_user_log_write_uint16(&log, value);
74 }
75 
76 template<>
77 inline int32_t logToDlt(DltContextData &log, uint32_t const &value)
78 {
79  return dlt_user_log_write_uint32(&log, value);
80 }
81 
82 template<>
83 inline int32_t logToDlt(DltContextData &log, uint64_t const &value)
84 {
85  return dlt_user_log_write_uint64(&log, value);
86 }
87 
88 template<>
89 inline int32_t logToDlt(DltContextData &log, float32_t const &value)
90 {
91  return dlt_user_log_write_float32(&log, value);
92 }
93 
94 template<>
95 inline int32_t logToDlt(DltContextData &log, double const &value)
96 {
97  return dlt_user_log_write_float64(&log, value);
98 }
99 
100 template<>
101 inline int32_t logToDlt(DltContextData &log, bool const &value)
102 {
103  return dlt_user_log_write_bool(&log, value);
104 }
105 
106 static inline int32_t logToDlt(DltContextData &log, char const * const value)
107 {
108  return dlt_user_log_write_utf8_string(&log, value);
109 }
110 
111 static inline int32_t logToDlt(DltContextData &log, char * const value)
112 {
113  return dlt_user_log_write_utf8_string(&log, value);
114 }
115 
116 template<>
117 inline int32_t logToDlt(DltContextData &log, std::string const &value)
118 {
119  return dlt_user_log_write_utf8_string(&log, value.c_str());
120 }
121 
122 /* stl types */
123 template<>
124 int32_t logToDlt(DltContextData &log, std::string const &value);
125 
126 template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
127 static inline int32_t logToDlt(DltContextData &log, std::vector<_Tp, _Alloc> const & value)
128 {
129  int result = 0;
130 
131  for (auto elem : value)
132  result += logToDlt(log, elem);
133 
134  if (result != 0)
135  result = -1;
136 
137  return result;
138 }
139 
140 template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
141 static inline int32_t logToDlt(DltContextData &log, std::list<_Tp, _Alloc> const & value)
142 {
143  int result = 0;
144 
145  for (auto elem : value)
146  result += logToDlt(log, elem);
147 
148  if (result != 0)
149  result = -1;
150 
151  return result;
152 }
153 
154 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
155  typename _Alloc = std::allocator<std::pair<const _Key, _Tp>>>
156 static inline int32_t logToDlt(DltContextData &log, std::map<_Key, _Tp, _Compare, _Alloc> const & value)
157 {
158  int result = 0;
159 
160  for (auto elem : value)
161  {
162  result += logToDlt(log, elem.first);
163  result += logToDlt(log, elem.second);
164  }
165 
166  if (result != 0)
167  result = -1;
168 
169  return result;
170 }
171 
172 //variadic functions using C11 standard
173 template<typename First>
174 static inline int32_t logToDltVariadic(DltContextData &log, First const &valueA)
175 {
176  return logToDlt(log, valueA);
177 }
178 
179 template<typename First, typename ... Rest>
180 static inline int32_t logToDltVariadic(DltContextData &log, First const &valueA, const Rest&... valueB)
181 {
182  int result = logToDlt(log, valueA) + logToDltVariadic(log, valueB...);
183 
184  if (result != 0)
185  result = -1;
186 
187  return result;
188 }
189 
198 #define DLT_LOG_CXX(CONTEXT, LOGLEVEL, ...)\
199  do\
200  {\
201  DltContextData log;\
202  if (dlt_user_log_write_start(&CONTEXT,&log,LOGLEVEL)>0)\
203  {\
204  logToDltVariadic(log, ##__VA_ARGS__);\
205  dlt_user_log_write_finish(&log);\
206  }\
207  }\
208  while(0)
209 
221 #define DLT_LOG_FCN_CXX(CONTEXT, LOGLEVEL, ...) \
222  do\
223  {\
224  DltContextData log;\
225  if (dlt_user_log_write_start(&CONTEXT, &log, LOGLEVEL) > 0)\
226  {\
227  dlt_user_log_write_string(&log, __PRETTY_FUNCTION__);\
228  logToDltVariadic(log, ##__VA_ARGS__);\
229  dlt_user_log_write_finish(&log);\
230  }\
231  }\
232  while(0)
233 
234 #endif /* DLT_CPP_EXTENSION_HPP */
DltReturnValue dlt_user_log_write_float32(DltContextData *log, float32_t data)
Definition: dlt_user.c:1559
DltReturnValue dlt_user_log_write_int16(DltContextData *log, int16_t data)
Definition: dlt_user.c:2140
DltReturnValue dlt_user_log_write_int32(DltContextData *log, int32_t data)
Definition: dlt_user.c:2175
DltReturnValue dlt_user_log_write_utf8_string(DltContextData *log, const char *text)
Definition: dlt_user.c:2332
DltReturnValue dlt_user_log_write_int64(DltContextData *log, int64_t data)
Definition: dlt_user.c:2210
DltReturnValue dlt_user_log_write_uint8(DltContextData *log, uint8_t data)
Definition: dlt_user.c:1682
DltReturnValue dlt_user_log_write_uint32(DltContextData *log, uint32_t data)
Definition: dlt_user.c:1752
DltReturnValue dlt_user_log_write_int8(DltContextData *log, int8_t data)
Definition: dlt_user.c:2105
int32_t logToDlt(DltContextData &log, T const &value)=delete
static int32_t logToDltVariadic(DltContextData &log, First const &valueA)
DltReturnValue dlt_user_log_write_uint64(DltContextData *log, uint64_t data)
Definition: dlt_user.c:1787
DltReturnValue dlt_user_log_write_uint16(DltContextData *log, uint16_t data)
Definition: dlt_user.c:1717
DltReturnValue dlt_user_log_write_bool(DltContextData *log, uint8_t data)
Definition: dlt_user.c:2245
DltReturnValue dlt_user_log_write_float64(DltContextData *log, float64_t data)
Definition: dlt_user.c:1599
float float32_t
Definition: dlt_types.h:178