未验证 提交 e8dd40a5 编写于 作者: O openharmony_ci 提交者: Gitee

!1355 init log等级设置

Merge pull request !1355 from cheng_jinsong/init1010
......@@ -52,7 +52,10 @@ typedef void (*InitCommLog)(int logLevel, uint32_t domain, const char *tag, cons
INIT_PUBLIC_API void StartupLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...);
INIT_PUBLIC_API void EnableInitLog(InitLogLevel level);
INIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level);
INIT_PUBLIC_API int GetInitLogLevel(void);
INIT_PUBLIC_API void SetInitCommLog(InitCommLog logFunc);
INIT_PUBLIC_API void EnableInitLogFromCmdline(void);
#define STARTUP_LOGV(domain, tag, fmt, ...) \
StartupLog(INIT_DEBUG, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
......
......@@ -71,6 +71,7 @@ if (defined(ohos_lite)) {
"param_cmd.c",
"sandbox.cpp",
"service_control.c",
"setloglevel.c",
"shell/shell_bas.c",
]
......@@ -119,6 +120,8 @@ if (defined(ohos_lite)) {
"service",
"sandbox",
"dump_service",
"setloglevel",
"getloglevel",
]
if (enable_ohos_startup_init_feature_ab_partition) {
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "begetctl.h"
#include "init_utils.h"
#include "init_param.h"
#include "securec.h"
#include "shell_utils.h"
static int32_t SetInitLogLevelFromParam(BShellHandle shell, int argc, char **argv)
{
if (argc != 2) { // 2 is set log level parameter number
BShellCmdHelp(shell, argc, argv);
return 0;
}
errno = 0;
unsigned int level = strtoul(argv[1], 0, 10); // 10 is decimal
if (errno != 0) {
printf("Failed to transform %s to unsigned int. \n", argv[1]);
return -1;
}
if ((level >= INIT_DEBUG) && (level <= INIT_FATAL)) {
int ret = SystemSetParameter("persist.init.debug.loglevel", argv[1]);
if (ret != 0) {
printf("Failed to set log level by param \"persist.init.debug.loglevel\" %s. \n", argv[1]);
} else {
printf("Success to set log level by param \"persist.init.debug.loglevel\" %s. \n", argv[1]);
}
} else {
printf("Set init log level in invailed parameter %s. \n", argv[1]);
}
return 0;
}
static int32_t GetInitLogLevelFromParam(BShellHandle shell, int argc, char **argv)
{
char logLevel[2] = {0}; // 2 is set param "persist.init.debug.loglevel" value length.
uint32_t len = sizeof(logLevel);
int ret = SystemReadParam("persist.init.debug.loglevel", logLevel, &len);
if (ret == 0) {
printf("Success to get init log level: %s from param \"persist.init.debug.loglevel\" \n", logLevel);
} else {
printf("Failed to get init log level from param, keep the system origin log level: %d.\n", GetInitLogLevel());
}
return 0;
}
MODULE_CONSTRUCTOR(void)
{
const CmdInfo infos[] = {
{"setloglevel", SetInitLogLevelFromParam, "set init log level 0:debug, 1:info, 2:warning, 3:err, 4:fatal",
"setloglevel level", NULL},
{"getloglevel", GetInitLogLevelFromParam, "get init log level 0:debug, 1:info, 2:warning, 3:err, 4:fatal",
"getloglevel", NULL},
};
for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ config("exported_header_files") {
include_dirs = [
"//base/startup/init/interfaces/innerkits/include",
"//base/startup/init/services/log",
"//base/startup/init/services/include",
]
}
......
......@@ -22,6 +22,7 @@
#include <time.h>
#include <sys/time.h>
#include "init_utils.h"
#include "securec.h"
#ifdef OHOS_LITE
#ifndef INIT_LOG_INIT
......@@ -131,8 +132,43 @@ INIT_LOCAL_API void InitLog(int logLevel, unsigned int domain, const char *tag,
PrintLog((InitLogLevel)logLevel, domain, tag, tmpFmt);
}
INIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level)
{
if ((level >= INIT_DEBUG) && (level <= INIT_FATAL)) {
g_logLevel = level;
}
return;
}
INIT_PUBLIC_API int GetInitLogLevel(void)
{
return g_logLevel;
}
INIT_PUBLIC_API void EnableInitLog(InitLogLevel level)
{
g_logLevel = level;
SetInitCommLog(InitLog);
}
INIT_PUBLIC_API void EnableInitLogFromCmdline(void)
{
SetInitCommLog(InitLog);
char level[MAX_BUFFER_LEN] = {0};
char *buffer = ReadFileData("/proc/cmdline");
if (buffer == NULL) {
INIT_LOGE("Failed to read \"/proc/cmdline\"");
return;
}
int ret = GetProcCmdlineValue("initloglevel", buffer, level, MAX_BUFFER_LEN);
free(buffer);
if (ret != 0) {
INIT_LOGE("Failed get log level from cmdline");
return;
}
errno = 0;
unsigned int logLevel = (unsigned int)strtoul(level, 0, 10); // 10 is decimal
INIT_INFO_CHECK(errno == 0, return, "Failed strtoul %s, err=%d", level, errno);
SetInitLogLevel(logLevel);
return;
}
......@@ -342,7 +342,6 @@ static void SetServiceBootEventFork(SERVICE_INFO_CTX *serviceCtx)
MODULE_CONSTRUCTOR(void)
{
EnableInitLog(INIT_DEBUG);
InitAddServiceHook(SetServiceBootEventFork, INIT_SERVICE_FORK_BEFORE);
InitAddServiceHook(ClearServiceBootEvent, INIT_SERVICE_CLEAR);
InitAddServiceHook(DumpServiceBootEvent, INIT_SERVICE_DUMP);
......
......@@ -122,9 +122,27 @@ static int CmdClear(int id, const char *name, int argc, const char **argv)
return 0;
}
static int ParamSetBootEventHook(const HOOK_INFO *hookInfo, void *cookie)
static int CmdSetLogLevel(int id, const char *name, int argc, const char **argv)
{
UNUSED(id);
if ((name == NULL) || (argv == NULL) || (argc < 1)) {
PLUGIN_LOGE("Failed get log level from parameter.");
return -1;
}
char *value = strrchr(argv[0], '.');
PLUGIN_CHECK(value != NULL, return -1, "Failed get \'.\' from string %s", argv[0]);
unsigned int level;
int ret = StringToUint(value + 1, &level);
PLUGIN_CHECK(ret == 0, return -1, "Failed make string to unsigned int");
PLUGIN_LOGI("level is %d", level);
SetInitLogLevel(level);
return 0;
}
static int ParamSetInitCmdHook(const HOOK_INFO *hookInfo, void *cookie)
{
AddCmdExecutor("clear", CmdClear);
AddCmdExecutor("setloglevel", CmdSetLogLevel);
return 0;
}
......@@ -137,7 +155,7 @@ static int DumpTrigger(const char *fmt, ...)
return 0;
}
static int DumpServiceHook(const HOOK_INFO *info, void *cookie)
static void DumpServiceHook(void)
{
// check and dump all jobs
char dump[8] = {0}; // 8 len
......@@ -147,12 +165,34 @@ static int DumpServiceHook(const HOOK_INFO *info, void *cookie)
if (ret == 0 && strcmp(dump, "1") == 0) {
SystemDumpTriggers(1, DumpTrigger);
}
return;
}
static void InitLogLevelFromPersist(void)
{
char logLevel[2] = {0}; // 2 is set param "persist.init.debug.loglevel" value length.
uint32_t len = sizeof(logLevel);
int ret = SystemReadParam("persist.init.debug.loglevel", logLevel, &len);
INIT_INFO_CHECK(ret == 0, return, "Can not get log level from param, keep the original loglevel.");
errno = 0;
unsigned int level = (unsigned int)strtoul(logLevel, 0, 10); // 10 is decimal
INIT_INFO_CHECK(errno == 0, return, "Failed strtoul %s, err=%d", logLevel, errno);
SetInitLogLevel(level);
return;
}
static int InitDebugHook(const HOOK_INFO *info, void *cookie)
{
UNUSED(info);
UNUSED(cookie);
InitLogLevelFromPersist();
DumpServiceHook();
return 0;
}
MODULE_CONSTRUCTOR(void)
{
InitAddGlobalInitHook(0, ParamSetBootEventHook);
InitAddGlobalInitHook(0, ParamSetInitCmdHook);
// Depends on parameter service
InitAddPostPersistParamLoadHook(0, DumpServiceHook);
InitAddPostPersistParamLoadHook(0, InitDebugHook);
}
......@@ -62,6 +62,7 @@ const ParamCmdInfo *GetOtherSpecial(size_t *size)
{
static const ParamCmdInfo other[] = {
{"bootevent.", "bootevent.", "bootevent"},
{"persist.init.debug.", "persist.init.debug.", "setloglevel"},
};
*size = ARRAY_LENGTH(other);
return other;
......
......@@ -183,7 +183,7 @@ char *ReadFileData(const char *fileName)
int fd = -1;
fd = open(fileName, O_RDONLY);
INIT_ERROR_CHECK(fd >= 0, return NULL, "Failed to read file %s", fileName);
buffer = (char *)malloc(MAX_SMALL_BUFFER); // fsmanager not create, can not get fileStat st_size
buffer = (char *)calloc(1, MAX_SMALL_BUFFER); // fsmanager not create, can not get fileStat st_size
INIT_ERROR_CHECK(buffer != NULL, close(fd);
return NULL, "Failed to allocate memory for %s", fileName);
ssize_t readLen = read(fd, buffer, MAX_SMALL_BUFFER - 1);
......@@ -472,7 +472,7 @@ int ReadFileInDir(const char *dirPath, const char *includeExt,
INIT_CHECK_RETURN_VALUE(dirPath != NULL && processFile != NULL, -1);
DIR *pDir = opendir(dirPath);
INIT_ERROR_CHECK(pDir != NULL, return -1, "Read dir :%s failed.%d", dirPath, errno);
char *fileName = malloc(MAX_BUF_SIZE);
char *fileName = calloc(1, MAX_BUF_SIZE);
INIT_ERROR_CHECK(fileName != NULL, closedir(pDir);
return -1, "Failed to malloc for %s", dirPath);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册