• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FILE_NAME (__builtin_strrchr("/" __FILE__, '/') + 1)
61 #define NSTACKX_LOG_COMMON(moduleName, logLevel, moduleDebugLevel, format, ...)                                      \
62     do {                                                                                                             \
63         if (logLevel <= moduleDebugLevel && g_nstackxLogCallBack != NULL) {                                          \
64             g_nstackxLogCallBack(                                                                                    \
65                 moduleName, logLevel, "[%s:%d] %s# " format "\n", FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__); \
66         }                                                                                                            \
67     } while (0)
68 
69 #define LOGF(moduleName, format, ...) \
70     NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_FATAL, GetLogLevel(), format, ##__VA_ARGS__)
71 #define LOGE(moduleName, format, ...) \
72     NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_ERROR, GetLogLevel(), format, ##__VA_ARGS__)
73 #define LOGW(moduleName, format, ...) \
74     NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_WARNING, GetLogLevel(), format, ##__VA_ARGS__)
75 #define LOGI(moduleName, format, ...) \
76     NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_INFO, GetLogLevel(), format, ##__VA_ARGS__)
77 #define LOGD(moduleName, format, ...) \
78     NSTACKX_LOG_COMMON(moduleName, NSTACKX_LOG_LEVEL_DEBUG, GetLogLevel(), format, ##__VA_ARGS__)
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 #endif // NSTACKX_LOG_H
85