AudioManager  7.6.6
Native Application Runtime Environment
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.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;
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  bool init(DltLogLevelType loglevel, DltContext* context = NULL);
161  {
162 #ifdef WITH_DLT
163  #ifdef DLT_IS_LOG_LEVEL_ENABLED
164  if (mlogDestination == logDestination::DAEMON)
165  {
166  return (dlt_user_is_logLevel_enabled(&mDltContext, logLevel) == DLT_RETURN_TRUE);
167  }
168  #else
169  (void)logLevel;
170  #endif
171  return true;
172 #else
173  return (logLevel <= mDltContext.log_level_user);
174 #endif
175  }
176  void deinit();
177  void send();
178  void append(const int8_t value);
179  void append(const uint8_t value);
180  void append(const int16_t value);
181  void append(const uint16_t value);
182  void append(const int32_t value);
183  void append(const uint32_t value);
184  void append(const uint64_t value);
185  void append(const int64_t value);
186  void append(const std::string& value);
187  void append(const bool value);
188  void append(const std::vector<uint8_t> & data);
189 
190  template<class T> void appendNoDLT(T value)
191  {
192  mNoDltContextData.buffer << value <<" ";
193  }
194 
195  // specialization for const char*
196  template<typename T = const char*> void append(const char* value)
197  {
198  #ifdef WITH_DLT
199  if (mlogDestination == logDestination::DAEMON)
200  {
201  dlt_user_log_write_string(&mDltContextData, value);
202  }
203  else
204  {
205  mNoDltContextData.buffer << std::string(value);
206  }
207  #else
208  mNoDltContextData.buffer << std::string(value);
209  #endif //WITH_DLT
210 
211  }
212 
213 private:
214  static const std::vector<const char*> mStr_error;
215  static const std::vector<const char*> mStr_sourceState;
216  static const std::vector<const char*> mStr_MuteState;
217  static const std::vector<const char*> mStr_DomainState;
218  static const std::vector<const char*> mStr_ConnectionState;
219  static const std::vector<const char*> mStr_Availability;
220  static const std::vector<const char*> mStr_Interrupt;
221  static const std::vector<const char*> mStr_Handle;
222  static const std::vector<const char*> mStr_NotificationStatus;
223 
224 public:
225 
226  // specialization for const am_Error_e
227  template<typename T = const am_Error_e> void append(const am_Error_e value)
228  {
229  if (static_cast<std::size_t>(value) >= mStr_error.size())
230  {
231  append("value for am_Error_e out of bounds!");
232  append(static_cast<uint16_t>(value));
233  return;
234  }
235  append(mStr_error[value]);
236  }
237 
238  // specialization for const am_Error_e
239  template<typename T = const am_SourceState_e> void append(const am_SourceState_e value)
240  {
241  if (static_cast<std::size_t>(value) >= mStr_sourceState.size())
242  {
243  append("value for am_SourceState_e out of bounds!");
244  append(static_cast<uint16_t>(value));
245  return;
246  }
247  append(mStr_sourceState[value]);
248  }
249 
250 
251  template<typename T = const am_MuteState_e> void append(const am_MuteState_e value)
252  {
253  if (static_cast<std::size_t>(value) >= mStr_MuteState.size())
254  {
255  append("value for am_MuteState_e out of bounds!");
256  append(static_cast<uint16_t>(value));
257  return;
258  }
259  append(mStr_MuteState[value]);
260  }
261 
262  template<typename T = const am_DomainState_e> void append(const am_DomainState_e value)
263  {
264  if (static_cast<std::size_t>(value) >= mStr_DomainState.size())
265  {
266  append("value for am_DomainState_e out of bounds!");
267  append(static_cast<uint16_t>(value));
268  return;
269  }
270  append(mStr_DomainState[value]);
271  }
272 
273  template<typename T = const am_ConnectionState_e> void append(const am_ConnectionState_e value)
274  {
275  if (static_cast<std::size_t>(value) >= mStr_ConnectionState.size())
276  {
277  append("value for am_ConnectionState_e out of bounds!");
278  append(static_cast<uint16_t>(value));
279  return;
280  }
281  append(mStr_ConnectionState[value]);
282  }
283 
284  template<typename T = const am_Availability_e> void append(const am_Availability_e value)
285  {
286  if (static_cast<std::size_t>(value) >= mStr_Availability.size())
287  {
288  append("value for am_Availability_e out of bounds!");
289  append(static_cast<uint16_t>(value));
290  return;
291  }
292  append(mStr_Availability[value]);
293  }
294 
295  template<typename T = const am_InterruptState_e> void append(const am_InterruptState_e value)
296  {
297  if (static_cast<std::size_t>(value) >= mStr_Interrupt.size())
298  {
299  append("value for am_InterruptState_e out of bounds!");
300  append(static_cast<uint16_t>(value));
301  return;
302  }
303  append(mStr_Interrupt[value]);
304  }
305 
306  template<typename T = const am_Handle_e> void append(const am_Handle_e value)
307  {
308  if (static_cast<std::size_t>(value) >= mStr_Handle.size())
309  {
310  append("value for am_Handle_e out of bounds!");
311  append(static_cast<uint16_t>(value));
312  return;
313  }
314  append(mStr_Handle[value]);
315  }
316 
317  template<typename T = const am_Handle_s> void append(const am_Handle_s value)
318  {
319  append (value.handleType);
320  append (value.handle);
321  }
322 
323  template<typename T = const am_NotificationStatus_e> void append(const am_NotificationStatus_e value)
324  {
325  if (static_cast<std::size_t>(value) >= mStr_NotificationStatus.size())
326  {
327  append("value for am_NotificationStatus_e out of bounds!");
328  append(static_cast<uint16_t>(value));
329  return;
330  }
331  append(mStr_NotificationStatus[value]);
332  }
333 
334  // Template to print unknown pointer types with their address
335  template<typename T> void append(T* value)
336  {
337  std::ostringstream ss;
338  ss << "0x" << std::hex << (uint64_t)value;
339  append(ss.str().c_str());
340  }
341 
342  // Template to print unknown types
343  template<typename T> void append(T value)
344  {
345  std::ostringstream ss;
346  ss << std::dec << value;
347  append(ss.str().c_str());
348  }
349 
350  // Template parameter pack to generate recursive code
351  void append(void) {}
352  template<typename T, typename... TArgs> void append(T value, TArgs... args)
353  {
354  this->append(value);
355  this->append(args...);
356  }
357 
358 private:
362  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
363  bool initNoDlt(DltLogLevelType loglevel, DltContext* context);
364  std::string now();
365  DltContext mDltContext;
366  DltContextData mDltContextData;
367  NoDltContextData mNoDltContextData;
368  std::map<DltContext*,std::string> mMapContext;
369  bool mDebugEnabled;
370  logDestination mlogDestination;
371  std::ofstream mFilename;
372  bool mOnlyError;
373  bool mLogOn;
374  static CAmDltWrapper* mpDLTWrapper;
375  static pthread_mutex_t mMutex;
376 
377 };
378 
386 template<typename T, typename... TArgs>
387 void log(DltContext* const context, DltLogLevelType loglevel, T value, TArgs... args)
388 {
390  if (!inst->getEnabled())
391  {
392  return;
393  }
394  if (!inst->init(loglevel, context))
395  {
396  return;
397  }
398  inst->append(value);
399  inst->append(args...);
400  inst->send();
401 }
402 
408 template<typename T, typename... TArgs>
409 void logDebug(T value, TArgs... args)
410 {
411  log(NULL, DLT_LOG_DEBUG, value, args...);
412 }
413 
419 template<typename T, typename... TArgs>
420 void logInfo(T value, TArgs... args)
421 {
422  log(NULL, DLT_LOG_INFO, value, args...);
423 }
424 
430 template<typename T, typename... TArgs>
431 void logError(T value, TArgs... args)
432 {
433  log(NULL, DLT_LOG_ERROR,value,args...);
434 }
435 
441 template<typename T, typename... TArgs>
442 void logWarning(T value, TArgs... args)
443 {
444  log(NULL, DLT_LOG_WARN,value,args...);
445 }
446 
452 template<typename T, typename... TArgs>
453 void logVerbose(T value, TArgs... args)
454 {
455  log(NULL, DLT_LOG_VERBOSE,value,args...);
456 }
457 
458 }
459 
460 #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
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
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
bool checkLogLevel(DltLogLevelType logLevel)
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)