1 /*
2 * Copyright (c) 2022 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 "ecmascript/js_runtime_options.h"
17 #include "ecmascript/log.h"
18 #include "generated/base_options.h"
19
20 #ifdef PANDA_TARGET_ANDROID
21 #include <android/log.h>
22 #endif
23
24 namespace panda::ecmascript {
25 Level Log::level_ = Level::ERROR;
26 ComponentMark Log::components_ = Component::ALL;
SetLogLevelFromString(const std::string & level)27 void Log::SetLogLevelFromString(const std::string& level)
28 {
29 if (level == "fatal") {
30 level_ = FATAL;
31 }
32 if (level == "error") {
33 level_ = ERROR;
34 }
35 if (level == "warning") {
36 level_ = WARN;
37 }
38 if (level == "info") {
39 level_ = INFO;
40 }
41 if (level == "debug") {
42 level_ = DEBUG;
43 }
44 if (level == "verbose") {
45 level_ = VERBOSE;
46 }
47 }
48
SetLogComponentFromString(const std::vector<std::string> & components)49 void Log::SetLogComponentFromString(const std::vector<std::string>& components)
50 {
51 components_ = Component::NONE;
52 for (const auto &component : components) {
53 if (component == "all") {
54 components_ = Component::ALL;
55 return;
56 }
57 if (component == "gc") {
58 components_ |= Component::GC;
59 continue;
60 }
61 if (component == "ecmascript") {
62 components_ |= Component::ECMASCRIPT;
63 continue;
64 }
65 if (component == "interpreter") {
66 components_ |= Component::INTERPRETER;
67 continue;
68 }
69 if (component == "debugger") {
70 components_ |= Component::DEBUGGER;
71 continue;
72 }
73 if (component == "compiler") {
74 components_ |= Component::COMPILER;
75 continue;
76 }
77 if (component == "builtins") {
78 components_ |= Component::BUILTINS;
79 continue;
80 }
81 }
82 }
83
PrintLogger(int32_t,int32_t level,const char *,const char *,const char * message)84 int32_t Log::PrintLogger(int32_t, int32_t level, const char *, const char *, const char *message)
85 {
86 switch (level) {
87 case Logger::PandaLog2MobileLog::VERBOSE:
88 LOG_ECMA(VERBOSE) << message;
89 break;
90 case Logger::PandaLog2MobileLog::DEBUG:
91 LOG_ECMA(DEBUG) << message;
92 break;
93 case Logger::PandaLog2MobileLog::INFO:
94 LOG_ECMA(INFO) << message;
95 break;
96 case Logger::PandaLog2MobileLog::WARN:
97 LOG_ECMA(WARN) << message;
98 break;
99 case Logger::PandaLog2MobileLog::ERROR:
100 LOG_ECMA(ERROR) << message;
101 break;
102 case Logger::PandaLog2MobileLog::FATAL:
103 LOG_ECMA(FATAL) << message;
104 break;
105 default:
106 LOG_ECMA(DEBUG) << message;
107 break;
108 }
109 return 0;
110 }
111
Initialize(const JSRuntimeOptions & options)112 void Log::Initialize(const JSRuntimeOptions &options)
113 {
114 // For ArkTS runtime log
115 if (options.WasSetLogFatal()) {
116 level_ = FATAL;
117 SetLogComponentFromString(options.GetLogFatal());
118 } else if (options.WasSetLogError()) {
119 level_ = ERROR;
120 SetLogComponentFromString(options.GetLogError());
121 } else if (options.WasSetLogWarning()) {
122 level_ = WARN;
123 SetLogComponentFromString(options.GetLogWarning());
124 } else if (options.WasSetLogInfo()) {
125 level_ = INFO;
126 SetLogComponentFromString(options.GetLogInfo());
127 } else if (options.WasSetLogDebug()) {
128 level_ = DEBUG;
129 SetLogComponentFromString(options.GetLogDebug());
130 } else {
131 SetLogLevelFromString(options.GetLogLevel());
132 SetLogComponentFromString(options.GetLogComponents());
133 }
134
135 // For runtime core log
136 base_options::Options baseOptions("");
137 baseOptions.SetLogLevel(options.GetLogLevel());
138 baseOptions.SetLogComponents({ "all" });
139 Logger::Initialize(baseOptions);
140 Logger::SetMobileLogPrintEntryPointByPtr(reinterpret_cast<void *>(Log::PrintLogger));
141 }
142
143 #ifdef PANDA_TARGET_ANDROID
144 const char *tag = "ArkCompiler";
145 template<>
~AndroidLog()146 AndroidLog<VERBOSE>::~AndroidLog()
147 {
148 __android_log_write(ANDROID_LOG_VERBOSE, tag, stream_.str().c_str());
149 }
150
151 template<>
~AndroidLog()152 AndroidLog<DEBUG>::~AndroidLog()
153 {
154 __android_log_write(ANDROID_LOG_DEBUG, tag, stream_.str().c_str());
155 }
156
157 template<>
~AndroidLog()158 AndroidLog<INFO>::~AndroidLog()
159 {
160 __android_log_write(ANDROID_LOG_INFO, tag, stream_.str().c_str());
161 }
162
163 template<>
~AndroidLog()164 AndroidLog<WARN>::~AndroidLog()
165 {
166 __android_log_write(ANDROID_LOG_WARN, tag, stream_.str().c_str());
167 }
168
169 template<>
~AndroidLog()170 AndroidLog<ERROR>::~AndroidLog()
171 {
172 __android_log_write(ANDROID_LOG_ERROR, tag, stream_.str().c_str());
173 }
174
175 template<>
~AndroidLog()176 AndroidLog<FATAL>::~AndroidLog()
177 {
178 __android_log_write(ANDROID_LOG_FATAL, tag, stream_.str().c_str());
179 std::abort();
180 }
181 #endif
182 } // namespace panda::ecmascript