log打印优化

Signed-off-by: wangyulie <wanglieyu@126.com>
This commit is contained in:
wangyulie 2024-10-29 14:52:06 +08:00
parent 0b9fc4349a
commit 773ac2db50
5 changed files with 65 additions and 3 deletions

View File

@ -240,9 +240,22 @@ ohos_prebuilt_etc("blacklist_cfg") {
] ]
} }
ohos_prebuilt_etc("log_ctr_whitelist_cfg") {
relative_install_dir = "ffrt"
source = "log_ctr_whitelist.conf"
part_name = "ffrt"
subsystem_name = "resourceschedule"
install_enable = true
install_images = [
"system",
"updater",
]
}
group("ffrt_ndk") { group("ffrt_ndk") {
deps = [ deps = [
":blacklist_cfg", ":blacklist_cfg",
":libffrt", ":libffrt",
":log_ctr_whitelist_cfg",
] ]
} }

3
log_ctr_whitelist.conf Normal file
View File

@ -0,0 +1,3 @@
foundation
CameraDaemon
com.ohos.sceneboard

View File

@ -16,18 +16,31 @@
#ifdef OHOS_STANDARD_SYSTEM #ifdef OHOS_STANDARD_SYSTEM
#include "faultloggerd_client.h" #include "faultloggerd_client.h"
#endif #endif
#include <string> #include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <atomic> #include <atomic>
#include "ffrt_log_api.h" #include "ffrt_log_api.h"
#include "internal_inc/osal.h" #include "internal_inc/osal.h"
static int g_ffrtLogLevel = FFRT_LOG_DEBUG; static int g_ffrtLogLevel = FFRT_LOG_DEBUG;
static std::atomic<unsigned int> g_ffrtLogId(0); static std::atomic<unsigned int> g_ffrtLogId(0);
static bool g_whiteListFlag = false;
namespace {
constexpr int PROCESS_NAME_BUFFER_LENGTH = 1024;
constexpr char CONF_FILEPATH[] = "/etc/ffrt/log_ctr_whitelist.conf";
}
unsigned int GetLogId(void) unsigned int GetLogId(void)
{ {
return ++g_ffrtLogId; return ++g_ffrtLogId;
} }
bool IsInWhitelist(void)
{
return g_whiteListFlag;
}
int GetFFRTLogLevel(void) int GetFFRTLogLevel(void)
{ {
return g_ffrtLogLevel; return g_ffrtLogLevel;
@ -45,7 +58,34 @@ static void SetLogLevel(void)
} }
} }
void InitWhiteListFlag(void)
{
// 获取当前进程名称
char processName[PROCESS_NAME_BUFFER_LENGTH] = "";
GetProcessName(processName, PROCESS_NAME_BUFFER_LENGTH);
if (strlen(processName) == 0) {
g_whiteListFlag = true;
return;
}
// 从配置文件读取白名单对比
std::string whiteProcess;
std::ifstream file(CONF_FILEPATH);
if (file.is_open()) {
while (std::getline(file, whiteProcess)) {
if (strstr(processName, whiteProcess.c_str()) != nullptr) {
g_whiteListFlag = true;
return;
}
}
} else {
// 当文件不存在或者无权限时默认都开
g_whiteListFlag = true;
}
}
static __attribute__((constructor)) void LogInit(void) static __attribute__((constructor)) void LogInit(void)
{ {
SetLogLevel(); SetLogLevel();
InitWhiteListFlag();
} }

View File

@ -23,6 +23,7 @@
#endif #endif
#include <string_view> #include <string_view>
#include "hilog/log.h" #include "hilog/log.h"
#include "internal_inc/osal.h"
#include "dfx/bbox/fault_logger_fd_manager.h" #include "dfx/bbox/fault_logger_fd_manager.h"
#else #else
#include "log_base.h" #include "log_base.h"
@ -35,6 +36,8 @@
#define FFRT_LOG_LEVEL_MAX (FFRT_LOG_DEBUG + 1) #define FFRT_LOG_LEVEL_MAX (FFRT_LOG_DEBUG + 1)
unsigned int GetLogId(void); unsigned int GetLogId(void);
bool IsInWhitelist(void);
void InitWhiteListFlag(void);
#ifdef OHOS_STANDARD_SYSTEM #ifdef OHOS_STANDARD_SYSTEM
template<size_t N> template<size_t N>
@ -78,8 +81,10 @@ constexpr auto convertFmtToPublic(const char(&str)[N])
#if (FFRT_LOG_LEVEL >= FFRT_LOG_DEBUG) #if (FFRT_LOG_LEVEL >= FFRT_LOG_DEBUG)
#define FFRT_LOGD(format, ...) \ #define FFRT_LOGD(format, ...) \
do { \ do { \
constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \ if (unlikely(IsInWhitelist())) {\
HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_DEBUG, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \ constexpr auto fmtPub = convertFmtToPublic("%u:%s:%d " format); \
HILOG_IMPL_STD_ARRAY(LOG_CORE, LOG_DEBUG, fmtPub, GetLogId(), __func__, __LINE__, ##__VA_ARGS__); \
} \
} while (0) } while (0)
#else #else
#define FFRT_LOGD(format, ...) #define FFRT_LOGD(format, ...)

View File

@ -57,5 +57,6 @@ FFRTFacade::FFRTFacade()
{ {
DependenceManager::Instance(); DependenceManager::Instance();
ProcessExitManager::Instance(); ProcessExitManager::Instance();
InitWhiteListFlag();
} }
} // namespace FFRT } // namespace FFRT