• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 #include <iostream>
17 #include "mem/vm_handle.h"
18 #include "plugins/ets/runtime/ets_exceptions.h"
19 #include "plugins/ets/runtime/types/ets_string.h"
20 #include "runtime/include/mem/panda_string.h"
21 #include "runtime/include/mem/panda_containers.h"
22 #include "libpandabase/utils/utf.h"
23 #include "intrinsics.h"
24 #include "utility"
25 #include "utils/logger.h"
26 
27 #ifdef PANDA_TARGET_OHOS
28 #include <hilog/log.h>
29 #endif  // PANDA_TARGET_OHOS
30 
31 namespace ark::ets::intrinsics {
32 
33 enum class ConsoleLevel : int32_t {
34     ENUM_BEGIN = 0,
35     DEBUG = ENUM_BEGIN,
36     INFO = 1,
37     LOG = 2,
38     WARN = 3,
39     ERROR = 4,
40     PRINTLN = 5,
41     ENUM_END = PRINTLN
42 };
43 
44 #ifdef PANDA_TARGET_OHOS
45 namespace {
46 
LogPrint(ConsoleLevel level,const char * component,const char * msg)47 void LogPrint(ConsoleLevel level, const char *component, const char *msg)
48 {
49     constexpr static auto TAG = "ArkTSApp";
50     constexpr static unsigned int ARK_DOMAIN = 0x3D00;
51 #ifdef PANDA_OHOS_USE_INNER_HILOG
52     constexpr static OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_APP, ARK_DOMAIN, TAG};
53     switch (level) {
54         case ConsoleLevel::DEBUG:
55             OHOS::HiviewDFX::HiLog::Debug(LABEL, "%{public}s: %{public}s", component, msg);
56             break;
57         case ConsoleLevel::LOG:
58         case ConsoleLevel::INFO:
59             OHOS::HiviewDFX::HiLog::Info(LABEL, "%{public}s: %{public}s", component, msg);
60             break;
61         case ConsoleLevel::WARN:
62             OHOS::HiviewDFX::HiLog::Warn(LABEL, "%{public}s: %{public}s", component, msg);
63             break;
64         case ConsoleLevel::ERROR:
65             OHOS::HiviewDFX::HiLog::Error(LABEL, "%{public}s: %{public}s", component, msg);
66             break;
67         default:
68             UNREACHABLE();
69     }
70 #else
71     switch (level) {
72         case ConsoleLevel::DEBUG:
73             OH_LOG_Print(LOG_APP, LOG_DEBUG, ARK_DOMAIN, TAG, "%s: %s", component, msg);
74             break;
75         case ConsoleLevel::LOG:
76         case ConsoleLevel::INFO:
77             OH_LOG_Print(LOG_APP, LOG_INFO, ARK_DOMAIN, TAG, "%s: %s", component, msg);
78             break;
79         case ConsoleLevel::WARN:
80             OH_LOG_Print(LOG_APP, LOG_WARN, ARK_DOMAIN, TAG, "%s: %s", component, msg);
81             break;
82         case ConsoleLevel::ERROR:
83             OH_LOG_Print(LOG_APP, LOG_ERROR, ARK_DOMAIN, TAG, "%s: %s", component, msg);
84             break;
85         default:
86             UNREACHABLE();
87     }
88 #endif  // PANDA_OHOS_USE_INNER_HILOG
89 }
90 }  // namespace
91 #endif  // PANDA_TARGET_OHOS
92 
93 extern "C" {
StdConsolePrintString(ObjectHeader * header,EtsString * data,int32_t level)94 void StdConsolePrintString(ObjectHeader *header [[maybe_unused]], EtsString *data, int32_t level)
95 {
96     auto lvl = static_cast<ConsoleLevel>(level);
97     ASSERT(lvl >= ConsoleLevel::ENUM_BEGIN && lvl <= ConsoleLevel::ENUM_END);
98 
99     auto thread = ManagedThread::GetCurrent();
100     [[maybe_unused]] HandleScope<ObjectHeader *> scope(thread);
101     VMHandle<coretypes::String> dH(thread, data->GetCoreType());
102     auto res = PandaString(data->GetUtf8());
103 
104 #ifdef PANDA_TARGET_OHOS
105     if (lvl == ConsoleLevel::PRINTLN) {
106         std::cout << res << std::flush;
107     } else {
108         LogPrint(lvl, "arkts.console", res.data());
109     }
110 #else
111     (lvl == ConsoleLevel::ERROR ? std::cerr : std::cout) << res << std::flush;
112 #endif  // PANDA_TARGET_OHOS
113 }
114 }
115 
116 }  // namespace ark::ets::intrinsics
117