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 /* max log type */ 55 LOG_TYPE_MAX 56 } LogType; 57 58 /* Log level */ 59 typedef enum { 60 /* min log level */ 61 LOG_LEVEL_MIN = 0, 62 /* Designates lower priority log. */ 63 LOG_DEBUG = 3, 64 /* Designates useful information. */ 65 LOG_INFO = 4, 66 /* Designates hazardous situations. */ 67 LOG_WARN = 5, 68 /* Designates very serious errors. */ 69 LOG_ERROR = 6, 70 /* Designates major fatal anomaly. */ 71 LOG_FATAL = 7, 72 /* max log level */ 73 LOG_LEVEL_MAX, 74 } LogLevel; 75 76 /** 77 * @brief Get log of fatal level 78 */ 79 const char* GetLastFatalMessage(void); 80 81 /** 82 * @brief Print hilog 83 * 84 * @return If the log is successfully printed, returns the number of bytes written, 85 * if failed, returns -1. 86 */ 87 int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) 88 __attribute__((__format__(os_log, 5, 6))); 89 90 /** 91 * @brief Hilog C interface of different log level 92 * 93 * @param type enum:LogType 94 */ 95 #define HILOG_DEBUG(type, ...) ((void)HILOG_IMPL((type), LOG_DEBUG, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) 96 97 #define HILOG_INFO(type, ...) ((void)HILOG_IMPL((type), LOG_INFO, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) 98 99 #define HILOG_WARN(type, ...) ((void)HILOG_IMPL((type), LOG_WARN, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) 100 101 #define HILOG_ERROR(type, ...) ((void)HILOG_IMPL((type), LOG_ERROR, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) 102 103 #define HILOG_FATAL(type, ...) ((void)HILOG_IMPL((type), LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__)) 104 105 /** 106 * @brief Hilog C interface implementation 107 * 108 * @param type enum:LogType 109 * @param level enum:LogLevel 110 * @param domain macro:LOG_DOMAIN 111 * @param tag macro:LOG_TAG 112 */ 113 #define HILOG_IMPL(type, level, domain, tag, ...) HiLogPrint(type, level, domain, tag, ##__VA_ARGS__) 114 115 /** 116 * @brief Check whether log of a specified domain, tag and level can be printed. 117 * 118 * @param domain macro:LOG_DOMAIN 119 * @param tag macro:LOG_TAG 120 * @param level enum:LogLevel 121 */ 122 bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level); 123 124 /** 125 * @brief Defines the function pointer type for the user-defined log processing function. 126 * 127 * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}. 128 * @param level Indicates the log level, which can be <b>LOG_DEBUG</b>, <b>LOG_INFO</b>, <b>LOG_WARN</b>, 129 * <b>LOG_ERROR</b>, and <b>LOG_FATAL</b>. 130 * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF. 131 * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior. 132 * @param msg Indicates the log message itself, which is a formatted log string. 133 * @since 11 134 */ 135 typedef void (*LogCallback)(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, 136 const char *msg); 137 138 /** 139 * @brief Set the user-defined log processing function. 140 * 141 * After calling this function, the callback function implemented by the user can receive all hilogs of the 142 * current process. 143 * Note that it will not change the default behavior of hilog logs of the current process, no matter whether this 144 * interface is called or not. \n 145 * 146 * @param callback Indicates the callback function implemented by the user. If you do not need to process hilog logs, 147 * you can transfer a null pointer. 148 * @since 11 149 */ 150 void LOG_SetCallback(LogCallback callback); 151 152 #ifdef __cplusplus 153 } 154 #endif 155 156 #ifdef HILOG_RAWFORMAT 157 #include "hilog/log_inner.h" 158 #endif 159 160 #endif // HIVIEWDFX_HILOG_C_H 161