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 NSTACKX_LOG_H 17 #define NSTACKX_LOG_H 18 19 #include "nstackx_common_header.h" 20 #ifdef ENABLE_HILOG 21 #include <hilog/log.h> 22 #endif 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 enum { 28 NSTACKX_LOG_LEVEL_OFF = 0, 29 NSTACKX_LOG_LEVEL_FATAL = 1, 30 NSTACKX_LOG_LEVEL_ERROR = 2, 31 NSTACKX_LOG_LEVEL_WARNING = 3, 32 NSTACKX_LOG_LEVEL_INFO = 4, 33 NSTACKX_LOG_LEVEL_DEBUG = 5, 34 NSTACKX_LOG_LEVEL_END, 35 }; 36 37 #define NSTACKX_DEFAULT_TAG "nStackX" 38 39 /* Log module initialization */ 40 NSTACKX_EXPORT void SetLogLevel(uint32_t logLevel); 41 42 /* Get current log level */ 43 NSTACKX_EXPORT uint32_t GetLogLevel(void); 44 45 /* Actual implementation of "print", which is platform dependent */ 46 NSTACKX_EXPORT void PrintfImpl(const char *moduleName, uint32_t logLevel, const char *format, ...); 47 48 /* internal log implementation for windows */ 49 typedef void (*LogImplInternal)(const char *tag, uint32_t level, const char *format, va_list args); 50 51 /* Set log implementation */ 52 NSTACKX_EXPORT void SetLogImpl(LogImplInternal fn); 53 54 typedef void (*NstakcxLogCallback)(const char *moduleName, uint32_t logLevel, const char *format, ...); 55 NSTACKX_EXPORT_VARIABLE extern NstakcxLogCallback g_nstackxLogCallBack; 56 57 NSTACKX_EXPORT int32_t SetLogCallback(NstakcxLogCallback logCb); 58 NSTACKX_EXPORT void SetDefaultLogCallback(void); 59 60 #define NSTACKX_LOG_COMMON(moduleName, logLevel, moduleDebugLevel, format, ...) \ 61 do { \ 62 if (logLevel <= moduleDebugLevel && g_nstackxLogCallBack != NULL) { \ 63 g_nstackxLogCallBack(moduleName, logLevel, "%s:[%d] :" format "\n", \ 64 __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 65 } \ 66 } while (0) 67 68 #define LOGF(moduleName, format, ...) \ 69 NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_FATAL, GetLogLevel(), format, ##__VA_ARGS__) 70 #define LOGE(moduleName, format, ...) \ 71 NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_ERROR, GetLogLevel(), format, ##__VA_ARGS__) 72 #define LOGW(moduleName, format, ...) \ 73 NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_WARNING, GetLogLevel(), format, ##__VA_ARGS__) 74 #define LOGI(moduleName, format, ...) \ 75 NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_INFO, GetLogLevel(), format, ##__VA_ARGS__) 76 #define LOGD(moduleName, format, ...) \ 77 NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_DEBUG, GetLogLevel(), format, ##__VA_ARGS__) 78 79 #ifdef __cplusplus 80 } 81 #endif 82 83 #endif // NSTACKX_LOG_H 84