1 /*
2  * Copyright (c) 2021 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 
16 #ifndef HIVIEWDFX_HILOG_C_H
17 #define HIVIEWDFX_HILOG_C_H
18 
19 #include <stdarg.h>
20 #include <stdbool.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * Log domain, indicates the log service domainID. Different LogType have different domainID ranges.
28  * Log type of LOG_APP: 0-0xFFFF
29  * Log type of LOG_CORE & LOG_INIT: 0xD000000-0xD0FFFFF
30  */
31 #ifndef LOG_DOMAIN
32 #define LOG_DOMAIN 0
33 #endif
34 
35 /**
36  * Log tag, indicates the log service tag, you can customize the tag as needed, usually the keyword of the module.
37  */
38 #ifndef LOG_TAG
39 #define LOG_TAG NULL
40 #endif
41 
42 /* Log type */
43 typedef enum {
44     /* min log type */
45     LOG_TYPE_MIN = 0,
46     /* Used by app log. */
47     LOG_APP = 0,
48     /* Used by system log: recommended for important logs during the startup phase. */
49     LOG_INIT = 1,
50     /* Used by core service, framework. */
51     LOG_CORE = 3,
52     /* Used by kmsg log. */
53     LOG_KMSG = 4,
54     /* Not print in release version. */
55     LOG_ONLY_PRERELEASE = 5,
56     /* max log type */
57     LOG_TYPE_MAX
58 } LogType;
59 
60 /* Log level */
61 typedef enum {
62     /* min log level */
63     LOG_LEVEL_MIN = 0,
64     /* Designates lower priority log. */
65     LOG_DEBUG = 3,
66     /* Designates useful information. */
67     LOG_INFO = 4,
68     /* Designates hazardous situations. */
69     LOG_WARN = 5,
70     /* Designates very serious errors. */
71     LOG_ERROR = 6,
72     /* Designates major fatal anomaly. */
73     LOG_FATAL = 7,
74     /* max log level */
75     LOG_LEVEL_MAX,
76 } LogLevel;
77 
78 /**
79  * @brief Get log of fatal level
80  */
81 const char* GetLastFatalMessage(void);
82 
83 /**
84  * @brief Print hilog
85  *
86  * @return If the log is successfully printed, returns the number of bytes written,
87  * if failed, returns -1.
88  */
89 int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
90     __attribute__((__format__(os_log, 5, 6)));
91 
92 /**
93  * @brief Hilog C interface of different log level
94  *
95  * @param type enum:LogType
96  */
97 #define HILOG_DEBUG(type, ...) ((void)HILOG_IMPL((type), LOG_DEBUG, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
98 
99 #define HILOG_INFO(type, ...) ((void)HILOG_IMPL((type), LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
100 
101 #define HILOG_WARN(type, ...) ((void)HILOG_IMPL((type), LOG_WARN, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
102 
103 #define HILOG_ERROR(type, ...) ((void)HILOG_IMPL((type), LOG_ERROR, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
104 
105 #define HILOG_FATAL(type, ...) ((void)HILOG_IMPL((type), LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
106 
107 /**
108  * @brief Hilog C interface implementation
109  *
110  * @param type enum:LogType
111  * @param level enum:LogLevel
112  * @param domain macro:LOG_DOMAIN
113  * @param tag macro:LOG_TAG
114  */
115 #define HILOG_IMPL(type, level, domain, tag, ...) HiLogPrint(type, level, domain, tag, ##__VA_ARGS__)
116 
117 /**
118  * @brief Check whether log of a specified domain, tag and level can be printed.
119  *
120  * @param domain macro:LOG_DOMAIN
121  * @param tag macro:LOG_TAG
122  * @param level enum:LogLevel
123  */
124 bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level);
125 
126 /**
127  * @brief Defines the function pointer type for the user-defined log processing function.
128  *
129  * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}.
130  * @param level Indicates the log level, which can be <b>LOG_DEBUG</b>, <b>LOG_INFO</b>, <b>LOG_WARN</b>,
131  * <b>LOG_ERROR</b>, and <b>LOG_FATAL</b>.
132  * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF.
133  * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior.
134  * @param msg Indicates the log message itself, which is a formatted log string.
135  * @since 11
136  */
137 typedef void (*LogCallback)(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
138     const char *msg);
139 
140 /**
141  * @brief Set the user-defined log processing function.
142  *
143  * After calling this function, the callback function implemented by the user can receive all hilogs of the
144  * current process.
145  * Note that it will not change the default behavior of hilog logs of the current process, no matter whether this
146  * interface is called or not. \n
147  *
148  * @param callback Indicates the callback function implemented by the user. If you do not need to process hilog logs,
149  * you can transfer a null pointer.
150  * @since 11
151  */
152 void LOG_SetCallback(LogCallback callback);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #ifdef HILOG_RAWFORMAT
159 #include "hilog/log_inner.h"
160 #endif
161 
162 #endif  // HIVIEWDFX_HILOG_C_H
163