From 51d24e72d32d31e53c909453682201357a065099 Mon Sep 17 00:00:00 2001 From: Igor Kotrasinski Date: Tue, 8 May 2018 10:23:35 +0200 Subject: [PATCH] Clean up simulator logging levels Share log levels with log_level.h, refactor logging function. Change-Id: I5b05b6e37bb631609794ed05082aac13a4707a39 Signed-off-by: Igor Kotrasinski --- log/log.c | 187 +++++++++++++++++++++++++++++--------------------------------- log/log.h | 64 ++++++++++----------- 2 files changed, 116 insertions(+), 135 deletions(-) diff --git a/log/log.c b/log/log.c index 90948cb..bdbc890 100644 --- a/log/log.c +++ b/log/log.c @@ -28,12 +28,13 @@ #include #include +#define LOG_FMT "[%s][%s:%d]%s\n" + /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ -static int32_t gdebug_level = 0xff; +static LogLevel glog_level = SIMU_LOG_DEBUG; static int32_t gmodule_level = 0xfffffff; -//static const char* gtag = "TA_SDK"; /*----------------------------------------------------------------------------- * Functions @@ -50,18 +51,15 @@ static int32_t gmodule_level = 0xfffffff; * @return void */ __attribute__((visibility("default"))) -void SetDebugAndModuleLevel(IN const int32_t module_level, - IN const int32_t debug_level) +void setDebugAndModuleLevel(IN const int32_t module_level, + IN const LogLevel log_level) { if (module_level < UTILS || module_level > ALL_MODULES - || debug_level < INFO_LEVEL_LOG || debug_level > VERBOSE_LEVEL_LOG) + || log_level < SIMU_LOG_EMERG || log_level > SIMU_LOG_SILENT) return; - /* - * set global variables for module level and debug level - */ gmodule_level = module_level; - gdebug_level = debug_level; + glog_level = log_level; return; } @@ -73,27 +71,27 @@ void SetDebugAndModuleLevel(IN const int32_t module_level, * * @return const char pointer */ -const char *GetDebugLevel(IN int32_t dbg_level) +const char *getDebugLevelString(IN LogLevel dbg_level) { switch (dbg_level) { - case INFO_LEVEL_LOG: + case SIMU_LOG_EMERG: + return "EMERG"; + case SIMU_LOG_ALERT: + return "ALERT"; + case SIMU_LOG_CRIT: + return "CRIT"; + case SIMU_LOG_ERR: + return "ERR"; + case SIMU_LOG_WARNING: + return "WARNING"; + case SIMU_LOG_NOTICE: + return "NOTICE"; + case SIMU_LOG_INFO: return "INFO"; - - case PACKET_LEVEL_LOG: - return "PACKET"; - - case ERROR_LEVEL_LOG: - return "ERROR"; - - case DEBUG_LEVEL_LOG: + case SIMU_LOG_DEBUG: return "DEBUG"; - - case SECURED_LEVEL_LOG: - return "SECURED"; - - case VERBOSE_LEVEL_LOG: - return "VERBOSE"; - + case SIMU_LOG_SILENT: + return "SILENT"; default: return ""; } @@ -107,7 +105,7 @@ const char *GetDebugLevel(IN int32_t dbg_level) * * @return const char pointer */ -const char *GetModuleLevel(IN int32_t module_level) +const char *getModuleLevelString(IN int32_t module_level) { switch (module_level) { case UTILS: @@ -154,97 +152,88 @@ const char *GetModuleLevel(IN int32_t module_level) * @return void */ -static void PrintLog(IN const char *function_name, IN const int32_t line_no, - IN int32_t module_level, IN int32_t debug_level, - IN const char *message, va_list variable_list) +#ifdef __TIZEN__ +static log_priority logLevelToDlogLevel(LogLevel log_level) { -#ifndef _ANDROID_NDK - - if (0 == (module_level & gmodule_level) - || 0 == (debug_level & gdebug_level)) - return; - + switch (log_level) { + case SIMU_LOG_EMERG: + case SIMU_LOG_ALERT: + case SIMU_LOG_CRIT: + return DLOG_FATAL; + case SIMU_LOG_ERR: + return DLOG_ERROR; + case SIMU_LOG_WARNING: + return DLOG_WARN; + case SIMU_LOG_NOTICE: + case SIMU_LOG_INFO: + return DLOG_INFO; + case SIMU_LOG_DEBUG: + return DLOG_DEBUG; + case SIMU_LOG_SILENT: + default: + return DLOG_SILENT; + } +} #endif - const char *module = GetModuleLevel(module_level); -#if defined(__TIZEN__) - char buf[512] = {0,}; - vsnprintf(buf, 511, message, variable_list); - - switch (debug_level) { - case INFO_LEVEL_LOG: - LOG(LOG_INFO, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); - break; - - case PACKET_LEVEL_LOG: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, - buf); - break; - - case ERROR_LEVEL_LOG: - LOG(LOG_ERROR, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, - buf); - break; - - case DEBUG_LEVEL_LOG: - case SECURED_LEVEL_LOG: - case VERBOSE_LEVEL_LOG: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, - buf); - break; +#ifdef _ANDROID_NDK +static android_LogPriority logLevelToAndroidLogLevel(LogLevel log_level) +{ + switch (log_level) { + case SIMU_LOG_EMERG: + case SIMU_LOG_ALERT: + case SIMU_LOG_CRIT: + return ANDROID_SIMU_LOG_FATAL; + case SIMU_LOG_ERR: + return ANDROID_SIMU_LOG_ERROR; + case SIMU_LOG_WARNING: + return ANDROID_SIMU_LOG_WARN; + case SIMU_LOG_NOTICE: + case SIMU_LOG_INFO: + return ANDROID_SIMU_LOG_INFO; + case SIMU_LOG_DEBUG: + return ANDROID_SIMU_LOG_DEBUG; + case SIMU_LOG_SILENT: default: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, - buf); - break; + return ANDROID_SIMU_LOG_SILENT; } +} +#endif -#elif defined(_ANDROID_NDK) +static void PrintLog(IN const char *function_name, IN const int32_t line_no, + IN int32_t module_level, IN LogLevel log_level, + IN const char *message, va_list variable_list) +{ + if (!(module_level & gmodule_level) + || log_level > glog_level) + return; + + const char *module = getModuleLevelString(module_level); char buf[512] = {'\0'}; vsnprintf(buf, sizeof(buf), message, variable_list); - - switch (debug_level) { - case INFO_LEVEL_LOG: - __android_log_print(ANDROID_LOG_INFO, gtag, "[%s] %s: %d: %s\n", module, - function_name, line_no, buf); - break; - - case ERROR_LEVEL_LOG: - __android_log_print(ANDROID_LOG_ERROR, gtag, "[%s] %s: %d: %s\n", module, - function_name, line_no, buf); - break; - - case DEBUG_LEVEL_LOG: - __android_log_print(ANDROID_LOG_WARN, gtag, "[%s] %s: %d: %s\n", module, - function_name, line_no, buf); - break; - - case SECURED_LEVEL_LOG: - __android_log_print(ANDROID_LOG_DEBUG, gtag, "[%s] %s: %d: %s\n", module, - function_name, line_no, buf); - break; - - default: - // __android_log_print(ANDROID_LOG_VERBOSE, gtag, "[%s]%s: %d: %s\n", module, function_name, line_no, buf); - break; - } - +#if defined(__TIZEN__) + log_priority prio = logLevelToDlogLevel(log_level); + dlog_print(prio, TA_SDK_TAG, LOG_FMT, + module, function_name, line_no, buf); +#elif defined(_ANDROID_NDK) + android_LogPriority prio = logLevelToAndroidLogLevel(log_level); + __android_log_print(prio, TA_SDK_TAG, LOG_FMT, + module, function_name, line_no, buf); #else - const char *severity = GetDebugLevel(debug_level); - printf("[%s] [%s] %s: %d: ", module, severity, function_name, line_no); - vprintf(message, variable_list); - printf("\n"); + const char *severity = getDebugLevelString(log_level); + printf("[%s]"LOG_FMT , severity, module, function_name, line_no, buf); #endif } __attribute__((visibility("default"))) void PrintSimulatorLog(IN const char *function_name, IN const int32_t line_no, - IN int32_t module_level, IN int32_t debug_level, + IN int32_t module_level, IN int32_t log_level, IN const char *message, ...) { #ifdef _LOGGING va_list args; va_start(args, message); - PrintLog(function_name, line_no, module_level, debug_level, message, + PrintLog(function_name, line_no, module_level, log_level, message, args); va_end(args); #endif diff --git a/log/log.h b/log/log.h index bc3b625..ab4d85d 100644 --- a/log/log.h +++ b/log/log.h @@ -55,18 +55,6 @@ typedef int timer_t; #endif /* - * Enum to identify Debug level - */ -typedef enum { - INFO_LEVEL_LOG = 0x01, - PACKET_LEVEL_LOG = 0x02, - ERROR_LEVEL_LOG = 0x04, - DEBUG_LEVEL_LOG = 0x08, - SECURED_LEVEL_LOG = 0x10, - VERBOSE_LEVEL_LOG = 0xFF, -} DebugLevel; - -/* * Enum to identify Module name */ typedef enum { @@ -75,41 +63,45 @@ typedef enum { TEEC_LIB = 0x04, TEE_STUB = 0x08, TEST = 0x10, - SSF_LIB = 0x11, - OSA_LIB = 0x12, + SSF_LIB = 0x20, + OSA_LIB = 0x40, ALL_MODULES = 0xFFFFFFF, } ModuleLevel; +typedef enum { + SIMU_LOG_EMERG = 0, + SIMU_LOG_ALERT, + SIMU_LOG_CRIT, + SIMU_LOG_ERR, + SIMU_LOG_WARNING, + SIMU_LOG_NOTICE, + SIMU_LOG_INFO, + SIMU_LOG_DEBUG, + SIMU_LOG_SILENT, +} LogLevel; + // if we use dlog, we need to replace its macros with our own #ifdef __TIZEN__ #ifdef LOGE #undef LOGE #endif - #ifdef LOGV - #undef LOGV - #endif - #ifdef LOGD - #undef LOGD + #ifdef LOGW + #undef LOGW #endif #ifdef LOGI #undef LOGI #endif - #ifdef LOGS - #undef LOGS - #endif - #ifdef LOGP - #undef LOGP + #ifdef LOGD + #undef LOGD #endif #endif // __TIZEN__ -#define _LOG(module_level, debug_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, debug_level, ##__VA_ARGS__) +#define _LOG(module_level, log_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, log_level, ##__VA_ARGS__) -#define LOGE(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, ERROR_LEVEL_LOG, ##__VA_ARGS__) -#define LOGV(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, VERBOSE_LEVEL_LOG, ##__VA_ARGS__) -#define LOGD(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, DEBUG_LEVEL_LOG, ##__VA_ARGS__) -#define LOGI(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, SECURED_LEVEL_LOG, ##__VA_ARGS__) -#define LOGS(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, INFO_LEVEL_LOG, ##__VA_ARGS__) -#define LOGP(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, PACKET_LEVEL_LOG, ##__VA_ARGS__) +#define LOGE(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, SIMU_LOG_ERR, ##__VA_ARGS__) +#define LOGW(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, SIMU_LOG_WARNING, ##__VA_ARGS__) +#define LOGI(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, SIMU_LOG_INFO, ##__VA_ARGS__) +#define LOGD(module_level, ...) PrintSimulatorLog(__FUNCTION__, __LINE__, module_level, SIMU_LOG_DEBUG, ##__VA_ARGS__) #if defined(__cplusplus) extern "C" { @@ -123,7 +115,7 @@ extern "C" { * * @return const char pointer */ -const char* GetDebugLevel(IN int32_t dbg_level); +const char* getDebugLevelString(IN LogLevel dbg_level); /* * This method is to get module for which debug is set @@ -133,7 +125,7 @@ const char* GetDebugLevel(IN int32_t dbg_level); * * @return const char pointer */ -const char* GetModuleLevel(IN int32_t mdl_level); +const char* getModuleLevelString(IN int32_t mdl_level); /* * This method is used to set module level and debug level to debug @@ -146,8 +138,8 @@ const char* GetModuleLevel(IN int32_t mdl_level); * * @return void */ -void SetDebugAndModuleLevel(IN const int32_t module_level, - IN const int32_t debug_level); +void setDebugAndModuleLevel(IN const int32_t module_level, + IN const LogLevel log_level); /* * This method is used to print the simulator debug logs @@ -170,7 +162,7 @@ void SetDebugAndModuleLevel(IN const int32_t module_level, * @return void */ void PrintSimulatorLog(IN const char* function_name, IN const int32_t line_no, - IN int32_t module_level, IN int32_t debug_level, + IN int32_t module_level, IN int32_t log_level, IN const char* message, ...); #if defined(__cplusplus) } // extern "C" -- 2.7.4