AudioManager  7.6.6
Native Application Runtime Environment
CAmDltWrapper.cpp
Go to the documentation of this file.
1 
26 #include "CAmDltWrapper.h"
27 #include <string>
28 #include <iostream>
29 #include <string.h>
30 #include <chrono>
31 #include <ctime>
32 #include <unistd.h>
33 
34 namespace am
35 {
36 CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL;
37 pthread_mutex_t CAmDltWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER;
38 
39 const std::vector<const char*> CAmDltWrapper::mStr_error =
40 {
41  "E_OK",
42  "E_UNKNOWN",
43  "E_OUT_OF_RANGE",
44  "E_NOT_USED",
45  "E_DATABASE_ERROR",
46  "E_ALREADY_EXISTS",
47  "E_NO_CHANGE",
48  "E_NOT_POSSIBLE",
49  "E_NON_EXISTENT",
50  "E_ABORTED",
51  "E_WRONG_FORMAT",
52  "E_COMMUNICATION",
53  "E_MAX"
54 };
55 
56 const std::vector<const char*> CAmDltWrapper::mStr_sourceState =
57 {
58  "SS_UNKNNOWN",
59  "SS_ON",
60  "SS_OFF",
61  "SS_PAUSED",
62  "SS_MAX"
63 };
64 
65 const std::vector<const char*> CAmDltWrapper::mStr_MuteState =
66 {
67  "MS_UNKNOWN" ,
68  "MS_MUTED" ,
69  "MS_UNMUTED" ,
70  "MS_MAX"
71 };
72 
73 const std::vector<const char*> CAmDltWrapper::mStr_DomainState =
74 {
75  "DS_UNKNOWN",
76  "DS_CONTROLLED",
77  "DS_INDEPENDENT_STARTUP",
78  "DS_INDEPENDENT_RUNDOWN",
79  "DS_MAX"
80 };
81 
82 const std::vector<const char*> CAmDltWrapper::mStr_ConnectionState =
83 {
84  "CS_UNKNOWN",
85  "CS_CONNECTING",
86  "CS_CONNECTED",
87  "CS_DISCONNECTING",
88  "CS_DISCONNECTED",
89  "CS_SUSPENDED",
90  "CS_MAX"
91 };
92 
93 const std::vector<const char*> CAmDltWrapper::mStr_Availability =
94 {
95  "A_UNKNOWN",
96  "A_AVAILABLE",
97  "A_UNAVAILABLE",
98  "A_MAX"
99 };
100 
101 const std::vector<const char*> CAmDltWrapper::mStr_Interrupt =
102 {
103  "IS_UNKNOWN",
104  "IS_OFF",
105  "IS_INTERRUPTED",
106  "IS_MAX"
107 };
108 
109 const std::vector<const char*> CAmDltWrapper::mStr_Handle =
110 {
111  "H_UNKNOWN",
112  "H_CONNECT",
113  "H_DISCONNECT",
114  "H_SETSOURCESTATE",
115  "H_SETSINKVOLUME",
116  "H_SETSOURCEVOLUME",
117  "H_SETSINKSOUNDPROPERTY",
118  "H_SETSOURCESOUNDPROPERTY",
119  "H_SETSINKSOUNDPROPERTIES",
120  "H_SETSOURCESOUNDPROPERTIES",
121  "H_CROSSFADE",
122  "H_SETVOLUMES",
123  "H_SETSINKNOTIFICATION",
124  "H_SETSOURCENOTIFICATION",
125  "H_MAX"
126 };
127 
128 const std::vector<const char*> CAmDltWrapper::mStr_NotificationStatus =
129 {
130  "NS_UNKNOWN",
131  "NS_OFF",
132  "NS_PERIODIC",
133  "NS_MINIMUM",
134  "NS_MAXIMUM",
135  "NS_CHANGE",
136  "NS_MAX"
137 };
138 
139 
140 
141 std::string CAmDltWrapper::now()
142 {
143  std::time_t t(std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()));
144  struct tm * timeinfo(localtime(&t));
145  char buffer[80];
146  std::strftime(buffer,80,"%D %T ",timeinfo);
147  return (std::string(buffer));
148 }
149 
150 CAmDltWrapper* CAmDltWrapper::instanctiateOnce(const char *appid, const char * description, const bool debugEnabled, const logDestination logDest, const std::string Filename,bool onlyError)
151 {
152  if (!mpDLTWrapper)
153  {
154  mpDLTWrapper = new CAmDltWrapper(appid,description,debugEnabled,logDest,Filename,onlyError);
155  }
156  return (mpDLTWrapper);
157 }
158 
160 {
161  if (!mpDLTWrapper)
162  {
163  // an application seems not to use our CAmDltWrapper class therefore create default
164  std::ostringstream description;
165  description << "PID=" << getpid() << " _=" << getenv("_");
166  mpDLTWrapper = new CAmDltWrapper("AMDL", description.str().c_str());
167  std::cerr << "Application doesn't call CAmDltWrapper::instanciateOnce!!!" << std::endl;
168  std::cerr << "-> CAmDltWrapper::instance registers DLT application [ AMDL | " << description.str() << " ]" << std::endl;
169  }
170  return mpDLTWrapper;
171 }
172 
174 {
175  return (mDebugEnabled);
176 }
177 
178 bool CAmDltWrapper::initNoDlt(DltLogLevelType loglevel, DltContext* context)
179 {
180  if (mlogDestination==logDestination::COMMAND_LINE)
181  {
182  if (!context)
183  {
184  switch (loglevel)
185  {
186  case DLT_LOG_OFF :
187  case DLT_LOG_FATAL :
188  case DLT_LOG_ERROR :
189  mNoDltContextData.buffer << "\033[0;31m"<<"[DEF] [Erro] \033[0m";
190  mLogOn=true;
191  break;
192  case DLT_LOG_WARN :
193  if (!mOnlyError)
194  {
195  mNoDltContextData.buffer << "\033[0;33m"<<"[DEF] [Warn] \033[0m";
196  }
197  else
198  mLogOn=false;
199  break;
200  case DLT_LOG_INFO :
201  if (!mOnlyError)
202  {
203  mNoDltContextData.buffer << "\033[0;36m"<<"[DEF] [Info] \033[0m";
204  }
205  else
206  mLogOn=false;
207  break;
208  default:
209  if (!mOnlyError)
210  {
211  mNoDltContextData.buffer << "\033[0;32m"<<"[DEF] [Defa] \033[0m";
212  }
213  else
214  mLogOn=false;
215  }
216  }
217  else
218  {
219  std::string con(mMapContext.at(context));
220  switch (loglevel)
221  {
222  case DLT_LOG_OFF :
223  case DLT_LOG_FATAL :
224  case DLT_LOG_ERROR :
225  mNoDltContextData.buffer << "\033[0;31m["<<con<<"] [Erro] \033[0m";
226  mLogOn=true;
227  break;
228  case DLT_LOG_WARN :
229  if (!mOnlyError)
230  {
231  mNoDltContextData.buffer << "\033[0;33m["<<con<<"] [Warn] \033[0m";
232  }
233  else
234  mLogOn=false;
235  break;
236  case DLT_LOG_INFO :
237  if (!mOnlyError)
238  {
239  mNoDltContextData.buffer << "\033[0;36m["<<con<<"] [Info] \033[0m";
240  }
241  else
242  mLogOn=false;
243 
244  break;
245  default:
246  if (!mOnlyError)
247  {
248  mNoDltContextData.buffer << "\033[0;32m["<<con<<"] [Defa] \033[0m";
249  }
250  else
251  mLogOn=false;
252  }
253  }
254  return true;
255  }
256  else
257  {
258  if (!context)
259  {
260  switch (loglevel)
261  {
262  case DLT_LOG_OFF :
263  case DLT_LOG_FATAL :
264  case DLT_LOG_ERROR :
265  mNoDltContextData.buffer <<"[DEF] [Erro] ";
266  mLogOn=true;
267  break;
268  case DLT_LOG_WARN :
269  if (!mOnlyError)
270  {
271  mNoDltContextData.buffer <<"[DEF] [Warn] ";
272  }
273  else
274  mLogOn=false;
275  break;
276  case DLT_LOG_INFO :
277  if (!mOnlyError)
278  {
279  mNoDltContextData.buffer <<"[DEF] [Info] ";
280  }
281  else
282  mLogOn=false;
283  break;
284  default:
285  if (!mOnlyError)
286  {
287  mNoDltContextData.buffer <<"[DEF] [Defa] ";
288  }
289  else
290  mLogOn=false;
291 
292  }
293  }
294  else
295  {
296  std::string con(mMapContext.at(context));
297  switch (loglevel)
298  {
299  case DLT_LOG_OFF :
300  case DLT_LOG_FATAL :
301  case DLT_LOG_ERROR :
302  mNoDltContextData.buffer << "["<<con<<"] [Erro] ";
303  mLogOn=true;
304  break;
305  case DLT_LOG_WARN :
306  if (!mOnlyError)
307  {
308  mNoDltContextData.buffer << "["<<con<<"] [Warn] ";
309  }
310  else
311  mLogOn=false;
312  break;
313  case DLT_LOG_INFO :
314  if (!mOnlyError)
315  {
316  mNoDltContextData.buffer << "["<<con<<"] [Info] ";
317  }
318  else
319  mLogOn=false;
320 
321  break;
322  default:
323  if (!mOnlyError)
324  {
325  mNoDltContextData.buffer << "["<<con<<"] [Defa] ";
326  }
327  else
328  mLogOn=false;
329  }
330  }
331  return true;
332  }
333 }
334 
335 #ifdef WITH_DLT
336 
337  CAmDltWrapper::CAmDltWrapper(const char *appid, const char * description, const bool debugEnabled, const logDestination logDest, const std::string Filename,bool onlyError) :
338  mDebugEnabled(debugEnabled), //
339  mlogDestination(logDest), //
340  mFilename(NULL), //
341  mOnlyError(onlyError), //
342  mLogOn(true)
343  {
344  if (mDebugEnabled && mlogDestination==logDestination::DAEMON)
345  {
346  dlt_register_app(appid, description);
347  //register a default context
348  dlt_register_context(&mDltContext, "DEF", "Default Context registered by DLTWrapper Class");
349  }
350  else if (mDebugEnabled)
351  {
352  if (mlogDestination==logDestination::COMMAND_LINE)
353  std::cout << "\033[0;36m[DLT] Registering AppID " << appid << " , " << description << "\033[0m"<< std::endl;
354  else
355  {
356  mFilename.open(Filename, std::ofstream::out | std::ofstream::trunc);
357  if (!mFilename.is_open())
358  {
359  throw std::runtime_error("Cannot open file for logging");
360  }
361  mFilename << now() << "[DLT] Registering AppID " << appid << " , " << description << std::endl;
362  }
363  }
364  }
365 
367  {
368  if (mpDLTWrapper && mDebugEnabled && mlogDestination==logDestination::DAEMON)
369  {
370  mpDLTWrapper->unregisterContext(mDltContext);
371  delete mpDLTWrapper;
372  }
373  else if (mpDLTWrapper && mDebugEnabled && mlogDestination==logDestination::COMMAND_LINE)
374  {
375  mFilename.close();
376  }
377  }
378 
380  {
381  if (mDebugEnabled && mlogDestination==logDestination::DAEMON)
382  {
383  dlt_unregister_context(&handle);
384  }
385  }
386 
387  void CAmDltWrapper::deinit()
388  {
389  if (mDebugEnabled)
390  {
391  unregisterContext(mDltContext);
392  }
393  }
394 
395  void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
396  {
397  if (mDebugEnabled && mlogDestination==logDestination::DAEMON)
398  {
399  dlt_register_context(&handle, contextid, description);
400  }
401  else if (mDebugEnabled)
402  {
403  mMapContext.emplace(&handle,std::string(contextid));
404 
405  if (mlogDestination==logDestination::COMMAND_LINE)
406  std::cout << "\033[0;36m[DLT] Registering Context " << contextid << " , " << description << "\033[0m"<< std::endl;
407  else
408  mFilename << now() << "[DLT] Registering Context " << contextid << " , " << description << std::endl;
409  }
410  }
411 
412  void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char * description,const DltLogLevelType level, const DltTraceStatusType status)
413  {
414  if (mDebugEnabled && mlogDestination==logDestination::DAEMON)
415  {
416  dlt_register_context_ll_ts(&handle, contextid, description, level, status);
417  }
418  else if (mDebugEnabled)
419  {
420  mMapContext.emplace(&handle,std::string(contextid));
421 
422  if (mlogDestination==logDestination::COMMAND_LINE)
423  std::cout << "\033[0;36m[DLT] Registering Context " << contextid << " , " << description << "\033[0m"<< std::endl;
424  else
425  mFilename << now() << " [DLT] Registering Context " << contextid << " , " << description << std::endl;
426  }
427  }
428 
429  bool CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context)
430  {
431  pthread_mutex_lock(&mMutex);
432  if (mlogDestination==logDestination::DAEMON)
433  {
434  if (!context)
435  context = &mDltContext;
436 
437  if(dlt_user_log_write_start(context, &mDltContextData, loglevel) <= 0)
438  {
439  pthread_mutex_unlock(&mMutex);
440  return false;
441  }
442  }
443  else
444  {
445  initNoDlt(loglevel,context);
446  }
447  return true;
448  }
449 
450  void CAmDltWrapper::send()
451  {
452  if (mlogDestination==logDestination::DAEMON)
453  {
454  dlt_user_log_write_finish(&mDltContextData);
455  }
456  else
457  {
458  if (mlogDestination==logDestination::COMMAND_LINE && mLogOn)
459  std::cout << mNoDltContextData.buffer.str().c_str() << std::endl;
460  else if (mLogOn)
461  mFilename << now() << mNoDltContextData.buffer.str().c_str() << std::endl;
462 
463  mNoDltContextData.buffer.str("");
464  mNoDltContextData.buffer.clear();
465  }
466  pthread_mutex_unlock(&mMutex);
467  }
468 
469  void CAmDltWrapper::append(const int8_t value)
470  {
471  if (mlogDestination==logDestination::DAEMON)
472  dlt_user_log_write_int8(&mDltContextData, value);
473  else
474  appendNoDLT(value);
475  }
476 
477  void CAmDltWrapper::append(const uint8_t value)
478  {
479  if (mlogDestination==logDestination::DAEMON)
480  dlt_user_log_write_uint8(&mDltContextData, value);
481  else
482  appendNoDLT(value);
483  }
484 
485  void CAmDltWrapper::append(const int16_t value)
486  {
487  if (mlogDestination==logDestination::DAEMON)
488  dlt_user_log_write_int16(&mDltContextData, value);
489  else
490  appendNoDLT(value);
491  }
492 
493  void CAmDltWrapper::append(const uint16_t value)
494  {
495  if (mlogDestination==logDestination::DAEMON)
496  dlt_user_log_write_uint16(&mDltContextData, value);
497  else
498  appendNoDLT(value);
499  }
500 
501  void CAmDltWrapper::append(const int32_t value)
502  {
503  if (mlogDestination==logDestination::DAEMON)
504  dlt_user_log_write_int32(&mDltContextData, value);
505  else
506  appendNoDLT(value);
507  }
508 
509  void CAmDltWrapper::append(const uint32_t value)
510  {
511  if (mlogDestination==logDestination::DAEMON)
512  dlt_user_log_write_uint32(&mDltContextData, value);
513  else
514  appendNoDLT(value);
515  }
516 
517  void CAmDltWrapper::append(const std::string& value)
518  {
519  append(value.c_str());
520  }
521 
522  void CAmDltWrapper::append(const bool value)
523  {
524  if (mlogDestination==logDestination::DAEMON)
525  dlt_user_log_write_bool(&mDltContextData, static_cast<uint8_t>(value));
526  else
527  appendNoDLT(value);
528  }
529 
530  void CAmDltWrapper::append(const int64_t value)
531  {
532  if (mlogDestination==logDestination::DAEMON)
533  dlt_user_log_write_int64(&mDltContextData, value);
534  else
535  appendNoDLT(value);
536  }
537 
538  void CAmDltWrapper::append(const uint64_t value)
539  {
540  if (mlogDestination==logDestination::DAEMON)
541  dlt_user_log_write_uint64(&mDltContextData, value);
542  else
543  appendNoDLT(value);
544  }
545 
546  void CAmDltWrapper::append(const std::vector<uint8_t> & data)
547  {
548  if (mlogDestination==logDestination::DAEMON)
549  dlt_user_log_write_raw(&mDltContextData,(void*)data.data(),data.size());
550  else
551  mNoDltContextData.buffer << data.data();
552  }
553 }
554 
555 #else //------------------------------------------------------------------------------------------------- no DLT !
556 
557  CAmDltWrapper::CAmDltWrapper(const char *appid, const char * description, const bool debugEnabled, const logDestination logDest, const std::string Filename,bool onlyError) :
558  mDebugEnabled(debugEnabled), //
559  mlogDestination(logDest), //
560  mFilename(NULL), //
561  mOnlyError(onlyError), //
562  mLogOn(true)
563  {
564  if (logDest==logDestination::DAEMON)
565  {
566  std::cout << "\033[0;31m[DLT] Cannot Use Daemon Logging, active in CMAKE! Using CommandLine\033[0m"<< std::endl;
567  mlogDestination=logDestination::COMMAND_LINE;
568  }
569  if (mDebugEnabled)
570  {
571  if (mlogDestination==logDestination::COMMAND_LINE)
572  std::cout << "\033[0;36m[DLT] Registering AppID " << appid << " , " << description << "\033[0m"<< std::endl;
573  else
574  {
575  mFilename.open(Filename, std::ofstream::out | std::ofstream::trunc);
576  if (!mFilename.is_open())
577  {
578  throw std::runtime_error("Cannot open file for logging");
579  }
580  mFilename << now() << "[DLT] Registering AppID " << appid << " , " << description << std::endl;
581  }
582  }
583  }
584 
586  {
587  if (mpDLTWrapper && mDebugEnabled && mlogDestination==logDestination::COMMAND_LINE)
588  {
589  mFilename.close();
590  }
591  }
592 
594  {}
595 
597  {}
598 
599  void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
600  {
601  if (mDebugEnabled)
602  {
603  mMapContext.emplace(&handle,std::string(contextid));
604 
605  if (mlogDestination==logDestination::COMMAND_LINE)
606  std::cout << "\033[0;36m[DLT] Registering Context " << contextid << " , " << description << "\033[0m"<< std::endl;
607  else
608  mFilename << now() << "[DLT] Registering Context " << contextid << " , " << description << std::endl;
609  }
610  }
611 
612  void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char * description,const DltLogLevelType level, const DltTraceStatusType status)
613  {
614  if (mDebugEnabled)
615  {
616  mMapContext.emplace(&handle,std::string(contextid));
617 
618  if (mlogDestination==logDestination::COMMAND_LINE)
619  std::cout << "\033[0;36m[DLT] Registering Context " << contextid << " , " << description << "\033[0m"<< std::endl;
620  else
621  mFilename << now() << " [DLT] Registering Context " << contextid << " , " << description << std::endl;
622  }
623  }
624 
626  {
627  pthread_mutex_lock(&mMutex);
628  initNoDlt(loglevel,context);
629  }
630 
632  {
633  if (mlogDestination==logDestination::COMMAND_LINE && mLogOn)
634  std::cout << mNoDltContextData.buffer.str().c_str() << std::endl;
635  else if (mLogOn)
636  mFilename << now() << mNoDltContextData.buffer.str().c_str() << std::endl;
637 
638  mNoDltContextData.buffer.str("");
639  mNoDltContextData.buffer.clear();
640  pthread_mutex_unlock(&mMutex);
641  }
642 
643  void CAmDltWrapper::append(const int8_t value)
644  {
645  appendNoDLT(value);
646  }
647 
648  void CAmDltWrapper::append(const uint8_t value)
649  {
650  appendNoDLT(value);
651  }
652 
653  void CAmDltWrapper::append(const int16_t value)
654  {
655  appendNoDLT(value);
656  }
657 
658  void CAmDltWrapper::append(const uint16_t value)
659  {
660  appendNoDLT(value);
661  }
662 
663  void CAmDltWrapper::append(const int32_t value)
664  {
665  appendNoDLT(value);
666  }
667 
668  void CAmDltWrapper::append(const uint32_t value)
669  {
670  appendNoDLT(value);
671  }
672 
673  void CAmDltWrapper::append(const std::string& value)
674  {
675  append(value.c_str());
676  }
677 
678  void CAmDltWrapper::append(const bool value)
679  {
680  appendNoDLT(value);
681  }
682 
683  void CAmDltWrapper::append(const int64_t value)
684  {
685  appendNoDLT(value);
686  }
687 
688  void CAmDltWrapper::append(const uint64_t value)
689  {
690  appendNoDLT(value);
691  }
692 
693  void CAmDltWrapper::append(const std::vector<uint8_t> & data)
694  {
695  mNoDltContextData.buffer << data.data();
696  }
697 }
698 #endif //WITH_DLT
699 
700 
Wraps around the dlt.
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
std::stringstream buffer
buffer for building log message
Log level off.
Definition: CAmDltWrapper.h:79
fatal system error
Definition: CAmDltWrapper.h:80
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.
SPDX license identifier: MPL-2.0.
bool init(DltLogLevelType loglevel, DltContext *context=NULL)
static CAmDltWrapper * instance()
get the Wrapper Instance
void registerContext(DltContext &handle, const char *contextid, const char *description)
register a context
This structure is used for every context used in an application.
Definition: CAmDltWrapper.h:42
informational
Definition: CAmDltWrapper.h:83
void appendNoDLT(T value)
error with impact to correct functionality
Definition: CAmDltWrapper.h:81
DltTraceStatusType
Definition of DLT trace status.
Definition: CAmDltWrapper.h:52
void unregisterContext(DltContext &handle)
warning, correct behaviour could not be ensured
Definition: CAmDltWrapper.h:82
DltLogLevelType
Definitions of DLT log level.
Definition: CAmDltWrapper.h:76