1 /* 2 * Copyright (c) 2021-2023 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 DFX_LOG_H 17 #define DFX_LOG_H 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 #ifdef DFX_NO_PRINT_LOG 24 #define DFXLOG_DEBUG(fmt, ...) 25 #define DFXLOG_INFO(fmt, ...) 26 #define DFXLOG_WARN(fmt, ...) 27 #define DFXLOG_ERROR(fmt, ...) 28 #define DFXLOG_FATAL(fmt, ...) 29 30 #define LOGD(fmt, ...) 31 #define LOGI(fmt, ...) 32 #define LOGW(fmt, ...) 33 #define LOGE(fmt, ...) 34 #define LOGF(fmt, ...) 35 36 #else 37 38 #define FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 39 40 #ifndef LOG_DOMAIN 41 #define LOG_DOMAIN 0xD002D11 42 #endif 43 44 #ifndef LOG_TAG 45 #define LOG_TAG "DfxFaultLogger" 46 #endif 47 48 typedef enum Level { 49 DEBUG = 0, 50 INFO, 51 WARN, 52 ERROR, 53 FATAL 54 } Level; 55 56 bool CheckDebugLevel(void); 57 void InitDebugFd(int fd); 58 int DfxLog(const Level logLevel, const unsigned int domain, const char* tag, const char *fmt, ...); 59 60 #define DFXLOG_DEBUG(fmt, ...) DfxLog(DEBUG, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__) 61 #define DFXLOG_INFO(fmt, ...) DfxLog(INFO, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__) 62 #define DFXLOG_WARN(fmt, ...) DfxLog(WARN, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__) 63 #define DFXLOG_ERROR(fmt, ...) DfxLog(ERROR, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__) 64 #define DFXLOG_FATAL(fmt, ...) DfxLog(FATAL, LOG_DOMAIN, LOG_TAG, fmt, ##__VA_ARGS__) 65 66 #define LOGD(fmt, ...) DfxLog(DEBUG, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 67 #define LOGI(fmt, ...) DfxLog(INFO, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 68 #define LOGW(fmt, ...) DfxLog(WARN, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 69 #define LOGE(fmt, ...) DfxLog(ERROR, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 70 #define LOGF(fmt, ...) DfxLog(FATAL, LOG_DOMAIN, LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 71 72 #ifndef LOG_ASSERT_MESSAGE 73 #define LOG_ASSERT_MESSAGE(condition, fmt, ...) \ 74 if (!(condition)) { \ 75 DfxLog(FATAL, LOG_DOMAIN, LOG_TAG, " assert failed: '%s' ", fmt, #condition, ##__VA_ARGS__); \ 76 } 77 #endif 78 79 #ifndef LOG_ASSERT 80 #define LOG_ASSERT(condition) LOG_ASSERT_MESSAGE(condition, "") 81 #endif 82 83 #endif 84 85 #ifdef __cplusplus 86 } 87 #endif 88 #endif // DFX_LOG_H 89