1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci* Copyright (c) 2022 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci* you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci* You may obtain a copy of the License at 6d9f0492fSopenharmony_ci* 7d9f0492fSopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci* 9d9f0492fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci* See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci* limitations under the License. 14d9f0492fSopenharmony_ci*/ 15d9f0492fSopenharmony_ci#include <errno.h> 16d9f0492fSopenharmony_ci#include <stdio.h> 17d9f0492fSopenharmony_ci#include <stdlib.h> 18d9f0492fSopenharmony_ci 19d9f0492fSopenharmony_ci#include "begetctl.h" 20d9f0492fSopenharmony_ci#include "init_log.h" 21d9f0492fSopenharmony_ci#include "init_utils.h" 22d9f0492fSopenharmony_ci#include "init_param.h" 23d9f0492fSopenharmony_ci#include "securec.h" 24d9f0492fSopenharmony_ci#include "shell_utils.h" 25d9f0492fSopenharmony_ci 26d9f0492fSopenharmony_cistatic int32_t SetInitLogLevelFromParam(BShellHandle shell, int argc, char **argv) 27d9f0492fSopenharmony_ci{ 28d9f0492fSopenharmony_ci if (argc != 2) { // 2 is set log level parameter number 29d9f0492fSopenharmony_ci BShellCmdHelp(shell, argc, argv); 30d9f0492fSopenharmony_ci return 0; 31d9f0492fSopenharmony_ci } 32d9f0492fSopenharmony_ci errno = 0; 33d9f0492fSopenharmony_ci unsigned int level = strtoul(argv[1], 0, 10); // 10 is decimal 34d9f0492fSopenharmony_ci if (errno != 0) { 35d9f0492fSopenharmony_ci printf("Failed to transform %s to unsigned int. \n", argv[1]); 36d9f0492fSopenharmony_ci return -1; 37d9f0492fSopenharmony_ci } 38d9f0492fSopenharmony_ci if ((level >= INIT_DEBUG) && (level <= INIT_FATAL)) { 39d9f0492fSopenharmony_ci int ret = SystemSetParameter("persist.init.debug.loglevel", argv[1]); 40d9f0492fSopenharmony_ci if (ret != 0) { 41d9f0492fSopenharmony_ci printf("Failed to set log level by param \"persist.init.debug.loglevel\" %s. \n", argv[1]); 42d9f0492fSopenharmony_ci } else { 43d9f0492fSopenharmony_ci printf("Success to set log level by param \"persist.init.debug.loglevel\" %s. \n", argv[1]); 44d9f0492fSopenharmony_ci } 45d9f0492fSopenharmony_ci } else { 46d9f0492fSopenharmony_ci printf("Set init log level in invailed parameter %s. \n", argv[1]); 47d9f0492fSopenharmony_ci } 48d9f0492fSopenharmony_ci return 0; 49d9f0492fSopenharmony_ci} 50d9f0492fSopenharmony_ci 51d9f0492fSopenharmony_cistatic int32_t GetInitLogLevelFromParam(BShellHandle shell, int argc, char **argv) 52d9f0492fSopenharmony_ci{ 53d9f0492fSopenharmony_ci char logLevel[2] = {0}; // 2 is set param "persist.init.debug.loglevel" value length. 54d9f0492fSopenharmony_ci uint32_t len = sizeof(logLevel); 55d9f0492fSopenharmony_ci int ret = SystemReadParam("persist.init.debug.loglevel", logLevel, &len); 56d9f0492fSopenharmony_ci if (ret == 0) { 57d9f0492fSopenharmony_ci printf("Success to get init log level: %s from param \"persist.init.debug.loglevel\". \n", logLevel); 58d9f0492fSopenharmony_ci } else { 59d9f0492fSopenharmony_ci printf("Failed to get init log level from param, keep the system origin log level. \n"); 60d9f0492fSopenharmony_ci } 61d9f0492fSopenharmony_ci return 0; 62d9f0492fSopenharmony_ci} 63d9f0492fSopenharmony_ci 64d9f0492fSopenharmony_ciMODULE_CONSTRUCTOR(void) 65d9f0492fSopenharmony_ci{ 66d9f0492fSopenharmony_ci const CmdInfo infos[] = { 67d9f0492fSopenharmony_ci {"setloglevel", SetInitLogLevelFromParam, "set init log level 0:debug, 1:info, 2:warning, 3:err, 4:fatal", 68d9f0492fSopenharmony_ci "setloglevel level", NULL}, 69d9f0492fSopenharmony_ci {"getloglevel", GetInitLogLevelFromParam, "get init log level 0:debug, 1:info, 2:warning, 3:err, 4:fatal", 70d9f0492fSopenharmony_ci "getloglevel", NULL}, 71d9f0492fSopenharmony_ci }; 72d9f0492fSopenharmony_ci for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) { 73d9f0492fSopenharmony_ci BShellEnvRegisterCmd(GetShellHandle(), &infos[i]); 74d9f0492fSopenharmony_ci } 75d9f0492fSopenharmony_ci}