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