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 COMMUNICATIONNETSTACK_NETSTACK_LOG
17 #define COMMUNICATIONNETSTACK_NETSTACK_LOG
18
19 #include <cstring>
20 #include <string>
21
22 #define MAKE_FILE_NAME strrchr(__FILE__, '/') + 1
23
24 #if !defined(_WIN32) && !defined(__APPLE__)
25
26 #define LOG_DOMAIN 0xD0015B0
27
28 #define LOG_TAG "NetMgrSubsystem"
29
30 #include "hilog/log.h"
31
32 #define NETSTACK_HILOG_PRINT(Level, fmt, ...) \
33 (void)HILOG_##Level(LOG_CORE, "NETSTACK [%{public}s %{public}d] " fmt, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__)
34
35 #else
36
37 #include "securec.h"
38 #include <cstdarg>
39 #include <cstdio>
40
41 static constexpr uint32_t NETSTACK_MAX_BUFFER_SIZE = 4096;
42
NetStackStripFormatString(const std::string & prefix,std::string & str)43 static void NetStackStripFormatString(const std::string &prefix, std::string &str)
44 {
45 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
46 str.erase(pos, prefix.size());
47 }
48 }
49
NetStackPrintLog(const char * fmt,...)50 static void NetStackPrintLog(const char *fmt, ...)
51 {
52 std::string newFmt(fmt);
53 NetStackStripFormatString("{public}", newFmt);
54 NetStackStripFormatString("{private}", newFmt);
55
56 va_list args;
57 va_start(args, fmt);
58
59 char buf[NETSTACK_MAX_BUFFER_SIZE] = {0};
60 int ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, newFmt.c_str(), args);
61 if (ret < 0) {
62 va_end(args);
63 return;
64 }
65 va_end(args);
66
67 printf("%s\r\n", buf);
68 fflush(stdout);
69 }
70
71 #define NETSTACK_HILOG_PRINT(Level, fmt, ...) \
72 NetStackPrintLog("NETSTACK %s [%s %d] " fmt, #Level, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__)
73
74 #endif /* !defined(_WIN32) && !defined(__APPLE__) */
75
76 #define NETSTACK_LOGE(fmt, ...) NETSTACK_HILOG_PRINT(ERROR, fmt, ##__VA_ARGS__)
77
78 #define NETSTACK_LOGI(fmt, ...) NETSTACK_HILOG_PRINT(INFO, fmt, ##__VA_ARGS__)
79
80 #define NETSTACK_LOGD(fmt, ...) NETSTACK_HILOG_PRINT(DEBUG, fmt, ##__VA_ARGS__)
81
82 #endif /* COMMUNICATIONNETSTACK_NETSTACK_LOG */
83