1/* 2 * Copyright (C) 2022-2024 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__) && !defined(__ANDROID__) 25 26#include "hilog/log.h" 27 28#undef LOG_TAG 29#define LOG_TAG "NETMANAGER_BASE" 30#undef LOG_DOMAIN 31#define LOG_DOMAIN 0xD0015B0 32 33#define NETMANAGER_BASE_HILOG_PRINT(Level, fmt, ...) \ 34 (void)HILOG_##Level(LOG_CORE, "[%{public}s %{public}d] " fmt, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__) 35 36#define NETMANAGER_BASE_LOGE(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(ERROR, fmt, ##__VA_ARGS__) 37 38#define NETMANAGER_BASE_LOGI(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(INFO, fmt, ##__VA_ARGS__) 39 40#define NETMANAGER_BASE_LOGD(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(DEBUG, fmt, ##__VA_ARGS__) 41 42#else 43 44#include <stdarg.h> 45#include <stdio.h> 46 47#include "securec.h" 48 49static constexpr uint32_t NETMANAGER_BASE_MAX_BUFFER_SIZE = 4096; 50 51static void NetManagerStandardStripFormatString(const std::string &prefix, std::string &str) 52{ 53 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) { 54 str.erase(pos, prefix.size()); 55 } 56} 57 58static 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_BASE_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_BASE_HILOG_PRINT(Level, fmt, ...) \ 80 NetManagerStandardPrintLog("NETMANAGER_BASE %s [%s %d] " fmt, #Level, MAKE_FILE_NAME, __LINE__, ##__VA_ARGS__) 81 82#define NETMANAGER_BASE_LOGE(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Error, fmt, ##__VA_ARGS__) 83 84#define NETMANAGER_BASE_LOGI(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Info, fmt, ##__VA_ARGS__) 85 86#define NETMANAGER_BASE_LOGD(fmt, ...) NETMANAGER_BASE_HILOG_PRINT(Debug, fmt, ##__VA_ARGS__) 87 88#endif /* !defined(_WIN32) && !defined(__APPLE__) */ 89 90#endif /* COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_LOG */