2 * Copyright (C) 2012, BMW AG
4 * This file is part of GENIVI Project AudioManager.
6 * Contributions are licensed to the GENIVI Alliance under one or more
7 * Contribution License Agreements.
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/.
15 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
17 * \file CAmDltWrapper.cpp
18 * For further information see http://www.genivi.org/.
23 #include "shared/CAmDltWrapper.h"
32 CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL;
33 pthread_mutex_t CAmDltWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER;
35 CAmDltWrapper *CAmDltWrapper::instance(const bool enableNoDLTDebug)
38 mpDLTWrapper = new CAmDltWrapper(enableNoDLTDebug);
41 mpDLTWrapper->enableNoDLTDebug(true);
43 return (mpDLTWrapper);
46 void CAmDltWrapper::unregisterContext(DltContext & handle)
49 dlt_unregister_context(&handle);
55 void CAmDltWrapper::deinit()
58 unregisterContext(mDltContext);
62 CAmDltWrapper::CAmDltWrapper(const bool enableNoDLTDebug) :
64 mEnableNoDLTDebug(enableNoDLTDebug),
69 (void) enableNoDLTDebug;
71 std::cout << "\e[0;34m[DLT]\e[0;30m\tRunning without DLT-support" << std::endl;
75 void CAmDltWrapper::registerApp(const char *appid, const char *description)
78 dlt_register_app(appid, description);
79 //register a default context
80 dlt_register_context(&mDltContext, "def", "default Context registered by DLTWrapper CLass");
87 void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
90 dlt_register_context(&handle, contextid, description);
92 strncpy(handle.contextID,contextid,4);
94 // store only the first contextID
95 if(0 == strlen(mDltContext.contextID))
97 memcpy(&mDltContext.contextID,contextid,4);
98 const size_t str_len = strlen(description);
101 mDltContextData.context_description = new char[str_len + 1];
102 (void) strcpy(mDltContextData.context_description,description);
106 std::cout << "\e[0;34m[DLT]\e[0;30m\tRegistering Context " << contextid << " , " << description << std::endl;
111 void CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context)
114 pthread_mutex_lock(&mMutex);
116 context = &mDltContext;
118 dlt_user_log_write_start(context, &mDltContextData, loglevel);
120 if(mEnableNoDLTDebug)
121 std::cout << "\e[0;34m[" << context->contextID << "]\e[0;30m\t";
126 void CAmDltWrapper::send()
129 dlt_user_log_write_finish(&mDltContextData);
131 if(mEnableNoDLTDebug)
132 std::cout << mDltContextData.buffer.str().c_str() << std::endl;
134 mDltContextData.buffer.str("");
135 mDltContextData.buffer.clear();
137 pthread_mutex_unlock(&mMutex);
140 void CAmDltWrapper::append(const int8_t value)
143 dlt_user_log_write_int8(&mDltContextData, value);
149 void CAmDltWrapper::append(const uint8_t value)
152 dlt_user_log_write_uint8(&mDltContextData, value);
158 void CAmDltWrapper::append(const int16_t value)
161 dlt_user_log_write_int16(&mDltContextData, value);
167 void CAmDltWrapper::append(const uint16_t value)
170 dlt_user_log_write_uint16(&mDltContextData, value);
176 void CAmDltWrapper::append(const int32_t value)
179 dlt_user_log_write_int32(&mDltContextData, value);
185 void CAmDltWrapper::append(const uint32_t value)
188 dlt_user_log_write_uint32(&mDltContextData, value);
194 void CAmDltWrapper::append(const char*& value)
197 dlt_user_log_write_string(&mDltContextData, value);
199 mDltContextData.buffer << value;
203 void CAmDltWrapper::append(const std::string& value)
206 dlt_user_log_write_string(&mDltContextData, value.c_str());
208 mDltContextData.buffer << value;
212 void CAmDltWrapper::append(const bool value)
215 dlt_user_log_write_bool(&mDltContextData, static_cast<uint8_t>(value));
221 void CAmDltWrapper::append(const int64_t value)
224 dlt_user_log_write_int64(&mDltContextData, value);
230 void CAmDltWrapper::append(const uint64_t value)
233 dlt_user_log_write_uint64(&mDltContextData, value);
239 void CAmDltWrapper::append(const am_Error_e value)
243 case am_Error_e::E_OK:
245 dlt_user_log_write_string(&mDltContextData, "E_OK");
247 mDltContextData.buffer << "E_OK";
250 case am_Error_e::E_ABORTED:
252 dlt_user_log_write_string(&mDltContextData, "E_ABORTED");
254 mDltContextData.buffer << "E_ABORTED";
257 case am_Error_e::E_ALREADY_EXISTS:
259 dlt_user_log_write_string(&mDltContextData, "E_ALREADY_EXISTS");
261 mDltContextData.buffer << "E_ALREADY_EXISTS";
264 case am_Error_e::E_DATABASE_ERROR:
266 dlt_user_log_write_string(&mDltContextData, "E_DATABASE_ERROR");
268 mDltContextData.buffer << "E_DATABASE_ERROR";
271 case am_Error_e::E_NON_EXISTENT:
273 dlt_user_log_write_string(&mDltContextData, "E_NON_EXISTENT");
275 mDltContextData.buffer << "E_NON_EXISTENT";
278 case am_Error_e::E_NOT_POSSIBLE:
280 dlt_user_log_write_string(&mDltContextData, "E_NOT_POSSIBLE");
282 mDltContextData.buffer << "E_NOT_POSSIBLE";
285 case am_Error_e::E_NOT_USED:
287 dlt_user_log_write_string(&mDltContextData, "E_NOT_USED");
289 mDltContextData.buffer << "E_NOT_USED";
292 case am_Error_e::E_NO_CHANGE:
294 dlt_user_log_write_string(&mDltContextData, "E_NO_CHANGE");
296 mDltContextData.buffer << "E_NO_CHANGE";
299 case am_Error_e::E_OUT_OF_RANGE:
301 dlt_user_log_write_string(&mDltContextData, "E_OUT_OF_RANGE");
303 mDltContextData.buffer << "E_OUT_OF_RANGE";
306 case am_Error_e::E_UNKNOWN:
308 dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
310 mDltContextData.buffer << "E_UNKNOWN";
315 dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
317 mDltContextData.buffer << "E_UNKNOWN";
324 template<class T> void CAmDltWrapper::appendNoDLT(T value)
326 mDltContextData.buffer << value;
329 void CAmDltWrapper::enableNoDLTDebug(const bool enableNoDLTDebug)
331 mEnableNoDLTDebug = enableNoDLTDebug;
335 CAmDltWrapper::~CAmDltWrapper()
339 mpDLTWrapper->unregisterContext(mDltContext);