Log: Introducing diagnostic callback injection
authorMichael Schuldt <michael.schuldt@bmw.de>
Thu, 30 Aug 2012 08:58:59 +0000 (10:58 +0200)
committerMichael Schuldt <michael.schuldt@bmw.de>
Thu, 30 Aug 2012 08:58:59 +0000 (10:58 +0200)
- On the OEM side it is needed to introduce diagnostic callbacks
- Therefore it is possible to control the platform specific plugIn
  by message injection from the dlt-deamon
- Currently the functionality is complete working WITH_DLT=ON
  otherwise only the callback registration is working, but no
  callback will occur.

LayerManagerUtils/include/Log.h
LayerManagerUtils/src/Log.cpp

index df47bbc..df3f13d 100644 (file)
@@ -40,7 +40,7 @@
 #include <iostream>
 #include <fstream>
 #include <pthread.h>
-
+#include <map>
 typedef enum {
     LOG_DISABLED = 0,
     LOG_ERROR = 1,
@@ -56,19 +56,35 @@ typedef enum {
   during the compiliation phase*/
 typedef void* LogContext;
 
+/* Diagnostic Injection Callback function to wrap dlt_injections for usage with c++ class names */
+typedef void (*diagnosticInjectionCallback)(unsigned int module_id, void *data, unsigned int length, void* userdata);
+
 class Log
 {
 public:
+    
+    typedef struct diagnosticCallbackData_t
+    {
+        unsigned int module_id;
+        void *userdata;
+        diagnosticInjectionCallback diagFunc;
+    } diagnosticCallbackData;
+    
+    typedef std::map<unsigned int, diagnosticCallbackData*> DiagnosticCallbackMap;
+    
     virtual ~Log();
     static LOG_MODES fileLogLevel;
     static LOG_MODES consoleLogLevel;
     static LOG_MODES dltLogLevel;
     static Log* getInstance();
+    static DiagnosticCallbackMap* getDiagnosticCallbackMap(){return getInstance()->m_diagnosticCallbackMap;};
     void warning (LogContext logContext, const std::string& moduleName, const std::basic_string<char>& output);
     void info (LogContext logContext, const std::string& moduleName, const std::basic_string<char>& output);
     void error (LogContext logContext, const std::string& moduleName, const std::basic_string<char>& output);
     void debug (LogContext logContext, const std::string& moduleName, const std::basic_string<char>& output);
     void log(LogContext logContext, LOG_MODES logMode, const std::string& moduleName, const std::basic_string<char>& output);
+    void registerDiagnosticInjectionCallback( unsigned int module_id, diagnosticInjectionCallback diagFunc, void* userdata );
+    void unregisterDiagnosticInjectionCallback( unsigned int module_id );
     LogContext getLogContext();
     static void closeInstance();
 private:
@@ -81,7 +97,9 @@ private:
     std::ofstream* m_fileStream;
     pthread_mutex_t m_LogBufferMutex;
     LogContext m_logContext;
-    static Log* m_instance;
+    static DiagnosticCallbackMap* m_diagnosticCallbackMap;
+    
+    static Log* m_instance;    
 };
 
 //#define LOG_ERROR(logcontext,module, message)
index fffa370..7d57a60 100644 (file)
@@ -43,6 +43,7 @@
 #endif
 
 Log* Log::m_instance = NULL;
+Log::DiagnosticCallbackMap* Log::m_diagnosticCallbackMap = NULL;
 LOG_MODES Log::fileLogLevel = LOG_DISABLED;
 LOG_MODES Log::consoleLogLevel = LOG_INFO;
 #ifdef WITH_DLT
@@ -51,11 +52,13 @@ LOG_MODES Log::dltLogLevel = LOG_DEBUG;
 LOG_MODES Log::dltLogLevel = LOG_DISABLED;
 #endif
 
+
 Log::Log()
 {
     // TODO Auto-generated constructor stub
     m_fileStream = new std::ofstream("/tmp/LayerManagerService.log");
     pthread_mutex_init(&m_LogBufferMutex, NULL);
+    Log::m_diagnosticCallbackMap = new Log::DiagnosticCallbackMap;
 #ifdef WITH_DLT
     m_logContext = new DltContext;    
     DLT_REGISTER_APP("LMSA","LayerManagerService");
@@ -72,6 +75,7 @@ Log* Log::getInstance()
     if ( m_instance == NULL ) 
     {
         m_instance = new Log();
+        
     }
     return m_instance;
 }
@@ -91,7 +95,10 @@ Log::~Log()
 #ifdef WITH_DLT
     DLT_UNREGISTER_CONTEXT(*((DltContext*)m_logContext));
     delete ((DltContext*)m_logContext);
-#endif    
+    DLT_UNREGISTER_APP();
+#endif
+    delete m_diagnosticCallbackMap;
+    m_diagnosticCallbackMap = NULL;
     m_logContext = NULL;
 }
 
@@ -187,6 +194,38 @@ LogContext Log::getLogContext()
 {
     return m_logContext;
 }
+#ifdef WITH_DLT
+int dlt_injection_callback(unsigned int module_id, void *data, unsigned int length)
+{
+    LOG_DEBUG("LOG","Injection for service " << module_id << " called");
+    Log::diagnosticCallbackData *cbData = (*Log::getDiagnosticCallbackMap())[module_id];
+    if ( NULL != cbData) 
+    {
+        cbData->diagFunc(module_id, data, length, cbData->userdata);
+    }
+    return 0;
+}
+#endif
+
+
+void Log::registerDiagnosticInjectionCallback( unsigned int module_id, diagnosticInjectionCallback diagFunc, void* userdata )
+{
+    Log::diagnosticCallbackData *cbData = new Log::diagnosticCallbackData;
+    cbData->module_id = module_id;
+    cbData->userdata = userdata;
+    cbData->diagFunc = diagFunc;
+    (*m_diagnosticCallbackMap)[module_id]=cbData;
+#ifdef WITH_DLT    
+    DLT_REGISTER_INJECTION_CALLBACK(*(DltContext*)m_logContext, module_id, dlt_injection_callback);
+#endif
+}
+    
+void Log::unregisterDiagnosticInjectionCallback( unsigned int module_id )
+{
+    m_diagnosticCallbackMap->erase(module_id);
+}
+
+
 
 #ifdef WITH_DLT    
 void Log::LogToDltDaemon(LogContext logContext, LOG_MODES logMode, const std::string& moduleName, const std::basic_string<char>& output)