mirror of
https://github.com/openharmony/ace_napi.git
synced 2026-08-24 21:51:37 -04:00
7b11996bb5
Signed-off-by: sunfei <sunfei.sun@huawei.com> Change-Id: Id319a8fe3c5705c2dfabad94c9fa714f089ae733
75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
/*
|
|
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#include "utils/log.h"
|
|
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
[[maybe_unused]] static void StripFormatString(const std::string& prefix, std::string& str)
|
|
{
|
|
for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) {
|
|
str.erase(pos, prefix.size());
|
|
}
|
|
}
|
|
|
|
#if defined(ANDROID_PLATFORM)
|
|
|
|
#include <android/log.h>
|
|
|
|
#define LOG_TAG "NAPI"
|
|
|
|
constexpr int LOG_LEVEL[] = { ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR,
|
|
ANDROID_LOG_FATAL };
|
|
|
|
NAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...)
|
|
{
|
|
std::string newFmt(fmt);
|
|
StripFormatString("{public}", newFmt);
|
|
StripFormatString("{private}", newFmt);
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
__android_log_vprint(
|
|
LOG_LEVEL[static_cast<int>(level)], LOG_TAG, newFmt.c_str(), args);
|
|
va_end(args);
|
|
}
|
|
|
|
#elif defined(MAC_PLATFORM) || defined(WINDOWS_PLATFORM)
|
|
|
|
#include <securec.h>
|
|
|
|
constexpr uint32_t MAX_BUFFER_SIZE = 4096;
|
|
|
|
NAPI_EXPORT void PrintLog(LogLevel level, const char* fmt, ...)
|
|
{
|
|
std::string newFmt(fmt);
|
|
StripFormatString("{public}", newFmt);
|
|
StripFormatString("{private}", newFmt);
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
|
|
char buf[MAX_BUFFER_SIZE] = { '\0' };
|
|
int ret = vsnprintf_s(buf, sizeof(buf), sizeof(buf) - 1, newFmt.c_str(), args);
|
|
if (ret < 0) {
|
|
return;
|
|
}
|
|
va_end(args);
|
|
|
|
printf("%s\r\n", buf);
|
|
fflush(stdout);
|
|
}
|
|
#endif
|