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 #include "nstackx_log.h"
17
18 #include <errno.h>
19 #include "securec.h"
20 #include "nstackx_util.h"
21 #include "nstackx_error.h"
22 #define TAG "nStackXLog"
23 static uint32_t g_logLevel = NSTACKX_LOG_LEVEL_INFO;
24
25 #ifdef BUILD_FOR_WINDOWS
DefaultLogImpl(const char * tag,uint32_t level,const char * format,va_list args)26 static void DefaultLogImpl(const char *tag, uint32_t level, const char *format, va_list args)
27 {
28 SYSTEMTIME st = {0};
29
30 GetLocalTime(&st);
31 printf("%02d-%02d %02d:%02d:%02d.%03d %d %d %d %s: ", st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
32 st.wMilliseconds, GetCurrentProcessId(), GetCurrentThreadId(), level, tag);
33 vprintf(format, args);
34 }
35
36 static LogImplInternal g_logImpl = DefaultLogImpl;
37 #endif
38
GetLogLevel(void)39 uint32_t GetLogLevel(void)
40 {
41 return g_logLevel;
42 }
43
SetLogLevel(uint32_t logLevel)44 void SetLogLevel(uint32_t logLevel)
45 {
46 if (logLevel >= NSTACKX_LOG_LEVEL_END) {
47 return;
48 }
49 g_logLevel = logLevel;
50 }
51
SetLogImpl(LogImplInternal fn)52 void SetLogImpl(LogImplInternal fn)
53 {
54 if (fn == NULL) {
55 return;
56 }
57 }
58 #ifdef BUILD_FOR_WINDOWS
59 #ifndef NEED_EXPORT_VARIABLE
60 #define NEED_EXPORT_VARIABLE
61 #endif
62 #endif
63
64 NstakcxLogCallback g_nstackxLogCallBack = PrintfImpl;
65
SetLogCallback(NstakcxLogCallback logCb)66 int32_t SetLogCallback(NstakcxLogCallback logCb)
67 {
68 if (logCb == NULL) {
69 LOGE(TAG, "log callback is null");
70 return NSTACKX_EINVAL;
71 }
72 if (logCb == g_nstackxLogCallBack) {
73 LOGW(TAG, "log callback is the same");
74 return NSTACKX_EOK;
75 }
76 LOGI(TAG, "log callback changed");
77 g_nstackxLogCallBack = logCb;
78 return NSTACKX_EOK;
79 }
80
SetDefaultLogCallback(void)81 void SetDefaultLogCallback(void)
82 {
83 g_nstackxLogCallBack = PrintfImpl;
84 return;
85 }