• 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**System capability**: SystemCapability.HiviewDFX.HiLog
11
12**Since**: 8
13
14
15## Summary
16
17
18### File
19
20| Name| Description|
21| -------- | -------- |
22| [log.h](log_8h.md) | Defines the logging functions of the HiLog module. |
23
24
25### Macros
26
27| Name| Description|
28| -------- | -------- |
29| [LOG_DOMAIN](#log_domain)   0 | Indicates the service domain for a log file. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
30| [LOG_TAG](#log_tag)   NULL | Indicates the string constant used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. |
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\_\_)) | Indicates 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\_\_)) | Indicates 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\_\_)) | Indicates 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\_\_)) | Indicates 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\_\_)) | Indicates FATAL logs. This is a function-like macro. |
36
37
38### Types
39
40| Name| Description|
41| -------- | -------- |
42| typedef void(\* [LogCallback](#logcallback)) (const [LogType](#logtype) type, const [LogLevel](#loglevel) level, const unsigned int domain, const char \*tag, const char \*msg) | Defines a function for customizing the processing of logs in the callback. |
43
44
45### Enum
46
47| Name| Description|
48| -------- | -------- |
49| [LogType](#logtype) { LOG_APP = 0 } | Enumerates the log types. |
50| [LogLevel](#loglevel) {<br>LOG_DEBUG = 3,<br>LOG_INFO = 4,<br>LOG_WARN = 5,<br>LOG_ERROR = 6,<br>LOG_FATAL = 7<br>} | Enumerates the log levels. |
51
52
53### Functions
54
55| Name| Description|
56| -------- | -------- |
57| int [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. |
58| int bool [OH_LOG_IsLoggable](#oh_log_isloggable) (unsigned int domain, const char \*tag, [LogLevel](#loglevel) level) | Checks whether logs of the specified service domain, tag, and level can be printed. |
59| void [OH_LOG_SetCallback](#oh_log_setcallback) ([LogCallback](#logcallback) callback) | Registers a callback function. |
60| void [OH_LOG_SetMinLogLevel](#oh_log_setminloglevel) ([LogLevel](#loglevel) level) | Sets the minimum log level. When a process prints logs, both the minimum log level and global log level are verified.<br>Therefore, the minimum log level cannot be lower than the global log level. The default [global log level](../../dfx/hilog.md#displaying-and-setting-log-levels) is **Info**.|
61| int [OH_LOG_PrintMsg](#oh_log_printmsg) ([LogType](#logtype) type, [LogLevel](#loglevel) level, unsigned int domain, const char \*tag, const char \*message) | Outputs constant log strings of the specified **type**, **level**, **domain**, and **tag**. |
62| int [OH_LOG_PrintMsgByLen](#oh_log_printmsgbylen) ([LogType](#logtype) type, [LogLevel](#loglevel) level, unsigned int domain, const char \*tag, size_t tagLen, const char \*message, size_t messageLen) | Outputs log constant strings of the specified **domain**, **tag**, and **level**. The tag and string length must be specified. Unlike **OH_LOG_PrintMsg**, this API allows strings without terminators. |
63| int [OH_LOG_VPrint](#oh_log_vprint)(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, va_list ap) | Outputs logs of the specified **type**, **level**, **domain**, **tag**, and variables determined by the format specifier and privacy identifier in the printf format. The variables are of the **va_list** type.|
64
65
66## Macro Description
67
68
69### LOG_DOMAIN
70
71```
72#define LOG_DOMAIN   0
73```
74**Description**
75Indicates the service domain for a log file.
76
77Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed.
78
79**Since**: 8
80
81
82### LOG_TAG
83
84```
85#define LOG_TAG   NULL
86```
87**Description**
88Indicates the string constant used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur.
89
90**Since**: 8
91
92
93### OH_LOG_DEBUG
94
95```
96#define OH_LOG_DEBUG( type,  ... )   ((void)OH_LOG_Print((type), LOG_DEBUG, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
97```
98**Description**
99Indicates DEBUG logs. This is a function-like macro.
100
101Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
102
103**Since**: 8
104
105**Parameters**
106
107| Name| Description|
108| -------- | -------- |
109| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
110| fmt | 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. |
111| ... | 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. |
112
113**See**
114
115[OH_LOG_Print](#oh_log_print)
116
117
118### OH_LOG_ERROR
119
120```
121#define OH_LOG_ERROR( type,  ... )   ((void)OH_LOG_Print((type), LOG_ERROR, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
122```
123**Description**
124Indicates ERROR logs. This is a function-like macro.
125
126Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
127
128**Since**: 8
129
130**Parameters**
131
132| Name| Description|
133| -------- | -------- |
134| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
135| fmt | 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. |
136| ... | 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. |
137
138**See**
139
140[OH_LOG_Print](#oh_log_print)
141
142
143### OH_LOG_FATAL
144
145```
146#define OH_LOG_FATAL( type,  ... )   ((void)HiLogPrint((type), LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
147```
148**Description**
149Indicates FATAL logs. This is a function-like macro.
150
151Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
152
153**Since**: 8
154
155**Parameters**
156
157| Name| Description|
158| -------- | -------- |
159| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
160| fmt | 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. |
161| ... | 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. |
162
163**See**
164
165[OH_LOG_Print](#oh_log_print)
166
167
168### OH_LOG_INFO
169
170```
171#define OH_LOG_INFO( type,  ... )   ((void)OH_LOG_Print((type), LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
172```
173**Description**
174Indicates INFO logs. This is a function-like macro.
175
176Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
177
178**Since**: 8
179
180**Parameters**
181
182| Name| Description|
183| -------- | -------- |
184| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
185| fmt | 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. |
186| ... | 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. |
187
188**See**
189
190[OH_LOG_Print](#oh_log_print)
191
192
193### OH_LOG_WARN
194
195```
196#define OH_LOG_WARN( type,  ... )   ((void)OH_LOG_Print((type), LOG_WARN, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
197```
198**Description**
199Indicates WARN logs. This is a function-like macro.
200
201Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.
202
203**Since**: 8
204
205**Parameters**
206
207| Name| Description|
208| -------- | -------- |
209| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
210| fmt | 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. |
211| ... | 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. |
212
213**See**
214
215[OH_LOG_Print](#oh_log_print)
216
217
218## Type Description
219
220
221### LogCallback
222
223```
224typedef void(* LogCallback) (const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *msg)
225```
226**Description**
227Defines a function for customizing the processing of logs in the callback.
228
229**Since**: 11
230
231**Parameters**
232
233| Name| Description|
234| -------- | -------- |
235| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
236| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
237| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
238| tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. |
239| msg | Log content, which is made up of formatted log strings. |
240
241
242## Enum Description
243
244
245### LogLevel
246
247```
248enum LogLevel
249```
250**Description**
251Enumerates the log levels.
252
253You are advised to select log levels based on their respective use cases. Log levels:
254
255**DEBUG**: 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.
256
257**INFO**: 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.
258
259**WARN**: indicates a severe, unexpected fault that has little impact on users and can be rectified by the programs themselves or through simple operations.
260
261**ERROR**: 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.
262
263**FATAL**: indicates that a program or functionality is about to crash and the fault cannot be rectified.
264
265**Since**: 8
266
267| Value| Description|
268| -------- | -------- |
269| LOG_DEBUG  | DEBUG level to be used by **OH_LOG_DEBUG**.&nbsp;&nbsp; |
270| LOG_INFO  | INFO level to be used by **OH_LOG_INFO**.&nbsp;&nbsp; |
271| LOG_WARN  | WARN level to be used by **OH_LOG_WARN**.&nbsp;&nbsp; |
272| LOG_ERROR  | ERROR level to be used by **OH_LOG_ERROR**.&nbsp;&nbsp; |
273| LOG_FATAL  | FATAL level to be used by **OH_LOG_FATAL**.&nbsp;&nbsp; |
274
275
276### LogType
277
278```
279enum LogType
280```
281**Description**
282Enumerates the log types.
283
284You can use this function to specify the type of output logs. Currently, only **LOG_APP** is available.
285
286**Since**: 8
287
288| Value| Description|
289| -------- | -------- |
290| LOG_APP  | Application log.|
291
292
293## Function Description
294
295
296### OH_LOG_IsLoggable()
297
298```
299int bool OH_LOG_IsLoggable (unsigned int domain, const char * tag, LogLevel level )
300```
301**Description**
302Checks whether logs of the specified service domain, tag, and level can be printed.
303
304**Since**: 8
305
306**Parameters**
307
308| Name| Description|
309| -------- | -------- |
310| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
311| tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. |
312| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
313
314**Returns**
315
316**true** if the specified logs can be output; **false** otherwise.
317
318
319### OH_LOG_Print()
320
321```
322int OH_LOG_Print (LogType type, LogLevel level, unsigned int domain, const char * tag, const char * fmt,  ... )
323```
324**Description**
325
326
327Outputs logs of the specified **type**, **level**, **domain**, **tag**, and variables determined by the format specifier and privacy identifier in the printf format.
328
329**Since**: 8
330
331**Parameters**
332
333| Name| Description|
334| -------- | -------- |
335| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
336| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
337| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
338| tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur.|
339| fmt | 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. |
340| ... | 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. |
341
342**Returns**
343
344**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
345
346
347### OH_LOG_PrintMsg()
348
349```
350int OH_LOG_PrintMsg (LogType type, LogLevel level, unsigned int domain, const char * tag, const char * message )
351```
352**Description**
353 Outputs constant log strings of the specified **type**, **level**, **domain**, and **tag**.
354
355**Since**: 18
356
357**Parameters**
358
359| Name| Description|
360| -------- | -------- |
361| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
362| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
363| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
364| tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. |
365| message | Constant log string. |
366
367**Returns**
368
369**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
370
371
372### OH_LOG_PrintMsgByLen()
373
374```
375int OH_LOG_PrintMsgByLen (LogType type, LogLevel level, unsigned int domain, const char * tag, size_t tagLen, const char * message, size_t messageLen )
376```
377**Description**
378 Outputs log constant strings of the specified **domain**, **tag**, and **level**. The tag and string length must be specified. Unlike **OH_LOG_PrintMsg**, this API allows strings without terminators.
379
380**Since**: 18
381
382**Parameters**
383
384| Name| Description|
385| -------- | -------- |
386| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
387| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
388| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
389| tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur. |
390| tagLen | Length of the tag. |
391| message | Constant log string. |
392| messageLen | Length of the constant string, which is less than 3500 characters. |
393
394**Returns**
395
396**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
397
398### OH_LOG_VPrint()
399
400```
401int OH_LOG_VPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, va_list ap)
402```
403
404**Description**
405
406 Outputs logs of the specified **type**, **level**, **domain**, **tag**, and variables determined by the format specifier and privacy identifier in the printf format. The variables are of the **va_list** type.
407
408**Since**: 18
409
410
411**Parameters**
412
413| Parameter| Description|
414| -- | -- |
415| LogType type | Log type. The type for third-party applications is defined by **LOG_APP**.|
416| LogLevel level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**.|
417| unsigned int domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF.|
418| const char *tag | Log tag, which is a string used to identify the class, file, or service. A tag can contain a maximum of 31 bytes. If a tag exceeds this limit, it will be truncated. Chinese characters are not recommended because garbled characters or alignment problems may occur.|
419| const char *fmt | 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.|
420| va_list ap | Parameter list of the **va_list** type that corresponds to the parameter type in the format string. The number and type of parameters must be mapped onto the identifier in the format string.|
421
422**Returns**
423
424| Type| Description|
425| -- | -- |
426| int | **0** or a larger value if the operation is successful; a value smaller than **0** otherwise.|
427
428### OH_LOG_SetCallback()
429
430```
431void OH_LOG_SetCallback (LogCallback callback)
432```
433**Description**
434Registers a callback function.
435
436After this function is called, the customized callback can take over all logs of the current process.
437
438Note that whether this API is called or not, it does not change the default log processing of the current process.
439
440**Since**: 11
441
442**Parameters**
443
444| Name| Description|
445| -------- | -------- |
446| callback | Custom callback function. If processing of logs is not needed, a null pointer can be transferred. |
447
448
449### OH_LOG_SetMinLogLevel()
450
451```
452void OH_LOG_SetMinLogLevel (LogLevel level)
453```
454**Description**
455Sets the minimum log level. When a process prints logs, both the minimum log level and global log level are verified.
456
457Therefore, the minimum log level cannot be lower than the global log level. The default [global log level](../../dfx/hilog.md#displaying-and-setting-log-levels) is **Info**.
458
459**Since**: 15
460
461**Parameters**
462
463| Name| Description|
464| -------- | -------- |
465| level | Log level. |
466