133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci#include "utils/log.h" 1633eb0b6dSopenharmony_ci 1733eb0b6dSopenharmony_ci#include <cstdint> 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci[[maybe_unused]] static void StripFormatString(const std::string& prefix, std::string& str) 2033eb0b6dSopenharmony_ci{ 2133eb0b6dSopenharmony_ci for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) { 2233eb0b6dSopenharmony_ci str.erase(pos, prefix.size()); 2333eb0b6dSopenharmony_ci } 2433eb0b6dSopenharmony_ci} 2533eb0b6dSopenharmony_ci 2633eb0b6dSopenharmony_ci#if defined(ANDROID_PLATFORM) 2733eb0b6dSopenharmony_ci 2833eb0b6dSopenharmony_ci#include <android/log.h> 2933eb0b6dSopenharmony_ci 3033eb0b6dSopenharmony_ci#define LOG_TAG "NAPI" 3133eb0b6dSopenharmony_ci 3233eb0b6dSopenharmony_ciconstexpr int LOG_LEVEL[] = { ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, 3333eb0b6dSopenharmony_ci ANDROID_LOG_FATAL }; 3433eb0b6dSopenharmony_ci 3533eb0b6dSopenharmony_ciNAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...) 3633eb0b6dSopenharmony_ci{ 3733eb0b6dSopenharmony_ci std::string newFmt(fmt); 3833eb0b6dSopenharmony_ci StripFormatString("{public}", newFmt); 3933eb0b6dSopenharmony_ci StripFormatString("{private}", newFmt); 4033eb0b6dSopenharmony_ci va_list args; 4133eb0b6dSopenharmony_ci va_start(args, fmt); 4233eb0b6dSopenharmony_ci __android_log_vprint(LOG_LEVEL[static_cast<int>(level)], LOG_TAG, newFmt.c_str(), args); 4333eb0b6dSopenharmony_ci va_end(args); 4433eb0b6dSopenharmony_ci} 4533eb0b6dSopenharmony_ci 4633eb0b6dSopenharmony_ci#elif defined(MAC_PLATFORM) || defined(WINDOWS_PLATFORM) || defined(IOS_PLATFORM) || defined(LINUX_PLATFORM) 4733eb0b6dSopenharmony_ci 4833eb0b6dSopenharmony_ci#include <securec.h> 4933eb0b6dSopenharmony_ci 5033eb0b6dSopenharmony_ciconstexpr uint32_t MAX_BUFFER_SIZE = 4096; 5133eb0b6dSopenharmony_ci 5233eb0b6dSopenharmony_ciNAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...) 5333eb0b6dSopenharmony_ci{ 5433eb0b6dSopenharmony_ci std::string newFmt(fmt); 5533eb0b6dSopenharmony_ci StripFormatString("{public}", newFmt); 5633eb0b6dSopenharmony_ci StripFormatString("{private}", newFmt); 5733eb0b6dSopenharmony_ci 5833eb0b6dSopenharmony_ci va_list args; 5933eb0b6dSopenharmony_ci va_start(args, fmt); 6033eb0b6dSopenharmony_ci 6133eb0b6dSopenharmony_ci char buf[MAX_BUFFER_SIZE] = { '\0' }; 6233eb0b6dSopenharmony_ci int ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, newFmt.c_str(), args); 6333eb0b6dSopenharmony_ci va_end(args); 6433eb0b6dSopenharmony_ci if (ret < 0) { 6533eb0b6dSopenharmony_ci return; 6633eb0b6dSopenharmony_ci } 6733eb0b6dSopenharmony_ci 6833eb0b6dSopenharmony_ci printf("%s\r\n", buf); 6933eb0b6dSopenharmony_ci fflush(stdout); 7033eb0b6dSopenharmony_ci} 7133eb0b6dSopenharmony_ci#endif 72