• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HiLog
2
3
4## Overview
5
6Provides logging functions.
7
8For example, you can use logging functions to output logs of the specified log type, service domain, log tag, and log level.
9
10\@syscap SystemCapability.HiviewDFX.HiLog
11
12**Since:**
138
14
15## Summary
16
17
18### Files
19
20| Name | Description |
21| -------- | -------- |
22| [log.h](log_8h.md) | Defines the logging functions of the HiLog module.<br>File to Include: <hilog/log.h>  |
23
24
25### Macros
26
27| Name | Description |
28| -------- | -------- |
29| [LOG_DOMAIN](#log_domain)   0 | Defines the service domain for a log file.  |
30| [LOG_TAG](#log_tag)   NULL | Defines a string constant used to identify the class, file, or service.  |
31| [OH_LOG_DEBUG](#oh_log_debug)(type, ...)   ((void)[OH_LOG_Print](#oh_log_print)((type), LOG_DEBUG, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Outputs DEBUG logs. This is a function-like macro.  |
32| [OH_LOG_INFO](#oh_log_info)(type, ...)   ((void)[OH_LOG_Print](#oh_log_print)((type), LOG_INFO, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Outputs INFO logs. This is a function-like macro.  |
33| [OH_LOG_WARN](#oh_log_warn)(type, ...)   ((void)[OH_LOG_Print](#oh_log_print)((type), LOG_WARN, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Outputs WARN logs. This is a function-like macro.  |
34| [OH_LOG_ERROR](#oh_log_error)(type, ...)   ((void)[OH_LOG_Print](#oh_log_print)((type), LOG_ERROR, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Outputs ERROR logs. This is a function-like macro.  |
35| [OH_LOG_FATAL](#oh_log_fatal)(type, ...)   ((void)HiLogPrint((type), LOG_FATAL, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Outputs FATAL logs. This is a function-like macro.  |
36
37
38### Enums
39
40| Name | Description |
41| -------- | -------- |
42| [LogType](#logtype) { LOG_APP = 0 } | Enumerates log types.  |
43| [LogLevel](#loglevel) {<br/>LOG_DEBUG = 3, LOG_INFO = 4, LOG_WARN = 5, LOG_ERROR = 6,<br/>LOG_FATAL = 7<br/>} | Enumerates log levels.  |
44
45
46### Functions
47
48| Name | Description |
49| -------- | -------- |
50| [OH_LOG_Print](#oh_log_print) ([LogType](#logtype) type, [LogLevel](#loglevel) level, unsigned int domain, const char *tag, const char *fmt,...) \_\_attribute\_\_((\_\_format\_\_(os_log, 5,6))) |Outputs logs.  |
51| [OH_LOG_IsLoggable](#oh_log_isloggable) (unsigned int domain, const char \*tag, [LogLevel](#loglevel) level) | Checks whether logs of the specified service domain, log tag, and log level can be output.  |
52
53
54## Macro Description
55
56
57### LOG_DOMAIN
58
59
60```
61#define LOG_DOMAIN   0
62```
63**Description**<br>
64Defines the service domain for a log file.
65
66The service domain is used to identify the subsystem and module of a service. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value is beyond the range, its significant bits are automatically truncated.
67
68
69### LOG_TAG
70
71
72```
73#define LOG_TAG   NULL
74```
75**Description**<br>
76Defines a string constant used to identify the class, file, or service.
77
78
79### OH_LOG_DEBUG
80
81
82```
83#define OH_LOG_DEBUG( type,  ... )   ((void)OH_LOG_Print((type), LOG_DEBUG, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
84```
85**Description**<br>
86Outputs DEBUG logs. This is a function-like macro.
87
88Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
89
90 **Parameters**
91
92| Name | Description |
93| -------- | -------- |
94| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
95| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
96| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
97
98 **See**
99
100[OH_LOG_Print](#oh_log_print)
101
102
103### OH_LOG_ERROR
104
105
106```
107#define OH_LOG_ERROR( type,  ... )   ((void)OH_LOG_Print((type), LOG_ERROR, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
108```
109**Description**<br>
110Outputs ERROR logs. This is a function-like macro.
111
112Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
113
114 **Parameters**
115
116| Name | Description |
117| -------- | -------- |
118| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
119| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
120| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
121
122 **See**
123
124[OH_LOG_Print](#oh_log_print)
125
126
127### OH_LOG_FATAL
128
129
130```
131#define OH_LOG_FATAL( type,  ... )   ((void)HiLogPrint((type), LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
132```
133**Description**<br>
134Outputs FATAL logs. This is a function-like macro.
135
136Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
137
138 **Parameters**
139
140| Name | Description |
141| -------- | -------- |
142| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
143| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
144| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
145
146 **See**
147
148[OH_LOG_Print](#oh_log_print)
149
150
151### OH_LOG_INFO
152
153
154```
155#define OH_LOG_INFO( type,  ... )   ((void)OH_LOG_Print((type), LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
156```
157**Description**<br>
158Outputs INFO logs. This is a function-like macro.
159
160Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
161
162 **Parameters**
163
164| Name | Description |
165| -------- | -------- |
166| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
167| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
168| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
169
170 **See**
171
172[OH_LOG_Print](#oh_log_print)
173
174
175### OH_LOG_WARN
176
177
178```
179#define OH_LOG_WARN( type,  ... )   ((void)OH_LOG_Print((type), LOG_WARN, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
180```
181**Description**<br>
182Outputs WARN logs. This is a function-like macro.
183
184Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
185
186 **Parameters**
187
188| Name | Description |
189| -------- | -------- |
190| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
191| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
192| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
193
194 **See**
195
196[OH_LOG_Print](#oh_log_print)
197
198
199## Enum Description
200
201
202### LogLevel
203
204
205```
206enum LogLevel
207```
208**Description**<br>
209Enumerates log levels.
210
211You are advised to select log levels based on their respective use cases:
212
213DEBUG: provides more detailed process information than INFO logs to help developers analyze service processes and locate faults. DEBUG logs are not recorded in official versions by default. They are available in debug versions or in official versions with the debug function enabled.
214
215INFO: indicates the key service process nodes and exceptions (for example, no network signal or login failure) that occur during service running. These logs should be recorded by the dominant module in the service to avoid repeated logging conducted by multiple invoked modules or low-level functions.
216
217WARN: indicates a severe, unexpected fault that has little impact on users and can be rectified by the programs themselves or through simple operations.
218
219ERROR: indicates a program or functional error that affects the normal running or use of the functionality and can be fixed at a high cost, for example, by resetting data.
220
221FATAL: indicates that a program or functionality is about to crash and the fault cannot be rectified.
222
223| Name | Description |
224| -------- | -------- |
225| LOG_DEBUG  | DEBUG level to be used by OH_LOG_DEBUG |
226| LOG_INFO  | INFO level to be used by OH_LOG_INFO |
227| LOG_WARN  | WARN level to be used by OH_LOG_WARN |
228| LOG_ERROR  | ERROR level to be used by OH_LOG_ERROR |
229| LOG_FATAL  | FATAL level to be used by OH_LOG_FATAL |
230
231
232### LogType
233
234
235```
236enum LogType
237```
238**Description**<br>
239Enumerates log types.
240
241Currently, only LOG_APP is available.
242
243| Name | Description |
244| -------- | -------- |
245| LOG_APP  | Application log |
246
247
248## Function Description
249
250
251### OH_LOG_IsLoggable()
252
253
254```
255int bool OH_LOG_IsLoggable (unsigned int domain, const char * tag, LogLevel level )
256```
257**Description**<br>
258Checks whether logs of the specified service domain, log tag, and log level can be output.
259
260 **Parameters**
261
262| Name | Description |
263| -------- | -------- |
264| domain | Indicates the service domain of logs.  |
265| tag | Indicates the log tag.  |
266| level | Indicates the log level.  |
267
268**Returns**
269
270Returns **true** if the specified logs can be output; returns **false** otherwise.
271
272
273### OH_LOG_Print()
274
275
276```
277int OH_LOG_Print (LogType type, LogLevel level, unsigned int domain, const char * tag, const char * fmt,  ... )
278```
279**Description**<br>
280Outputs logs.
281
282You can use this function to output logs based on the specified log type, log level, service domain, log tag, and variable parameters determined by the format specifier and privacy identifier in the printf format.
283
284 **Parameters**
285
286| Name | Description |
287| -------- | -------- |
288| type | Indicates the log type. The type for third-party applications is defined by LOG_APP.  |
289| level | Indicates the log level, which can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**.  |
290| domain | Indicates the service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF.  |
291| tag | Indicates the log tag, which is a string used to identify the class, file, or service.  |
292| fmt | Indicates the format string, which is an enhancement of a printf format string and supports the privacy identifier. Specifically, **{public}** or **{private}** is added between the % character and the format specifier in each parameter.  |
293| ... | Indicates the parameter list corresponding to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.  |
294
295**Returns**
296
297Returns **0** or a larger value if the operation is successful; returns a value smaller than **0** otherwise.
298