• 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### Files
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. |
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. Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.|
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. Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.|
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. Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file. |
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. Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file. |
35| [OH_LOG_FATAL](#oh_log_fatal)(type, ...)   ((void) [OH_LOG_Print](#oh_log_print)((type), LOG_FATAL, [LOG_DOMAIN](#log_domain), [LOG_TAG](#log_tag), __VA_ARGS__)) | Indicates FATAL logs. This is a function-like macro. Before calling this function, define the log service domain and log tag. Generally, you need to define them at the beginning of the source file.|
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 |  Outputs logs of the specified **type**, **level**, **domain**, **tag**, and variables determined by the format specifier and privacy identifier in the printf format. |
58| int 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**. |
59| 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. |
60| int [OH_LOG_VPrint](#oh_log_vprint) ([LogType](#logtype) type, [LogLevel](#loglevel) level, unsigned int domain, const char \*tag, const char \*fmt, va_list ap) __attribute__((__format__(os_log |  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. |
61| 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. |
62| void [OH_LOG_SetCallback](#oh_log_setcallback) ([LogCallback](#logcallback) callback) | Registers a callback function. |
63| 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**.|
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)OH_LOG_Print((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.
345Possible failure causes: The **LogLevel** passed in is lower than the allowed log level; the **domain** is out of range; the **tag** is a null pointer; the CPU is overloaded; the memory is insufficient; the number of logs on the device is too large.
346
347
348### OH_LOG_PrintMsg()
349
350```
351int OH_LOG_PrintMsg (LogType type, LogLevel level, unsigned int domain, const char * tag, const char * message )
352```
353**Description**
354 Outputs constant log strings of the specified **type**, **level**, **domain**, and **tag**.
355
356**Since**: 18
357
358**Parameters**
359
360| Name| Description|
361| -------- | -------- |
362| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
363| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
364| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
365| 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. |
366| message | Constant log string. |
367
368**Returns**
369
370**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
371Possible failure causes: The **LogLevel** passed in is lower than the allowed log level; the **domain** is out of range; the **tag** is a null pointer; the CPU is overloaded; the memory is insufficient; the number of logs on the device is too large.
372
373
374### OH_LOG_PrintMsgByLen()
375
376```
377int OH_LOG_PrintMsgByLen (LogType type, LogLevel level, unsigned int domain, const char * tag, size_t tagLen, const char * message, size_t messageLen )
378```
379**Description**
380 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.
381
382**Since**: 18
383
384**Parameters**
385
386| Name| Description|
387| -------- | -------- |
388| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
389| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
390| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
391| 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. |
392| tagLen | Length of the tag. |
393| message | Constant log string. |
394| messageLen | Length of the constant string, which is less than 3500 characters. |
395
396**Returns**
397
398**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
399Possible failure causes: The **LogLevel** passed in is lower than the allowed log level; the **domain** is out of range; the **tag** is a null pointer; the CPU is overloaded; the memory is insufficient; the number of logs on the device is too large.
400
401
402### OH_LOG_SetCallback()
403
404```
405void OH_LOG_SetCallback (LogCallback callback)
406```
407**Description**
408Registers a callback function.
409
410After this function is called, the customized callback can take over all logs of the current process.
411
412Note that whether this API is called or not, it does not change the default log processing of the current process.
413
414**Since**: 11
415
416**Parameters**
417
418| Name| Description|
419| -------- | -------- |
420| callback | Custom callback function. If processing of logs is not needed, a null pointer can be transferred. |
421
422
423### OH_LOG_SetMinLogLevel()
424
425```
426void OH_LOG_SetMinLogLevel (LogLevel level)
427```
428**Description**
429Sets the minimum log level. When a process prints logs, both the minimum log level and global log level are verified.
430
431Therefore, 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**.
432
433**Since**: 15
434
435**Parameters**
436
437| Name| Description|
438| -------- | -------- |
439| level | Log level. |
440
441
442### OH_LOG_VPrint()
443
444```
445int OH_LOG_VPrint (LogType type, LogLevel level, unsigned int domain, const char * tag, const char * fmt, va_list ap )
446```
447**Description**
448 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.
449
450**Since**: 18
451
452**Parameters**
453
454| Name| Description|
455| -------- | -------- |
456| type | Log type. The type for third-party applications is defined by **LOG_APP**. |
457| level | Log level. The value can be **LOG_DEBUG**, **LOG_INFO**, **LOG_WARN**, **LOG_ERROR**, and **LOG_FATAL**. |
458| domain | Service domain. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. If the value exceeds the range, logs cannot be printed. |
459| 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. |
460| 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. |
461| 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. |
462
463**Returns**
464
465**0** or a larger value if the operation is successful; a value smaller than **0** otherwise.
466
467Possible failure causes: The **LogLevel** passed in is lower than the allowed log level; the **domain** is out of range; the **tag** is a null pointer; the CPU is overloaded; the memory is insufficient; the number of logs on the device is too large.
468