• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 #include "hilog/log.h"
27 
28 #define NETSTACK_LOG_TAG "NetMgrSubsystem"
29 
30 #define NETSTACK_LOG_DOMAIN 0xD0015B0
31 
32 static constexpr OHOS::HiviewDFX::HiLogLabel NETSTACK_LOG_LABEL = {LOG_CORE, NETSTACK_LOG_DOMAIN, NETSTACK_LOG_TAG};
33 
34 #define NETSTACK_HILOG_PRINT(Level, fmt, ...)                                                                        \
35     (void)OHOS::HiviewDFX::HiLog::Level(NETSTACK_LOG_LABEL, "NETSTACK [%{public}s %{public}d] " fmt, MAKE_FILE_NAME, \
36                                         __LINE__, ##__VA_ARGS__)
37 
38 #else
39 
40 #ifdef _WIN32
41 #undef MAKE_FILE_NAME
42 #define MAKE_FILE_NAME (strrchr(__FILE__, '\\') + 1)
43 #endif /* _WIN32 */
44 
45 #include <cstdarg>
46 #include <cstdio>
47 
48 #include "securec.h"
49 
50 static constexpr uint32_t NETSTACK_MAX_BUFFER_SIZE = 4096;
51 
NetStackStripFormatString(const std::string & prefix,std::string & str)52 static void NetStackStripFormatString(const std::string &prefix, std::string &str)
53 {
54     for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
55         str.erase(pos, prefix.size());
56     }
57 }
58 
NetStackPrintLog(const char * fmt,...)59 static void NetStackPrintLog(const char *fmt, ...)
60 {
61     std::string newFmt(fmt);
62     NetStackStripFormatString("{public}", newFmt);
63     NetStackStripFormatString("{private}", newFmt);
64 
65     va_list args;
66     va_start(args, fmt);
67 
68     char buf[NETSTACK_MAX_BUFFER_SIZE] = {0};
69     int ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, newFmt.c_str(), args);
70     if (ret < 0) {
71         va_end(args);
72         return;
73     }
74     va_end(args);
75 
76     printf("%s\r\n", buf);
77     fflush(stdout);
78 }
79 
80 #define NETSTACK_HILOG_PRINT(Level, fmt, ...) \
81     NetStackPrintLog("NETSTACK %s [%s %d] " fmt, #Level, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__)
82 
83 #endif /* !defined(_WIN32) && !defined(__APPLE__) */
84 
85 #define NETSTACK_LOGE(fmt, ...) NETSTACK_HILOG_PRINT(Error, fmt, ##__VA_ARGS__)
86 
87 #define NETSTACK_LOGI(fmt, ...) NETSTACK_HILOG_PRINT(Info, fmt, ##__VA_ARGS__)
88 
89 #endif /* COMMUNICATIONNETSTACK_NETSTACK_LOG */