d1bbab2b6e7d5666136256362361c0e329fb77f3
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / src / CAmDltWrapper.cpp
1 /**
2  * Copyright (C) 2012, BMW AG
3  *
4  * This file is part of GENIVI Project AudioManager.
5  *
6  * Contributions are licensed to the GENIVI Alliance under one or more
7  * Contribution License Agreements.
8  *
9  * \copyright
10  * This Source Code Form is subject to the terms of the
11  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
12  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  *
15  * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
16  *
17  * \file CAmDltWrapper.cpp
18  * For further information see http://www.genivi.org/.
19  *
20  */
21
22
23 #include "shared/CAmDltWrapper.h"
24 #include <string>
25 #include <sstream>
26 #include <iostream>
27 #include <string.h>
28
29 namespace am
30 {
31
32 CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL;
33 pthread_mutex_t CAmDltWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER;
34
35 CAmDltWrapper *CAmDltWrapper::instance(const bool enableNoDLTDebug)
36 {
37     if (!mpDLTWrapper)
38         mpDLTWrapper = new CAmDltWrapper(enableNoDLTDebug);
39 #ifndef WITH_DLT
40     if(enableNoDLTDebug)
41         mpDLTWrapper->enableNoDLTDebug(true);
42 #endif        
43     return (mpDLTWrapper);
44 }
45
46 void CAmDltWrapper::unregisterContext(DltContext & handle)
47 {
48 #ifdef WITH_DLT
49     dlt_unregister_context(&handle);
50 #else
51     (void) handle;
52 #endif
53 }
54
55 void CAmDltWrapper::deinit()
56 {
57 #ifdef WITH_DLT
58     unregisterContext(mDltContext);
59 #endif
60 }
61
62 CAmDltWrapper::CAmDltWrapper(const bool enableNoDLTDebug) :
63 #ifndef WITH_DLT
64         mEnableNoDLTDebug(enableNoDLTDebug),
65 #endif
66         mDltContext(), //
67         mDltContextData()
68 {
69     (void) enableNoDLTDebug;
70 #ifndef WITH_DLT
71     std::cout << "\e[0;34m[DLT]\e[0;30m\tRunning without DLT-support" << std::endl;
72 #endif
73 }
74
75 void CAmDltWrapper::registerApp(const char *appid, const char *description)
76 {
77 #ifdef WITH_DLT
78     dlt_register_app(appid, description);
79     //register a default context
80     dlt_register_context(&mDltContext, "def", "default Context registered by DLTWrapper CLass");
81 #else
82     (void) appid;
83     (void) description;
84 #endif
85 }
86
87 void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
88 {
89 #ifdef WITH_DLT
90     dlt_register_context(&handle, contextid, description);
91 #else
92     strncpy(handle.contextID,contextid,4);
93
94     // store only the first contextID
95     if(0 == strlen(mDltContext.contextID))
96     {
97         memcpy(&mDltContext.contextID,contextid,4);
98         const size_t str_len = strlen(description);
99         if(str_len < 2000)
100         {
101             mDltContextData.context_description = new char[str_len + 1];
102             (void) strcpy(mDltContextData.context_description,description);
103         }
104     }
105
106     std::cout << "\e[0;34m[DLT]\e[0;30m\tRegistering Context " << contextid << " , " << description << std::endl;
107
108 #endif
109 }
110
111 void CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context)
112 {
113     (void) loglevel;
114     pthread_mutex_lock(&mMutex);
115     if (!context)
116         context = &mDltContext;
117 #ifdef WITH_DLT
118     dlt_user_log_write_start(context, &mDltContextData, loglevel);
119 #else
120     if(mEnableNoDLTDebug)
121         std::cout << "\e[0;34m[" << context->contextID << "]\e[0;30m\t";
122 #endif
123
124 }
125
126 void CAmDltWrapper::send()
127 {
128 #ifdef WITH_DLT
129     dlt_user_log_write_finish(&mDltContextData);
130 #else
131     if(mEnableNoDLTDebug)
132         std::cout << mDltContextData.buffer.str().c_str() << std::endl;
133
134     mDltContextData.buffer.str("");
135     mDltContextData.buffer.clear();
136 #endif
137     pthread_mutex_unlock(&mMutex);
138 }
139
140 void CAmDltWrapper::append(const int8_t value)
141 {
142 #ifdef WITH_DLT
143     dlt_user_log_write_int8(&mDltContextData, value);
144 #else
145     appendNoDLT(value);
146 #endif
147 }
148
149 void CAmDltWrapper::append(const uint8_t value)
150 {
151 #ifdef WITH_DLT
152     dlt_user_log_write_uint8(&mDltContextData, value);
153 #else
154     appendNoDLT(value);
155 #endif
156 }
157
158 void CAmDltWrapper::append(const int16_t value)
159 {
160 #ifdef WITH_DLT
161     dlt_user_log_write_int16(&mDltContextData, value);
162 #else
163     appendNoDLT(value);
164 #endif
165 }
166
167 void CAmDltWrapper::append(const uint16_t value)
168 {
169 #ifdef WITH_DLT
170     dlt_user_log_write_uint16(&mDltContextData, value);
171 #else
172     appendNoDLT(value);
173 #endif
174 }
175
176 void CAmDltWrapper::append(const int32_t value)
177 {
178 #ifdef WITH_DLT
179     dlt_user_log_write_int32(&mDltContextData, value);
180 #else
181     appendNoDLT(value);
182 #endif
183 }
184
185 void CAmDltWrapper::append(const uint32_t value)
186 {
187 #ifdef WITH_DLT
188     dlt_user_log_write_uint32(&mDltContextData, value);
189 #else
190     appendNoDLT(value);
191 #endif
192 }
193
194 void CAmDltWrapper::append(const char*& value)
195 {
196 #ifdef WITH_DLT
197     dlt_user_log_write_string(&mDltContextData, value);
198 #else
199     mDltContextData.buffer << value;
200 #endif
201 }
202
203 void CAmDltWrapper::append(const std::string& value)
204 {
205 #ifdef WITH_DLT
206     dlt_user_log_write_string(&mDltContextData, value.c_str());
207 #else
208     mDltContextData.buffer << value;
209 #endif
210 }
211
212 void CAmDltWrapper::append(const bool value)
213 {
214 #ifdef WITH_DLT
215     dlt_user_log_write_bool(&mDltContextData, static_cast<uint8_t>(value));
216 #else
217     appendNoDLT(value);
218 #endif
219 }
220
221 void CAmDltWrapper::append(const int64_t value)
222 {
223 #ifdef WITH_DLT
224     dlt_user_log_write_int64(&mDltContextData, value);
225 #else
226     appendNoDLT(value);
227 #endif
228 }
229
230 void CAmDltWrapper::append(const uint64_t value)
231 {
232 #ifdef WITH_DLT
233     dlt_user_log_write_uint64(&mDltContextData, value);
234 #else
235     appendNoDLT(value);
236 #endif
237 }
238
239 void CAmDltWrapper::append(const am_Error_e value)
240 {
241         switch (value)
242         {
243                 case am_Error_e::E_OK:
244                         #ifdef WITH_DLT
245                                 dlt_user_log_write_string(&mDltContextData, "E_OK");
246                         #else
247                                 mDltContextData.buffer << "E_OK";
248                         #endif
249                         break;
250                 case am_Error_e::E_ABORTED:
251                         #ifdef WITH_DLT
252                                 dlt_user_log_write_string(&mDltContextData, "E_ABORTED");
253                         #else
254                                 mDltContextData.buffer << "E_ABORTED";
255                         #endif
256                         break;
257                 case am_Error_e::E_ALREADY_EXISTS:
258                         #ifdef WITH_DLT
259                                 dlt_user_log_write_string(&mDltContextData, "E_ALREADY_EXISTS");
260                         #else
261                                 mDltContextData.buffer << "E_ALREADY_EXISTS";
262                         #endif
263                         break;
264                 case am_Error_e::E_DATABASE_ERROR:
265                         #ifdef WITH_DLT
266                                 dlt_user_log_write_string(&mDltContextData, "E_DATABASE_ERROR");
267                         #else
268                                 mDltContextData.buffer << "E_DATABASE_ERROR";
269                         #endif
270                         break;
271                 case am_Error_e::E_NON_EXISTENT:
272                         #ifdef WITH_DLT
273                                 dlt_user_log_write_string(&mDltContextData, "E_NON_EXISTENT");
274                         #else
275                                 mDltContextData.buffer << "E_NON_EXISTENT";
276                         #endif
277                         break;
278                 case am_Error_e::E_NOT_POSSIBLE:
279                         #ifdef WITH_DLT
280                                 dlt_user_log_write_string(&mDltContextData, "E_NOT_POSSIBLE");
281                         #else
282                                 mDltContextData.buffer << "E_NOT_POSSIBLE";
283                         #endif
284                         break;
285                 case am_Error_e::E_NOT_USED:
286                         #ifdef WITH_DLT
287                                 dlt_user_log_write_string(&mDltContextData, "E_NOT_USED");
288                         #else
289                                 mDltContextData.buffer << "E_NOT_USED";
290                         #endif
291                         break;
292                 case am_Error_e::E_NO_CHANGE:
293                         #ifdef WITH_DLT
294                                 dlt_user_log_write_string(&mDltContextData, "E_NO_CHANGE");
295                         #else
296                                 mDltContextData.buffer << "E_NO_CHANGE";
297                         #endif
298                         break;
299                 case am_Error_e::E_OUT_OF_RANGE:
300                         #ifdef WITH_DLT
301                                 dlt_user_log_write_string(&mDltContextData, "E_OUT_OF_RANGE");
302                         #else
303                                 mDltContextData.buffer << "E_OUT_OF_RANGE";
304                         #endif
305                         break;
306                 case am_Error_e::E_UNKNOWN:
307                         #ifdef WITH_DLT
308                                 dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
309                         #else
310                                 mDltContextData.buffer << "E_UNKNOWN";
311                         #endif
312                         break;
313                 default:
314                         #ifdef WITH_DLT
315                                 dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
316                         #else
317                                 mDltContextData.buffer << "E_UNKNOWN";
318                         #endif
319
320         }
321 }
322
323 #ifndef WITH_DLT
324 template<class T> void CAmDltWrapper::appendNoDLT(T value)
325 {
326     mDltContextData.buffer << value;
327 }
328
329 void CAmDltWrapper::enableNoDLTDebug(const bool enableNoDLTDebug)
330 {
331     mEnableNoDLTDebug = enableNoDLTDebug;
332 }
333 #endif
334
335 CAmDltWrapper::~CAmDltWrapper()
336 {
337     if (mpDLTWrapper)
338     {
339         mpDLTWrapper->unregisterContext(mDltContext);
340         delete mpDLTWrapper;
341     }
342 }
343 }
344
345