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