* solved merge conflicts
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / src / CAmDltWrapper.cpp
1 /**
2  * Copyright (C) 2011, BMW AG
3  *
4  * GeniviAudioMananger AudioManagerDaemon
5  *
6  * \file CAmDltWrapper.cpp
7  *
8  * \date 20-Oct-2011 3:42:04 PM
9  * \author Christian Mueller (christian.ei.mueller@bmw.de)
10  *
11  * \section License
12  * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13  * Copyright (C) 2011, BMW AG Christian Mueller  Christian.ei.mueller@bmw.de
14  *
15  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19  * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20  * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21  * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
22  *
23  */
24
25
26 #include "shared/CAmDltWrapper.h"
27 #include <string.h>
28 #include <sstream>
29 #include <iostream>
30
31 namespace am
32 {
33
34 CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL;
35
36 CAmDltWrapper *CAmDltWrapper::instance(const bool enableNoDLTDebug)
37 {
38     if (!mpDLTWrapper)
39         mpDLTWrapper = new CAmDltWrapper(enableNoDLTDebug);
40 #ifndef WITH_DLT
41     if(enableNoDLTDebug)
42         mpDLTWrapper->enableNoDLTDebug(true);
43 #endif        
44     return mpDLTWrapper;
45 }
46
47 void CAmDltWrapper::unregisterContext(DltContext & handle)
48 {
49 #ifdef WITH_DLT
50     dlt_unregister_context(&handle);
51 #endif
52 }
53
54 CAmDltWrapper::CAmDltWrapper(const bool enableNoDLTDebug) :
55 #ifndef WITH_DLT
56         mEnableNoDLTDebug(enableNoDLTDebug),
57 #endif
58         mDltContext(), //
59         mDltContextData()
60 {
61     (void) enableNoDLTDebug;
62 #ifndef WITH_DLT
63     std::cout << "[DLT] Running without DLT-support" << std::endl;
64 #endif
65 }
66
67 void CAmDltWrapper::registerApp(const char *appid, const char *description)
68 {
69 #ifdef WITH_DLT
70     dlt_register_app(appid, description);
71     //register a default context
72     dlt_register_context(&mDltContext, "def", "default Context registered by DLTWrapper CLass");
73 #endif
74 }
75
76 void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
77 {
78 #ifdef WITH_DLT
79     dlt_register_context(&handle, contextid, description);
80 #else
81     memcpy(&mDltContext.contextID,contextid,4);
82     strlen(description);
83     const size_t str_len = strlen(description);
84     if(str_len < 2000)
85     {
86         mDltContextData.context_description = new char[str_len + 1];
87         (void) strcpy(mDltContextData.context_description,description);
88     }
89 #endif
90 }
91
92 void CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context)
93 {
94     if (!context)
95         context = &mDltContext;
96 #ifdef WITH_DLT
97     dlt_user_log_write_start(context, &mDltContextData, loglevel);
98 #endif
99
100 }
101
102 void CAmDltWrapper::send()
103 {
104 #ifdef WITH_DLT
105     dlt_user_log_write_finish(&mDltContextData);
106 #else
107     if(mEnableNoDLTDebug)
108         std::cout << "[" << mDltContext.contextID << "] " << mDltContextData.buffer.str().c_str() << std::endl;
109
110     mDltContextData.buffer.str("");
111     mDltContextData.buffer.clear();
112 #endif
113 }
114
115 void CAmDltWrapper::append(const int8_t value)
116 {
117 #ifdef WITH_DLT
118     dlt_user_log_write_int8(&mDltContextData, value);
119 #else
120     appendNoDLT(value);
121 #endif
122 }
123
124 void CAmDltWrapper::append(const uint8_t value)
125 {
126 #ifdef WITH_DLT
127     dlt_user_log_write_uint8(&mDltContextData, value);
128 #else
129     appendNoDLT(value);
130 #endif
131 }
132
133 void CAmDltWrapper::append(const int16_t value)
134 {
135 #ifdef WITH_DLT
136     dlt_user_log_write_int16(&mDltContextData, value);
137 #else
138     appendNoDLT(value);
139 #endif
140 }
141
142 void CAmDltWrapper::append(const uint16_t value)
143 {
144 #ifdef WITH_DLT
145     dlt_user_log_write_uint16(&mDltContextData, value);
146 #else
147     appendNoDLT(value);
148 #endif
149 }
150
151 void CAmDltWrapper::append(const int32_t value)
152 {
153 #ifdef WITH_DLT
154     dlt_user_log_write_int32(&mDltContextData, value);
155 #else
156     appendNoDLT(value);
157 #endif
158 }
159
160 void CAmDltWrapper::append(const uint32_t value)
161 {
162 #ifdef WITH_DLT
163     dlt_user_log_write_uint32(&mDltContextData, value);
164 #else
165     appendNoDLT(value);
166 #endif
167 }
168
169 void CAmDltWrapper::append(const char*& value)
170 {
171 #ifdef WITH_DLT
172     dlt_user_log_write_string(&mDltContextData, value);
173 #else
174     mDltContextData.buffer << value;
175 #endif
176 }
177
178 void CAmDltWrapper::append(const std::string& value)
179 {
180 #ifdef WITH_DLT
181     dlt_user_log_write_string(&mDltContextData, value.c_str());
182 #else
183     mDltContextData.buffer << value;
184 #endif
185 }
186
187 void CAmDltWrapper::append(const bool value)
188 {
189 #ifdef WITH_DLT
190     dlt_user_log_write_bool(&mDltContextData, static_cast<uint8_t>(value));
191 #else
192     appendNoDLT(value);
193 #endif
194 }
195
196 #ifndef WITH_DLT
197 template<class T> void CAmDltWrapper::appendNoDLT(T value)
198 {
199     mDltContextData.buffer << value;
200 }
201
202 void CAmDltWrapper::enableNoDLTDebug(const bool enableNoDLTDebug)
203 {
204     mEnableNoDLTDebug = enableNoDLTDebug;
205 }
206 #endif
207
208 CAmDltWrapper::~CAmDltWrapper()
209 {
210     if (mpDLTWrapper)
211         delete mpDLTWrapper;
212 }
213 }
214