1 /*
2 * Copyright (C) 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 COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_LOG
17 #define COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_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 NETMANAGER_BASE_LOG_TAG "NetMgrSubsystem"
29
30 #define NETMANAGER_BASE_LOG_DOMAIN 0xD0015B0
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel NETMANAGER_BASE_LOG_LABEL = {LOG_CORE, NETMANAGER_BASE_LOG_DOMAIN, NETMANAGER_BASE_LOG_TAG};
33
34 #define NETMANAGER_BASE_HILOG_PRINT(Level, fmt, ...) \
35 (void)OHOS::HiviewDFX::HiLog::Level(NETMANAGER_BASE_LOG_LABEL, "NETMANAGER_BASE [%{public}s %{public}d] " fmt, MAKE_FILE_NAME, \
36 __LINE__, ##__VA_ARGS__)
37
38 #else
39
40 #include <stdarg.h>
41 #include <stdio.h>
42
43 #include "securec.h"
44
45 static constexpr uint32_t NETMANAGER_BASE_MAX_BUFFER_SIZE = 4096;
46
NetManagerStandardStripFormatString(const std::string & prefix,std::string & str)47 static void NetManagerStandardStripFormatString(const std::string &prefix, std::string &str)
48 {
49 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
50 str.erase(pos, prefix.size());
51 }
52 }
53
NetManagerStandardPrintLog(const char * fmt,...)54 static void NetManagerStandardPrintLog(const char *fmt, ...)
55 {
56 std::string newFmt(fmt);
57 NetManagerStandardStripFormatString("{public}", newFmt);
58 NetManagerStandardStripFormatString("{private}", newFmt);
59
60 va_list args;
61 va_start(args, fmt);
62
63 char buf[NETMANAGER_BASE_MAX_BUFFER_SIZE] = {0};
64 int ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, newFmt.c_str(), args);
65 if (ret < 0) {
66 va_end(args);
67 return;
68 }
69 va_end(args);
70
71 printf("%s\r\n", buf);
72 fflush(stdout);
73 }
74
75 #define NETMANAGER_BASE_HILOG_PRINT(Level, fmt, ...) \
76 NetManagerStandardPrintLog("NETMANAGER_BASE %s [%s %d] " fmt, #Level, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__)
77
78 #endif /* !defined(_WIN32) && !defined(__APPLE__) */
79
80 #define NETMANAGER_BASE_LOGE(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Error, fmt, ##__VA_ARGS__)
81
82 #define NETMANAGER_BASE_LOGI(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Info, fmt, ##__VA_ARGS__)
83
84 #define NETMANAGER_BASE_LOGD(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Debug, fmt, ##__VA_ARGS__)
85
86 #endif /* COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_LOG */
87