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