AudioManager  7.5.11
Native Application Runtime Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CAmDltWrapper.h
Go to the documentation of this file.
1 
18 #ifndef DLTWRAPPER_H_
19 #define DLTWRAPPER_H_
20 
21 #include <string>
22 #include <pthread.h>
23 #include <sstream>
24 #include <iostream>
25 #include <fstream>
26 #include <map>
27 #include <vector>
28 #include <audiomanagerconfig.h>
29 #include "audiomanagertypes.h"
30 
31 #ifdef WITH_DLT
32  #include <dlt/dlt.h>
33 #else
34  #include <stdint.h>
35  #include <sstream>
36 
37  #define DLT_USER_BUF_MAX_SIZE 2048
38 
42  typedef struct
43  {
44  char contextID[4];
45  int32_t log_level_pos;
46  int32_t log_level_user;
47  } DltContext;
48 
52  typedef enum
53  {
58 
62  typedef struct
63  {
65  std::stringstream buffer;
66  int32_t log_level;
67  int32_t trace_status;
68  int32_t args_num;
69  uint8_t mcnt;
71  } DltContextData;
72 
76  typedef enum
77  {
79  DLT_LOG_OFF = 0x00,
80  DLT_LOG_FATAL = 0x01,
81  DLT_LOG_ERROR = 0x02,
82  DLT_LOG_WARN = 0x03,
83  DLT_LOG_INFO = 0x04,
84  DLT_LOG_DEBUG = 0x05,
87 
88  #define DLT_DEFAULT_LOG_LEVEL DLT_LOG_INFO
89  #define DLT_DECLARE_CONTEXT(CONTEXT) \
90  DltContext CONTEXT;
91 
92  #define DLT_IMPORT_CONTEXT(CONTEXT) \
93  extern DltContext CONTEXT;
94 
95 #endif // WITH_DLT
96 
97 namespace am
98 {
99 
107 {
108 public:
109 
113  typedef struct
114  {
116  std::stringstream buffer;
117  int32_t log_level;
118  int32_t trace_status;
119  int32_t args_num;
120  uint8_t mcnt;
123 
124  /*
125  * The eunum gives the logtype
126  */
128  {
129  DAEMON=0,
132  };
133 
143  static CAmDltWrapper* instanctiateOnce(const char *appid, const char * description, const bool debugEnabled = true, const logDestination logDest = logDestination::DAEMON, const std::string Filename="",bool onlyError=false);
144 
148  static CAmDltWrapper* instance();
149 
153  void registerContext(DltContext& handle, const char *contextid, const char * description);
154  void registerContext(DltContext& handle, const char *contextid, const char * description, const DltLogLevelType level, const DltTraceStatusType status);
155  void unregisterContext(DltContext& handle);
156  bool getEnabled();
157  ~CAmDltWrapper();
158 
159 
160  bool init(DltLogLevelType loglevel, DltContext* context = NULL);
161  void deinit();
162  void send();
163  void append(const int8_t value);
164  void append(const uint8_t value);
165  void append(const int16_t value);
166  void append(const uint16_t value);
167  void append(const int32_t value);
168  void append(const uint32_t value);
169  void append(const uint64_t value);
170  void append(const int64_t value);
171  void append(const std::string& value);
172  void append(const bool value);
173  void append(const std::vector<uint8_t> & data);
174 
175  template<class T> void appendNoDLT(T value)
176  {
177  mNoDltContextData.buffer << value <<" ";
178  }
179 
180  // specialization for const char*
181  template<typename T = const char*> void append(const char* value)
182  {
183  #ifdef WITH_DLT
184  if (mlogDestination == logDestination::DAEMON)
185  {
186  dlt_user_log_write_string(&mDltContextData, value);
187  }
188  else
189  {
190  mNoDltContextData.buffer << std::string(value);
191  }
192  #else
193  mNoDltContextData.buffer << std::string(value);
194  #endif //WITH_DLT
195 
196  }
197 
198 private:
199  static const std::vector<const char*> mStr_error;
200  static const std::vector<const char*> mStr_sourceState;
201  static const std::vector<const char*> mStr_MuteState;
202  static const std::vector<const char*> mStr_DomainState;
203  static const std::vector<const char*> mStr_ConnectionState;
204  static const std::vector<const char*> mStr_Availability;
205  static const std::vector<const char*> mStr_Interrupt;
206  static const std::vector<const char*> mStr_Handle;
207  static const std::vector<const char*> mStr_NotificationStatus;
208 
209 public:
210 
211  // specialization for const am_Error_e
212  template<typename T = const am_Error_e> void append(const am_Error_e value)
213  {
214  if ((int)value >=mStr_error.size())
215  {
216  append("value for am_Error_e out of bounds!");
217  append(value);
218  return;
219  }
220  append(mStr_error[value]);
221  }
222 
223  // specialization for const am_Error_e
224  template<typename T = const am_SourceState_e> void append(const am_SourceState_e value)
225  {
226  if ((int)value >=(mStr_sourceState.size()))
227  {
228  append("value for am_SourceState_e out of bounds!");
229  append(value);
230  return;
231  }
232  append(mStr_sourceState[value]);
233  }
234 
235 
236  template<typename T = const am_MuteState_e> void append(const am_MuteState_e value)
237  {
238  if ((int)value >=mStr_MuteState.size())
239  {
240  append("value for am_MuteState_e out of bounds!");
241  append(value);
242  return;
243  }
244  append(mStr_MuteState[value]);
245  }
246 
247  template<typename T = const am_DomainState_e> void append(const am_DomainState_e value)
248  {
249  if ((int)value >= mStr_DomainState.size())
250  {
251  append("value for am_DomainState_e out of bounds!");
252  append(value);
253  return;
254  }
255  append(mStr_DomainState[value]);
256  }
257 
258  template<typename T = const am_ConnectionState_e> void append(const am_ConnectionState_e value)
259  {
260  if ((int)value >=mStr_ConnectionState.size())
261  {
262  append("value for am_ConnectionState_e out of bounds!");
263  append(value);
264  return;
265  }
266  append(mStr_ConnectionState[value]);
267  }
268 
269  template<typename T = const am_Availability_e> void append(const am_Availability_e value)
270  {
271  if ((int)value >= mStr_Availability.size())
272  {
273  append("value for am_Availability_e out of bounds!");
274  append(value);
275  return;
276  }
277  append(mStr_Availability[value]);
278  }
279 
280  template<typename T = const am_InterruptState_e> void append(const am_InterruptState_e value)
281  {
282  if ((int)value >=mStr_Interrupt.size())
283  {
284  append("value for am_InterruptState_e out of bounds!");
285  append(value);
286  return;
287  }
288  append(mStr_Interrupt[value]);
289  }
290 
291  template<typename T = const am_Handle_e> void append(const am_Handle_e value)
292  {
293  if ((int)value >=mStr_Handle.size())
294  {
295  append("value for am_Handle_e out of bounds!");
296  append(value);
297  return;
298  }
299  append(mStr_Handle[value]);
300  }
301 
302  template<typename T = const am_Handle_s> void append(const am_Handle_s value)
303  {
304  append (value.handleType);
305  append (value.handle);
306  }
307 
308  template<typename T = const am_NotificationStatus_e> void append(const am_NotificationStatus_e value)
309  {
310  if ((int)value >=mStr_NotificationStatus.size())
311  {
312  append("value for am_NotificationStatus_e out of bounds!");
313  append(value);
314  return;
315  }
316  append(mStr_NotificationStatus[value]);
317  }
318 
319  // Template to print unknown pointer types with their address
320  template<typename T> void append(T* value)
321  {
322  std::ostringstream ss;
323  ss << "0x" << std::hex << (uint64_t)value;
324  append(ss.str().c_str());
325  }
326 
327  // Template to print unknown types
328  template<typename T> void append(T value)
329  {
330  std::ostringstream ss;
331  ss << std::dec << value;
332  append(ss.str().c_str());
333  }
334 
335  // Template parameter pack to generate recursive code
336  void append(void) {}
337  template<typename T, typename... TArgs> void append(T value, TArgs... args)
338  {
339  this->append(value);
340  this->append(args...);
341  }
342 
343 private:
347  CAmDltWrapper(const char *appid, const char * description, const bool debugEnabled = true, const logDestination logDest = logDestination::DAEMON, const std::string Filename="",bool onlyError=false); //is private because of singleton pattern
348  bool initNoDlt(DltLogLevelType loglevel, DltContext* context);
349  std::string now();
350  DltContext mDltContext;
351  DltContextData mDltContextData;
352  NoDltContextData mNoDltContextData;
353  std::map<DltContext*,std::string> mMapContext;
354  bool mDebugEnabled;
355  logDestination mlogDestination;
356  std::ofstream mFilename;
357  bool mOnlyError;
358  bool mLogOn;
359  static CAmDltWrapper* mpDLTWrapper;
360  static pthread_mutex_t mMutex;
361 
362 };
363 
371 template<typename T, typename... TArgs>
372 void log(DltContext* const context, DltLogLevelType loglevel, T value, TArgs... args)
373 {
375  if (!inst->getEnabled())
376  {
377  return;
378  }
379  if (!inst->init(loglevel, context))
380  {
381  return;
382  }
383  inst->append(value);
384  inst->append(args...);
385  inst->send();
386 }
387 
393 template<typename T, typename... TArgs>
394 void logDebug(T value, TArgs... args)
395 {
396  log(NULL, DLT_LOG_DEBUG, value, args...);
397 }
398 
404 template<typename T, typename... TArgs>
405 void logInfo(T value, TArgs... args)
406 {
407  log(NULL, DLT_LOG_INFO, value, args...);
408 }
409 
415 template<typename T, typename... TArgs>
416 void logError(T value, TArgs... args)
417 {
418  log(NULL, DLT_LOG_ERROR,value,args...);
419 }
420 
426 template<typename T, typename... TArgs>
427 void logWarning(T value, TArgs... args)
428 {
429  log(NULL, DLT_LOG_WARN,value,args...);
430 }
431 
437 template<typename T, typename... TArgs>
438 void logVerbose(T value, TArgs... args)
439 {
440  log(NULL, DLT_LOG_VERBOSE,value,args...);
441 }
442 
443 }
444 
445 #endif /* DLTWRAPPER_H_ */
Wraps around the dlt.
int32_t args_num
number of arguments for extended header
Definition: CAmDltWrapper.h:68
am_Availability_e
with the help of this enum, sinks and sources can report their availability state ...
logging into a file
void logWarning(T value, TArgs...args)
logs given values with warninglevel with the default context
std::stringstream buffer
buffer for building log message
am_Error_e
the errors of the audiomanager.
am_InterruptState_e
void append(T value)
Log level off.
Definition: CAmDltWrapper.h:79
void logInfo(T value, TArgs...args)
logs given values with infolevel with the default context
fatal system error
Definition: CAmDltWrapper.h:80
void append(const int8_t value)
void append(const am_Handle_s value)
logging with the DLT daemon
This structure is used for context data used in an application.
Definition: CAmDltWrapper.h:62
DltContext * handle
pointer to DltContext
Definition: CAmDltWrapper.h:64
static CAmDltWrapper * instanctiateOnce(const char *appid, const char *description, const bool debugEnabled=true, const logDestination logDest=logDestination::DAEMON, const std::string Filename="", bool onlyError=false)
Instanciate the Dlt Wrapper.
char * context_description
description of context
highest grade of information
Definition: CAmDltWrapper.h:85
void append(const am_SourceState_e value)
bool init(DltLogLevelType loglevel, DltContext *context=NULL)
void log(DltContext *const context, DltLogLevelType loglevel, T value, TArgs...args)
logs given values with a given context (register first!) and given loglevel
void append(T *value)
Trace status: Off.
Definition: CAmDltWrapper.h:55
char * context_description
description of context
Definition: CAmDltWrapper.h:70
void append(const am_Availability_e value)
Default trace status.
Definition: CAmDltWrapper.h:54
am_NotificationStatus_e
int32_t trace_status
trace status
Definition: CAmDltWrapper.h:67
void append(const am_Error_e value)
void logVerbose(T value, TArgs...args)
logs given values with verbose with the default context
int32_t args_num
number of arguments for extended header
static CAmDltWrapper * instance()
get the Wrapper Instance
Default log level.
Definition: CAmDltWrapper.h:78
void registerContext(DltContext &handle, const char *contextid, const char *description)
register a context
uint8_t mcnt
message counter
a handle is used for asynchronous operations and is uniquely assigned for each of this operations ...
This structure is used for every context used in an application.
Definition: CAmDltWrapper.h:42
Trace status: On.
Definition: CAmDltWrapper.h:56
int32_t log_level_pos
offset in user-application context field
Definition: CAmDltWrapper.h:45
informational
Definition: CAmDltWrapper.h:83
am_ConnectionState_e
represents the connection state
This structure is used for context data used in an application.
void logDebug(T value, TArgs...args)
logs given values with debuglevel with the default context
void append(const am_NotificationStatus_e value)
void append(const am_ConnectionState_e value)
void appendNoDLT(T value)
am_Handle_e handleType
the handletype
void append(const am_MuteState_e value)
int32_t log_level
log level
Definition: CAmDltWrapper.h:66
void append(const am_DomainState_e value)
Copyright (C) 2012 - 2014, BMW AG.
error with impact to correct functionality
Definition: CAmDltWrapper.h:81
uint16_t handle
the handle as value
void logError(T value, TArgs...args)
logs given values with errorlevel with the default context
am_Handle_e
This enumeration is used to define the type of the action that is correlated to a handle...
DltTraceStatusType
Definition of DLT trace status.
Definition: CAmDltWrapper.h:52
void unregisterContext(DltContext &handle)
uint8_t mcnt
message counter
Definition: CAmDltWrapper.h:69
void append(const am_InterruptState_e value)
warning, correct behaviour could not be ensured
Definition: CAmDltWrapper.h:82
logging with commandline
void append(T value, TArgs...args)
DltLogLevelType
Definitions of DLT log level.
Definition: CAmDltWrapper.h:76
void append(const am_Handle_e value)
std::stringstream buffer
buffer for building log message
Definition: CAmDltWrapper.h:65
int32_t log_level_user
Definition: CAmDltWrapper.h:46
am_SourceState_e
The source state reflects the state of the source.
DltContext * handle
pointer to DltContext
void append(const char *value)