2022-07-06 06:12:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ECMASCRIPT_LOG_H
|
|
|
|
#define ECMASCRIPT_LOG_H
|
|
|
|
|
2022-08-08 08:20:33 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
2022-07-06 06:12:54 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2022-08-08 08:20:33 +00:00
|
|
|
#include "ecmascript/common.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_HILOG
|
2022-07-06 06:12:54 +00:00
|
|
|
#include "hilog/log.h"
|
|
|
|
|
2022-08-17 01:10:10 +00:00
|
|
|
constexpr static unsigned int ARK_DOMAIN = 0xD003F00;
|
2022-07-06 06:12:54 +00:00
|
|
|
constexpr static auto TAG = "ArkCompiler";
|
2022-08-17 01:10:10 +00:00
|
|
|
constexpr static OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, ARK_DOMAIN, TAG};
|
2022-07-06 06:12:54 +00:00
|
|
|
|
2022-07-09 10:43:58 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_VERBOSE_LEVEL_LOG
|
|
|
|
// print Debug level log if enable Verbose log
|
|
|
|
#define LOG_VERBOSE LOG_DEBUG
|
|
|
|
#else
|
|
|
|
#define LOG_VERBOSE LOG_LEVEL_MIN
|
|
|
|
#endif
|
2022-08-08 08:20:33 +00:00
|
|
|
#endif // ENABLE_HILOG
|
|
|
|
|
|
|
|
enum Level {
|
|
|
|
VERBOSE,
|
|
|
|
DEBUG,
|
|
|
|
INFO,
|
|
|
|
WARN,
|
|
|
|
ERROR,
|
|
|
|
FATAL,
|
|
|
|
};
|
2022-07-09 10:43:58 +00:00
|
|
|
|
2022-07-06 06:12:54 +00:00
|
|
|
namespace panda::ecmascript {
|
2022-08-17 08:53:35 +00:00
|
|
|
class JSRuntimeOptions;
|
2022-08-08 08:20:33 +00:00
|
|
|
class PUBLIC_API Log {
|
|
|
|
public:
|
2022-08-17 08:53:35 +00:00
|
|
|
static void Initialize(const JSRuntimeOptions &options);
|
2022-08-08 08:20:33 +00:00
|
|
|
static Level GetLevel()
|
|
|
|
{
|
|
|
|
return level_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetLevel(Level level)
|
|
|
|
{
|
|
|
|
level_ = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void SetLogLevelFromString(const std::string& level);
|
|
|
|
static Level level_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef ENABLE_HILOG
|
2022-07-06 06:12:54 +00:00
|
|
|
template<LogLevel level>
|
2022-08-08 08:20:33 +00:00
|
|
|
class HiLog {
|
2022-07-06 06:12:54 +00:00
|
|
|
public:
|
2022-08-08 08:20:33 +00:00
|
|
|
HiLog() = default;
|
|
|
|
~HiLog()
|
2022-07-06 06:12:54 +00:00
|
|
|
{
|
2022-07-09 10:43:58 +00:00
|
|
|
if constexpr (level == LOG_LEVEL_MIN) {
|
|
|
|
// print nothing
|
|
|
|
} else if constexpr (level == LOG_DEBUG) {
|
2022-07-06 06:12:54 +00:00
|
|
|
OHOS::HiviewDFX::HiLog::Debug(LABEL, "%{public}s", stream_.str().c_str());
|
|
|
|
} else if constexpr (level == LOG_INFO) {
|
|
|
|
OHOS::HiviewDFX::HiLog::Info(LABEL, "%{public}s", stream_.str().c_str());
|
|
|
|
} else if constexpr (level == LOG_WARN) {
|
|
|
|
OHOS::HiviewDFX::HiLog::Warn(LABEL, "%{public}s", stream_.str().c_str());
|
|
|
|
} else if constexpr (level == LOG_ERROR) {
|
|
|
|
OHOS::HiviewDFX::HiLog::Error(LABEL, "%{public}s", stream_.str().c_str());
|
|
|
|
} else {
|
|
|
|
OHOS::HiviewDFX::HiLog::Fatal(LABEL, "%{public}s", stream_.str().c_str());
|
2022-07-21 12:38:13 +00:00
|
|
|
std::abort();
|
2022-07-06 06:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
template<class type>
|
|
|
|
std::ostream &operator <<(type input)
|
|
|
|
{
|
|
|
|
stream_ << input;
|
|
|
|
return stream_;
|
|
|
|
}
|
|
|
|
|
2022-08-08 08:20:33 +00:00
|
|
|
private:
|
|
|
|
std::ostringstream stream_;
|
|
|
|
};
|
|
|
|
#endif // ENABLE_HILOG
|
2022-08-11 03:14:26 +00:00
|
|
|
template<Level level>
|
2022-08-08 08:20:33 +00:00
|
|
|
class StdLog {
|
|
|
|
public:
|
|
|
|
StdLog() = default;
|
|
|
|
~StdLog()
|
|
|
|
{
|
|
|
|
std::cerr << stream_.str().c_str() << std::endl;
|
2022-08-11 03:14:26 +00:00
|
|
|
if constexpr (level == FATAL) {
|
|
|
|
std::abort();
|
|
|
|
}
|
2022-08-08 08:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class type>
|
|
|
|
std::ostream &operator <<(type input)
|
|
|
|
{
|
|
|
|
stream_ << input;
|
|
|
|
return stream_;
|
|
|
|
}
|
|
|
|
|
2022-07-06 06:12:54 +00:00
|
|
|
private:
|
|
|
|
std::ostringstream stream_;
|
|
|
|
};
|
|
|
|
|
2022-08-18 07:41:19 +00:00
|
|
|
#ifdef PANDA_TARGET_ANDROID
|
|
|
|
template<Level level>
|
|
|
|
class PUBLIC_API AndroidLog {
|
|
|
|
public:
|
|
|
|
AndroidLog() = default;
|
|
|
|
~AndroidLog();
|
|
|
|
|
|
|
|
template<class type>
|
|
|
|
std::ostream &operator <<(type input)
|
|
|
|
{
|
|
|
|
stream_ << input;
|
|
|
|
return stream_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostringstream stream_;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-08-08 08:20:33 +00:00
|
|
|
#ifdef ENABLE_HILOG
|
2022-08-29 08:24:48 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_VERBOSE_LEVEL_LOG
|
2022-08-27 02:03:37 +00:00
|
|
|
static bool LOGGABLE_VERBOSE = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_VERBOSE);
|
|
|
|
#else
|
|
|
|
static bool LOGGABLE_VERBOSE = false;
|
|
|
|
#endif
|
2022-08-29 08:24:48 +00:00
|
|
|
static bool LOGGABLE_DEBUG = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_DEBUG);
|
2022-08-27 02:03:37 +00:00
|
|
|
static bool LOGGABLE_INFO = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_INFO);
|
|
|
|
static bool LOGGABLE_WARN = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_WARN);
|
|
|
|
static bool LOGGABLE_ERROR = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_ERROR);
|
|
|
|
static bool LOGGABLE_FATAL = HiLogIsLoggable(ARK_DOMAIN, TAG, LOG_FATAL);
|
|
|
|
|
|
|
|
#define LOG_ECMA(level) panda::ecmascript::LOGGABLE_##level && panda::ecmascript::HiLog<LOG_##level>()
|
2022-08-18 07:41:19 +00:00
|
|
|
#elif defined(PANDA_TARGET_ANDROID)
|
|
|
|
#define LOG_ECMA(level) panda::ecmascript::AndroidLog<(level)>()
|
|
|
|
#else
|
2022-08-11 03:14:26 +00:00
|
|
|
#define LOG_ECMA(level) ((level) >= panda::ecmascript::Log::GetLevel()) && panda::ecmascript::StdLog<(level)>()
|
2022-08-18 07:41:19 +00:00
|
|
|
#endif
|
2022-08-11 03:14:26 +00:00
|
|
|
} // namespace panda::ecmascript
|
2022-07-06 06:12:54 +00:00
|
|
|
#endif // ECMASCRIPT_LOG_H
|