diff options
Diffstat (limited to 'AudioManagerUtilities/include/CAmDltWrapper.h')
-rw-r--r-- | AudioManagerUtilities/include/CAmDltWrapper.h | 299 |
1 files changed, 164 insertions, 135 deletions
diff --git a/AudioManagerUtilities/include/CAmDltWrapper.h b/AudioManagerUtilities/include/CAmDltWrapper.h index 4d725d8..b6772d9 100644 --- a/AudioManagerUtilities/include/CAmDltWrapper.h +++ b/AudioManagerUtilities/include/CAmDltWrapper.h @@ -29,67 +29,67 @@ #include "audiomanagertypes.h" #ifdef WITH_DLT - #include <dlt.h> +# include <dlt.h> #else - #include <stdint.h> - #include <sstream> +# include <stdint.h> +# include <sstream> - #define DLT_USER_BUF_MAX_SIZE 2048 +# define DLT_USER_BUF_MAX_SIZE 2048 - /** - * This structure is used for every context used in an application. - */ - typedef struct - { - char contextID[4]; /**< context id */ - int32_t log_level_pos; /**< offset in user-application context field */ - int32_t log_level_user; /** any message above this log level is not logged */ - } DltContext; +/** + * This structure is used for every context used in an application. + */ +typedef struct +{ + char contextID[4]; /**< context id */ + int32_t log_level_pos; /**< offset in user-application context field */ + int32_t log_level_user; /** any message above this log level is not logged */ +} DltContext; - /** - * Definition of DLT trace status - */ - typedef enum - { - DLT_TRACE_STATUS_DEFAULT = -1, /**< Default trace status */ - DLT_TRACE_STATUS_OFF = 0x00, /**< Trace status: Off */ - DLT_TRACE_STATUS_ON = 0x01 /**< Trace status: On */ - } DltTraceStatusType; +/** + * Definition of DLT trace status + */ +typedef enum +{ + DLT_TRACE_STATUS_DEFAULT = -1, /**< Default trace status */ + DLT_TRACE_STATUS_OFF = 0x00, /**< Trace status: Off */ + DLT_TRACE_STATUS_ON = 0x01 /**< Trace status: On */ +} DltTraceStatusType; - /** - * This structure is used for context data used in an application. - */ - typedef struct - { - DltContext *handle; /**< pointer to DltContext */ - std::stringstream buffer; /**< buffer for building log message*/ - int32_t log_level; /**< log level */ - int32_t trace_status; /**< trace status */ - int32_t args_num; /**< number of arguments for extended header*/ - uint8_t mcnt; /**< message counter */ - char* context_description; /**< description of context */ - } DltContextData; +/** + * This structure is used for context data used in an application. + */ +typedef struct +{ + DltContext *handle; /**< pointer to DltContext */ + std::stringstream buffer; /**< buffer for building log message*/ + int32_t log_level; /**< log level */ + int32_t trace_status; /**< trace status */ + int32_t args_num; /**< number of arguments for extended header*/ + uint8_t mcnt; /**< message counter */ + char *context_description; /**< description of context */ +} DltContextData; - /** - * Definitions of DLT log level - */ - typedef enum - { - DLT_LOG_DEFAULT = -1, /**< Default log level */ - DLT_LOG_OFF = 0x00, /**< Log level off */ - DLT_LOG_FATAL = 0x01, /**< fatal system error */ - DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */ - DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */ - DLT_LOG_INFO = 0x04, /**< informational */ - DLT_LOG_DEBUG = 0x05, /**< debug */ - DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */ - } DltLogLevelType; - - #define DLT_DEFAULT_LOG_LEVEL DLT_LOG_INFO - #define DLT_DECLARE_CONTEXT(CONTEXT) \ +/** + * Definitions of DLT log level + */ +typedef enum +{ + DLT_LOG_DEFAULT = -1, /**< Default log level */ + DLT_LOG_OFF = 0x00, /**< Log level off */ + DLT_LOG_FATAL = 0x01, /**< fatal system error */ + DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */ + DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */ + DLT_LOG_INFO = 0x04, /**< informational */ + DLT_LOG_DEBUG = 0x05, /**< debug */ + DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */ +} DltLogLevelType; + +# define DLT_DEFAULT_LOG_LEVEL DLT_LOG_INFO +# define DLT_DECLARE_CONTEXT(CONTEXT) \ DltContext CONTEXT; - #define DLT_IMPORT_CONTEXT(CONTEXT) \ +# define DLT_IMPORT_CONTEXT(CONTEXT) \ extern DltContext CONTEXT; #endif // WITH_DLT @@ -112,13 +112,13 @@ public: */ typedef struct { - DltContext *handle; /**< pointer to DltContext */ - std::stringstream buffer; /**< buffer for building log message*/ - int32_t log_level; /**< log level */ - int32_t trace_status; /**< trace status */ - int32_t args_num; /**< number of arguments for extended header*/ - uint8_t mcnt; /**< message counter */ - char* context_description; /**< description of context */ + DltContext *handle; /**< pointer to DltContext */ + std::stringstream buffer; /**< buffer for building log message*/ + int32_t log_level; /**< log level */ + int32_t trace_status; /**< trace status */ + int32_t args_num; /**< number of arguments for extended header*/ + uint8_t mcnt; /**< message counter */ + char *context_description; /**< description of context */ } NoDltContextData; /* @@ -126,9 +126,9 @@ public: */ enum logDestination { - DAEMON=0, //!< logging with the DLT daemon - COMMAND_LINE=1, //!< logging with commandline - FILE_OUT =2 //!< logging into a file + DAEMON = 0, //!< logging with the DLT daemon + COMMAND_LINE = 1, //!< logging with commandline + FILE_OUT = 2 //!< logging into a file }; /** @@ -140,39 +140,42 @@ public: * @param Filename the filename with absolute path where the log shall be written. only needed if logDest==FILE_OUT * @param onlyError if set to true, only errors will be logged. just valid for commandline and file logs, default value = false */ - 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); - + 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); + /** * get the Wrapper Instance */ - static CAmDltWrapper* instance(); + static CAmDltWrapper *instance(); /** * register a context */ - void registerContext(DltContext& handle, const char *contextid, const char * description); - void registerContext(DltContext& handle, const char *contextid, const char * description, const DltLogLevelType level, const DltTraceStatusType status); - void unregisterContext(DltContext& handle); + void registerContext(DltContext &handle, const char *contextid, const char *description); + void registerContext(DltContext &handle, const char *contextid, const char *description, const DltLogLevelType level, const DltTraceStatusType status); + void unregisterContext(DltContext &handle); bool getEnabled(); + ~CAmDltWrapper(); - bool init(DltLogLevelType loglevel, DltContext* context = NULL); + bool init(DltLogLevelType loglevel, DltContext *context = NULL); + bool checkLogLevel(DltLogLevelType logLevel) { #ifdef WITH_DLT - #ifdef DLT_IS_LOG_LEVEL_ENABLED +# ifdef DLT_IS_LOG_LEVEL_ENABLED if (mlogDestination == logDestination::DAEMON) { return (dlt_user_is_logLevel_enabled(&mDltContext, logLevel) == DLT_RETURN_TRUE); } - #else +# else (void)logLevel; - #endif +# endif // ifdef DLT_IS_LOG_LEVEL_ENABLED return true; -#else +#else // ifdef WITH_DLT return (logLevel <= mDltContext.log_level_user); -#endif +#endif // ifdef WITH_DLT } + void deinit(); void send(); void append(const int8_t value); @@ -183,48 +186,51 @@ public: void append(const uint32_t value); void append(const uint64_t value); void append(const int64_t value); - void append(const std::string& value); + void append(const std::string &value); void append(const bool value); - void append(const std::vector<uint8_t> & data); - - template<class T> void appendNoDLT(T value) + void append(const std::vector<uint8_t> &data); + + template<class T> + void appendNoDLT(T value) { - mNoDltContextData.buffer << value <<" "; + mNoDltContextData.buffer << value << " "; } // specialization for const char* - template<typename T = const char*> void append(const char* value) + template<typename T = const char *> + void append(const char *value) { - #ifdef WITH_DLT - if (mlogDestination == logDestination::DAEMON) - { - dlt_user_log_write_string(&mDltContextData, value); - } - else - { - mNoDltContextData.buffer << std::string(value); - } - #else +#ifdef WITH_DLT + if (mlogDestination == logDestination::DAEMON) + { + dlt_user_log_write_string(&mDltContextData, value); + } + else + { mNoDltContextData.buffer << std::string(value); - #endif //WITH_DLT + } +#else // ifdef WITH_DLT + mNoDltContextData.buffer << std::string(value); +#endif // WITH_DLT } private: - static const std::vector<const char*> mStr_error; - static const std::vector<const char*> mStr_sourceState; - static const std::vector<const char*> mStr_MuteState; - static const std::vector<const char*> mStr_DomainState; - static const std::vector<const char*> mStr_ConnectionState; - static const std::vector<const char*> mStr_Availability; - static const std::vector<const char*> mStr_Interrupt; - static const std::vector<const char*> mStr_Handle; - static const std::vector<const char*> mStr_NotificationStatus; + static const std::vector<const char *> mStr_error; + static const std::vector<const char *> mStr_sourceState; + static const std::vector<const char *> mStr_MuteState; + static const std::vector<const char *> mStr_DomainState; + static const std::vector<const char *> mStr_ConnectionState; + static const std::vector<const char *> mStr_Availability; + static const std::vector<const char *> mStr_Interrupt; + static const std::vector<const char *> mStr_Handle; + static const std::vector<const char *> mStr_NotificationStatus; public: // specialization for const am_Error_e - template<typename T = const am_Error_e> void append(const am_Error_e value) + template<typename T = const am_Error_e> + void append(const am_Error_e value) { if (static_cast<std::size_t>(value) >= mStr_error.size()) { @@ -232,11 +238,13 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_error[value]); } // specialization for const am_Error_e - template<typename T = const am_SourceState_e> void append(const am_SourceState_e value) + template<typename T = const am_SourceState_e> + void append(const am_SourceState_e value) { if (static_cast<std::size_t>(value) >= mStr_sourceState.size()) { @@ -244,11 +252,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_sourceState[value]); } - - template<typename T = const am_MuteState_e> void append(const am_MuteState_e value) + template<typename T = const am_MuteState_e> + void append(const am_MuteState_e value) { if (static_cast<std::size_t>(value) >= mStr_MuteState.size()) { @@ -256,10 +265,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_MuteState[value]); } - template<typename T = const am_DomainState_e> void append(const am_DomainState_e value) + template<typename T = const am_DomainState_e> + void append(const am_DomainState_e value) { if (static_cast<std::size_t>(value) >= mStr_DomainState.size()) { @@ -267,10 +278,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_DomainState[value]); } - template<typename T = const am_ConnectionState_e> void append(const am_ConnectionState_e value) + template<typename T = const am_ConnectionState_e> + void append(const am_ConnectionState_e value) { if (static_cast<std::size_t>(value) >= mStr_ConnectionState.size()) { @@ -278,10 +291,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_ConnectionState[value]); } - template<typename T = const am_Availability_e> void append(const am_Availability_e value) + template<typename T = const am_Availability_e> + void append(const am_Availability_e value) { if (static_cast<std::size_t>(value) >= mStr_Availability.size()) { @@ -289,10 +304,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_Availability[value]); } - template<typename T = const am_InterruptState_e> void append(const am_InterruptState_e value) + template<typename T = const am_InterruptState_e> + void append(const am_InterruptState_e value) { if (static_cast<std::size_t>(value) >= mStr_Interrupt.size()) { @@ -300,10 +317,12 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_Interrupt[value]); } - template<typename T = const am_Handle_e> void append(const am_Handle_e value) + template<typename T = const am_Handle_e> + void append(const am_Handle_e value) { if (static_cast<std::size_t>(value) >= mStr_Handle.size()) { @@ -311,16 +330,19 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_Handle[value]); } - - template<typename T = const am_Handle_s> void append(const am_Handle_s value) + + template<typename T = const am_Handle_s> + void append(const am_Handle_s value) { - append (value.handleType); - append (value.handle); + append(value.handleType); + append(value.handle); } - template<typename T = const am_NotificationStatus_e> void append(const am_NotificationStatus_e value) + template<typename T = const am_NotificationStatus_e> + void append(const am_NotificationStatus_e value) { if (static_cast<std::size_t>(value) >= mStr_NotificationStatus.size()) { @@ -328,11 +350,13 @@ public: append(static_cast<uint16_t>(value)); return; } + append(mStr_NotificationStatus[value]); } // Template to print unknown pointer types with their address - template<typename T> void append(T* value) + template<typename T> + void append(T *value) { std::ostringstream ss; ss << "0x" << std::hex << (uint64_t)value; @@ -340,7 +364,8 @@ public: } // Template to print unknown types - template<typename T> void append(T value) + template<typename T> + void append(T value) { std::ostringstream ss; ss << std::dec << value; @@ -349,7 +374,8 @@ public: // Template parameter pack to generate recursive code void append(void) {} - template<typename T, typename... TArgs> void append(T value, TArgs... args) + template<typename T, typename... TArgs> + void append(T value, TArgs... args) { this->append(value); this->append(args...); @@ -359,19 +385,20 @@ private: /** * private contructor */ - 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 - bool initNoDlt(DltLogLevelType loglevel, DltContext* context); + 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 + bool initNoDlt(DltLogLevelType loglevel, DltContext *context); std::string now(); - DltContext mDltContext; //!< the default context - DltContextData mDltContextData; //!< contextdata - NoDltContextData mNoDltContextData; //!<contextdata for std out logging - std::map<DltContext*,std::string> mMapContext; //!< a Map for all registered context - bool mDebugEnabled; //!< debug Enabled or not - logDestination mlogDestination; //!< The log destination - std::ofstream mFilename; //!< Filename for logging - bool mOnlyError; //!< Only if Log Level is above Error - bool mLogOn; //!< Used to keep track if currently logging is on - static CAmDltWrapper* mpDLTWrapper; //!< pointer to the wrapper instance + + DltContext mDltContext; //!< the default context + DltContextData mDltContextData; //!< contextdata + NoDltContextData mNoDltContextData; //!< contextdata for std out logging + std::map<DltContext *, std::string> mMapContext; //!< a Map for all registered context + bool mDebugEnabled; //!< debug Enabled or not + logDestination mlogDestination; //!< The log destination + std::ofstream mFilename; //!< Filename for logging + bool mOnlyError; //!< Only if Log Level is above Error + bool mLogOn; //!< Used to keep track if currently logging is on + static CAmDltWrapper *mpDLTWrapper; //!< pointer to the wrapper instance static pthread_mutex_t mMutex; }; @@ -384,17 +411,19 @@ private: * @param ... */ template<typename T, typename... TArgs> -void log(DltContext* const context, DltLogLevelType loglevel, T value, TArgs... args) +void log(DltContext *const context, DltLogLevelType loglevel, T value, TArgs... args) { - CAmDltWrapper* inst(CAmDltWrapper::instance()); + CAmDltWrapper *inst(CAmDltWrapper::instance()); if (!inst->getEnabled()) { return; } + if (!inst->init(loglevel, context)) { return; } + inst->append(value); inst->append(args...); inst->send(); @@ -430,7 +459,7 @@ void logInfo(T value, TArgs... args) template<typename T, typename... TArgs> void logError(T value, TArgs... args) { - log(NULL, DLT_LOG_ERROR,value,args...); + log(NULL, DLT_LOG_ERROR, value, args...); } /** @@ -441,7 +470,7 @@ void logError(T value, TArgs... args) template<typename T, typename... TArgs> void logWarning(T value, TArgs... args) { - log(NULL, DLT_LOG_WARN,value,args...); + log(NULL, DLT_LOG_WARN, value, args...); } /** @@ -452,7 +481,7 @@ void logWarning(T value, TArgs... args) template<typename T, typename... TArgs> void logVerbose(T value, TArgs... args) { - log(NULL, DLT_LOG_VERBOSE,value,args...); + log(NULL, DLT_LOG_VERBOSE, value, args...); } } |