/* * Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2020. All rights reserved. * Description: Logcat version of the slog function implementation * Author: Hisilicon * Create: 2020-10-9 */ #include #include #include #include "securec.h" #include "android_slog.h" #define UNUSED(x) ((void)(x)) #ifdef __cplusplus #ifndef LOG_CPP extern "C" { #endif #endif // __cplusplus #define ONE_ACT_NO_LOG(expr, action) \ if (expr) { \ action; \ } \ static int g_isInited = NO_INITED; __attribute__ ((constructor)) void dlog_init(); static const ModuleInfo *GetModuleInfos() { return (const ModuleInfo *)g_moduleInfo; } static int GetLevelByModuleId(const int moduleId) { const ModuleInfo *set = g_moduleInfo; if (moduleId >= 0 && moduleId < INVLID_MOUDLE_ID) { return set[moduleId].moduleLevel; } return MODULE_DEFAULT_LOG_LEVEL; } /** * @brief dlog_init: libslog.so initialize * @return: void */ void dlog_init(void) { char globalLevel[PROPERTY_VALUE_MAX] = {0}; property_get("persist.vendor.slog.global.level", globalLevel, "3"); g_globalLogLevel = atoi(globalLevel); char eventValue[PROPERTY_VALUE_MAX] = {0}; property_get("persist.vendor.slog.event", eventValue, "1"); g_enableEvent = atoi(eventValue); g_isInited = INITED; } /** * @brief dlog_getlevel: get module loglevel * @param [in]moduleId: moudule Id eg: CCE * @param [in/out]enableEvent: point to enableEvent to record enableEvent * @return: module level(0: debug, 1: info, 2: warning, 3: error, 4: null output) */ int dlog_getlevel(int moduleId, int *enableEvent) { if (enableEvent != NULL) { *enableEvent = g_enableEvent; } int moduleLevel = GetLevelByModuleId(moduleId); if (moduleLevel < LOG_MIN_LEVEL || moduleLevel > LOG_MAX_LEVEL) { moduleLevel = g_globalLogLevel; } return moduleLevel; } /** * @brief dlog_setlevel: set module loglevel and enableEvent * @param [in]moduleId: moudule id(see slog.h, eg: CCE), -1: all modules, others: invalid * @param [in]level: log level(0: debug, 1: info, 2: warning, 3: error, 4: null output) * @param [in]enableEvent: 1: enable; 0: disable, others:invalid * @return: 0: SUCCEED, others: FAILED */ int dlog_setlevel(int moduleId, int level, int enableEvent) { int ret = SYS_OK; if ((enableEvent == TRUE) || (enableEvent == FALSE)) { g_enableEvent = enableEvent; } else { ret = SYS_ERROR; } if ((level < LOG_MIN_LEVEL) || (level > LOG_MAX_LEVEL)) { return SYS_ERROR; } if (moduleId == INVALID) { g_globalLogLevel = level; } return ret; } /** * @brief CheckLogLevel: check log allow output or not * @param [in]moduleId: module Id * @param [in]level: log level * @return: TRUE/FALSE */ int CheckLogLevel(int moduleId, int level) { if (level == DLOG_TRACE || level == DLOG_OPLOG) { return TRUE; } // event log contrl if (level == DLOG_EVENT) { return (g_enableEvent == TRUE) ? TRUE : FALSE; } // level parameter check if (level < LOG_MIN_LEVEL || level >= LOG_MAX_LEVEL) { return FALSE; } const ModuleInfo *moduleInfos = GetModuleInfos(); // get module loglevel by moduleId int moduleLevel = MODULE_DEFAULT_LOG_LEVEL; if (moduleId >= 0 && moduleId < INVLID_MOUDLE_ID) { moduleLevel = moduleInfos[moduleId].moduleLevel; if (moduleLevel < LOG_MIN_LEVEL || moduleLevel > LOG_MAX_LEVEL) { moduleLevel = g_globalLogLevel; } } else { moduleLevel = g_globalLogLevel; } // check module loglevel and log control, check time diff to make switch back to false if (level >= moduleLevel) { return TRUE; } return FALSE; } /** * @brief DlogSetAttr: set log attr * @param [in]logAttr: attr info, include pid, process type and device id * @return: 0: SUCCEED, others: FAILED */ int DlogSetAttr(LogAttr logAttr) { UNUSED(logAttr); return 0; } void DlogErrorInner(int moduleId, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, DLOG_ERROR); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_ERROR, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } void DlogWarnInner(int moduleId, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, DLOG_WARN); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_WARN, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } void DlogInfoInner(int moduleId, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, DLOG_INFO); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } void DlogDebugInner(int moduleId, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, DLOG_DEBUG); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_DEBUG, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } void DlogEventInner(int moduleId, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, DLOG_EVENT); ONE_ACT_NO_LOG(result == FALSE, return); char msg[MSG_LENGTH] = {0}; int len = static_cast(strlen(msg)); int err; err = snprintf_s(msg + len, MSG_LENGTH - len, MSG_LENGTH - len - 1, " "); ONE_ACT_NO_LOG(err == -1, return); va_list list; va_start(list, fmt); len = static_cast(strlen(msg)); err = vsnprintf_truncated_s(msg + len, MSG_LENGTH - len - 1, fmt, list); va_end(list); ONE_ACT_NO_LOG(err == -1, return); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_print(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, "%s", msg); } } /** * @brief DlogInner: log interface with level * @param [in]moduleId: moudule Id eg: CCE * @param [in]level: log level((0: debug, 1: info, 2: warning, 3: error) * @param [in]fmt: log msg string * @return: void */ void DlogInner(int moduleId, int level, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, level); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } /** * @brief DlogWithKVInner: log interface with level * @param [in]moduleId: moudule Id eg: CCE * @param [in]level: log level(0: debug, 1: info, 2: warning, 3: error) * @param [in]pstKVArray: key-value arrary * @param [in]kvNum: num of key-value arrary * @param [in]fmt: log msg string * @return: void */ void DlogWithKVInner(int moduleId, int level, KeyValue *pstKVArray, int kvNum, const char *fmt, ...) { ONE_ACT_NO_LOG(pstKVArray == NULL, return); ONE_ACT_NO_LOG(kvNum <= 0, return); if (fmt != nullptr) { int result = CheckLogLevel(moduleId, level); ONE_ACT_NO_LOG(result == FALSE, return); char msg[MSG_LENGTH] = {0}; int len = static_cast(strlen(msg)); int err; int i; for (i = 0; i < kvNum; i++, pstKVArray++) { ONE_ACT_NO_LOG(pstKVArray->kname == nullptr || pstKVArray->value == nullptr, continue); len = static_cast(strlen(msg)); err = snprintf_s(msg + len, MSG_LENGTH - len, MSG_LENGTH - len - 1, "[%s:%s] ", pstKVArray->kname, pstKVArray->value); ONE_ACT_NO_LOG(err == -1, return); } va_list list; va_start(list, fmt); len = static_cast(strlen(msg)); err = vsnprintf_truncated_s(msg + len, MSG_LENGTH - len - 1, fmt, list); va_end(list); ONE_ACT_NO_LOG(err == -1, return); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_print(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, "%s", msg); } } /** * @brief DlogFlush: flush log buffer to file * @return: void */ void DlogFlush(void) { return; } #ifdef __cplusplus #ifndef LOG_CPP } #endif // LOG_CPP #endif // __cplusplus #ifdef LOG_CPP #ifdef __cplusplus extern "C" { #endif int DlogGetlevelForC(int moduleId, int *enableEvent) { return dlog_getlevel(moduleId, enableEvent); } int DlogSetlevelForC(int moduleId, int level, int enableEvent) { return dlog_setlevel(moduleId, level, enableEvent); } int CheckLogLevelForC(int moduleId, int logLevel) { return CheckLogLevel(moduleId, logLevel); } int DlogSetAttrForC(LogAttr logAttr) { return DlogSetAttr(logAttr); } void DlogFlushForC(void) { return DlogFlush(); } void DlogInnerForC(int moduleId, int level, const char *fmt, ...) { if (fmt != nullptr) { int result = CheckLogLevel(moduleId, level); ONE_ACT_NO_LOG(result == FALSE, return); va_list list; va_start(list, fmt); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_vprint(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, fmt, list); va_end(list); } } void DlogWithKVInnerForC(int moduleId, int level, KeyValue *pstKVArray, int kvNum, const char *fmt, ...) { ONE_ACT_NO_LOG(pstKVArray == NULL, return); ONE_ACT_NO_LOG(kvNum <= 0, return); if (fmt != nullptr) { int result = CheckLogLevel(moduleId, level); ONE_ACT_NO_LOG(result == FALSE, return); char msg[MSG_LENGTH] = {0}; int len = static_cast(strlen(msg)); int err; int i; for (i = 0; i < kvNum; i++, pstKVArray++) { ONE_ACT_NO_LOG(pstKVArray->kname == nullptr || pstKVArray->value == nullptr, continue); len = static_cast(strlen(msg)); err = snprintf_s(msg + len, MSG_LENGTH - len, MSG_LENGTH - len - 1, "[%s:%s] ", pstKVArray->kname, pstKVArray->value); ONE_ACT_NO_LOG(err == -1, return); } va_list list; va_start(list, fmt); len = static_cast(strlen(msg)); err = vsnprintf_truncated_s(msg + len, MSG_LENGTH - len - 1, fmt, list); va_end(list); ONE_ACT_NO_LOG(err == -1, return); const ModuleInfo *moduleInfos = GetModuleInfos(); __android_log_print(ANDROID_LOG_INFO, moduleInfos[moduleId].moduleName, "%s", msg); } } #ifdef __cplusplus } #endif #endif // LOG_CPP // the code for stub #if (!defined PROCESS_LOG && !defined PLOG_AUTO) #include "plog.h" #ifdef __cplusplus extern "C" { #endif int DlogReportInitialize() { return 0; } int DlogReportFinalize() { return 0; } #ifdef __cplusplus } #endif #endif