1e41f4b71Sopenharmony_ci# Log Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Function Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciBasic functions of a log include recording key nodes and troubleshooting faults during the init startup process.
8e41f4b71Sopenharmony_ci- Logs help troubleshoot faults, check the startup duration of each subsystem, command execution duration, and more.
9e41f4b71Sopenharmony_ci- Log tags (including **param**, **uevent**, and **module**) for different modules can be viewed.
10e41f4b71Sopenharmony_ci- Logs can be printed for key phases, for example, the startup of the first phase, a required partition and its device node, **uevent** creation, service startup, and more.
11e41f4b71Sopenharmony_ci- Log levels are configurable. Available log levels include INIT_DEBUG, INIT_INFO, INIT_WARN, INIT_ERROR, and INIT_FATAL.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci### Basic Concepts
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciThe implementation of the init log function varies according to the OpenHarmony version.
16e41f4b71Sopenharmony_ci- OpenHarmony standard system: The init log function is implemented via **dmesg log** of the Linux kernel.
17e41f4b71Sopenharmony_ci- For the OpenHarmony LiteOS small system, the init log function is implemented by using the **hilog** API.
18e41f4b71Sopenharmony_ci- For the OpenHarmony LiteOS mini system, the init log function is implemented by using the **printf** API.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci### Log Type
21e41f4b71Sopenharmony_ci  init logs are classified into hilog and demsg logs. The hilog log records information related to system service processes, and the demsg log records information related to the kernel.
22e41f4b71Sopenharmony_ci### Log Level
23e41f4b71Sopenharmony_ci  init logs are classified into five levels, which can be set by the **(INIT_DEBUG_LEVEL)persist.init.debug.loglevel** parameter.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci  ```
26e41f4b71Sopenharmony_ci  InitLogLevel:
27e41f4b71Sopenharmony_ci    INIT_DEBUG = 0,
28e41f4b71Sopenharmony_ci    INIT_INFO,
29e41f4b71Sopenharmony_ci    INIT_WARN,
30e41f4b71Sopenharmony_ci    INIT_ERROR,
31e41f4b71Sopenharmony_ci    INIT_FATAL
32e41f4b71Sopenharmony_ci  ```
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci  Kmsg log level. The options are as follows:
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci  ```
37e41f4b71Sopenharmony_ci  "<7>" =====> "DEBUG"
38e41f4b71Sopenharmony_ci  "<6>" =====> "INFO"
39e41f4b71Sopenharmony_ci  "<4>" =====> "WARNING"
40e41f4b71Sopenharmony_ci  "<3>" =====> "ERROR"
41e41f4b71Sopenharmony_ci  "<3>" =====> "FATAL"
42e41f4b71Sopenharmony_ci  ```
43e41f4b71Sopenharmony_ci### Key Macros for Log Control
44e41f4b71Sopenharmony_ci  **INIT_DMESG** specifies whether to record kernel logs in **/dev/kmsg**.
45e41f4b71Sopenharmony_ci  **INIT_FILE** specifies whether to write log information to the **/data/init_agent/begetctl.log** file.
46e41f4b71Sopenharmony_ci  **INIT_AGENT** specifies whether to use hilog to record log information.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci  Key APIs:
49e41f4b71Sopenharmony_ci  ```
50e41f4b71Sopenharmony_ci    void EnableInitLog (InitLogLevel level) Enables the init log function.
51e41f4b71Sopenharmony_ci  
52e41f4b71Sopenharmony_ci    void SetInitLogLevel(InitLogLevel level) Sets the log level to control log output.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci    void StartupLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...) Provides an entry to the init log function.
55e41f4b71Sopenharmony_ci  ```
56e41f4b71Sopenharmony_ci  **STARTUP_LOGI** is a macro defined for **StartupLog**, which is defined in the **/base/startup/init/interfaces/innerkits/include/beget_ext.h** file. Other logs are redefined based on the **STARTUP_LOGI** macro.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci  - deviceInfo module (init/device_info/idevice_info.h):
59e41f4b71Sopenharmony_ci  ```
60e41f4b71Sopenharmony_ci    DINFO_LOGI
61e41f4b71Sopenharmony_ci    DINFO_LOGE
62e41f4b71Sopenharmony_ci    DINFO_LOGV
63e41f4b71Sopenharmony_ci  ```
64e41f4b71Sopenharmony_ci  - Param JS API module (init/interfaces/kits/jskits/src/native_parameters_js.h):
65e41f4b71Sopenharmony_ci  ```
66e41f4b71Sopenharmony_ci    PARAM_JS_LOGI
67e41f4b71Sopenharmony_ci    PARAM_JS_LOGE
68e41f4b71Sopenharmony_ci    PARAM_JS_LOGV
69e41f4b71Sopenharmony_ci    PARAM_JS_LOGW
70e41f4b71Sopenharmony_ci  ```
71e41f4b71Sopenharmony_ci  - Shell module (init/services/begetctl/shell/shell_utils.h):
72e41f4b71Sopenharmony_ci  ```
73e41f4b71Sopenharmony_ci    BSH_LOGI
74e41f4b71Sopenharmony_ci    BSH_LOGE
75e41f4b71Sopenharmony_ci    BSH_LOGV
76e41f4b71Sopenharmony_ci  ```
77e41f4b71Sopenharmony_ci  - LoopEvent module (init/services/loopevent/utils/le_utils.h):
78e41f4b71Sopenharmony_ci  ```
79e41f4b71Sopenharmony_ci    LE_LOGI
80e41f4b71Sopenharmony_ci    LE_LOGE
81e41f4b71Sopenharmony_ci    LE_LOGV
82e41f4b71Sopenharmony_ci  ```
83e41f4b71Sopenharmony_ci  - Plug-in module (init/services/modules/plugin_adapter.h):
84e41f4b71Sopenharmony_ci  ```
85e41f4b71Sopenharmony_ci    PLUGIN_LOGI
86e41f4b71Sopenharmony_ci    PLUGIN_LOGE
87e41f4b71Sopenharmony_ci    PLUGIN_LOGV
88e41f4b71Sopenharmony_ci    PLUGIN_LOGW
89e41f4b71Sopenharmony_ci  ```
90e41f4b71Sopenharmony_ci  - Param module (init/services/param/include/param_utils.h):
91e41f4b71Sopenharmony_ci  ```
92e41f4b71Sopenharmony_ci    PARAM_LOGI
93e41f4b71Sopenharmony_ci    PARAM_LOGE
94e41f4b71Sopenharmony_ci    PARAM_LOGV
95e41f4b71Sopenharmony_ci    PARAM_LOGW
96e41f4b71Sopenharmony_ci  ```
97e41f4b71Sopenharmony_ci  - ParamWatcher module (init/services/param/watcher/include/watcher_utils.h):
98e41f4b71Sopenharmony_ci  ```
99e41f4b71Sopenharmony_ci    WATCHER_LOGI
100e41f4b71Sopenharmony_ci    WATCHER_LOGE
101e41f4b71Sopenharmony_ci    WATCHER_LOGV
102e41f4b71Sopenharmony_ci  ```
103e41f4b71Sopenharmony_ci  - init log module implemented based on the macros defined by **StartupLog** (init/services/log/init_log.h)
104e41f4b71Sopenharmony_ci  ```
105e41f4b71Sopenharmony_ci    INIT_LOGV
106e41f4b71Sopenharmony_ci    INIT_LOGI
107e41f4b71Sopenharmony_ci    INIT_LOGW
108e41f4b71Sopenharmony_ci    INIT_LOGE
109e41f4b71Sopenharmony_ci    INIT_LOGF
110e41f4b71Sopenharmony_ci  ```
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci### Constraints
113e41f4b71Sopenharmony_ciN/A
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci## How to Develop
116e41f4b71Sopenharmony_ci### Use Cases
117e41f4b71Sopenharmony_ciinit logs are mainly used to start modules (like **param**, **ueventd**, and **module**) during init startup, and are used in the open API **begetutils**.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci### Available APIs
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**Table 1** Log APIs
122e41f4b71Sopenharmony_ci   | API| Format and Example| Description|
123e41f4b71Sopenharmony_ci   | -------- | -------- | -------- |
124e41f4b71Sopenharmony_ci   | INIT_LOGV | INIT_LOGV("Add %s to job %s", service->name, jobName); | Prints the log of the DEBUG level.|
125e41f4b71Sopenharmony_ci   | INIT_LOGI | INIT_LOGI("Start init first stage."); | Prints the log of the INFO level.|
126e41f4b71Sopenharmony_ci   | INIT_LOGW | INIT_LOGW("initialize signal handler failed"); | Prints the log of the WARN level.|
127e41f4b71Sopenharmony_ci   | INIT_LOGE | INIT_LOGE("Failed to format other opt"); | Prints the log of the ERROR level.|
128e41f4b71Sopenharmony_ci   | INIT_LOGF | INIT_LOGF("Failed to init system"); | Prints the log of the FATAL level.|
129e41f4b71Sopenharmony_ci   | INIT_ERROR_CHECK | INIT_ERROR_CHECK(ctx != NULL, return NULL, "Failed to get cmd args "); | Prints a log and executes **return NULL** when **ctx != NULL** is not true.|
130e41f4b71Sopenharmony_ci   | INIT_INFO_CHECK | INIT_INFO_CHECK(sockopt != NULL, return SERVICE_FAILURE, "Failed to malloc for service %s", service->name); | Prints a log and executes **return SERVICE_FAILURE** when **sockopt != NULL** is not true.|
131e41f4b71Sopenharmony_ci   | INIT_WARNING_CHECK | INIT_WARNING_CHECK(argsCount <= SPACES_CNT_IN_CMD_MAX, argsCount = SPACES_CNT_IN_CMD_MAX, "Too much arguments for command, max number is %d", SPACES_CNT_IN_CMD_MAX); | Prints a log and executes **argsCount = SPACES_CNT_IN_CMD_MAX** when **argsCount <= SPACES_CNT_IN_CMD_MAX** is not true.|
132e41f4b71Sopenharmony_ci   | INIT_CHECK | INIT_CHECK(arg != NULL, return NULL); | Executes **return NULL** when **arg != NULL** is not true.|
133e41f4b71Sopenharmony_ci   | INIT_CHECK_RETURN_VALUE | INIT_CHECK_RETURN_VALUE(errno == 0, -1); | Executes **return -1** when **errno == 0** is not true.|
134e41f4b71Sopenharmony_ci   | INIT_CHECK_ONLY_RETURN | INIT_CHECK_ONLY_RETURN(cmd != NULL); | Executes **return** when **cmd != NULL** is not true.|
135e41f4b71Sopenharmony_ci   | INIT_CHECK_ONLY_ELOG | INIT_CHECK_ONLY_ELOG(execv(argv[0], argv) == 0, "execv %s failed! err %d.", argv[0], errno); | Prints only **log "execv %s failed! err %d."** when **execv(argv[0], argv) == 0** is not true.|
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci### Development Example
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci   1. Call an API to print the log.
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci      For example, call **INIT_LOGI("Start init first stage.")** in **//base/startup/init/services/init/standard/init.c** to print the log.
142e41f4b71Sopenharmony_ci       ```c
143e41f4b71Sopenharmony_ci       void SystemPrepare(void)
144e41f4b71Sopenharmony_ci       {
145e41f4b71Sopenharmony_ci          MountBasicFs();
146e41f4b71Sopenharmony_ci          CreateDeviceNode();
147e41f4b71Sopenharmony_ci          LogInit();
148e41f4b71Sopenharmony_ci          // Make sure init log always output to /dev/kmsg.
149e41f4b71Sopenharmony_ci          EnableDevKmsg();
150e41f4b71Sopenharmony_ci          INIT_LOGI("Start init first stage.");
151e41f4b71Sopenharmony_ci          // Only ohos normal system support
152e41f4b71Sopenharmony_ci          // two stages of init.
153e41f4b71Sopenharmony_ci          // If we are in updater mode, only one stage of init.
154e41f4b71Sopenharmony_ci          if (InUpdaterMode() == 0) {
155e41f4b71Sopenharmony_ci              StartInitSecondStage();
156e41f4b71Sopenharmony_ci          }
157e41f4b71Sopenharmony_ci       }
158e41f4b71Sopenharmony_ci       ```
159e41f4b71Sopenharmony_ci      Run **dmesg** to check the printed log **Start init first stage.**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci  2. Set the log level by running the **begetctl setloglevel level** command.
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci     In the command, set **level** to one of the following log levels: **0** (**INIT_DEBUG**), **1** (**INIT_INFO**), **2** (**INIT_WARN**), **3** (**INIT_ERROR**), and **4** (**INIT_FATAL**).
164e41f4b71Sopenharmony_ci  
165e41f4b71Sopenharmony_ci     After the setting is complete, the level set via **g_logLevel** of init takes effect immediately, and log APIs can print logs only when their log levels are higher than or equal to this level. For example, **begetctl setloglevel 3** sets the log level to **INIT_ERROR**. In this case, only **INIT_LOGE** and **INIT_LOGF** will print logs.
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci     After the system is restarted, the configured log level will take effect after the **load_persist_params** command in the **init.cfg** file is executed.
168e41f4b71Sopenharmony_ci   
169