1 /**
2 * Copyright (c) 2023-2024 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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_LOGGER_H_
17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_LOGGER_H_
18
19 #include <string>
20 #include <cstdarg>
21 #include <securec.h>
22 #include "libpandabase/mem/mem.h"
23
24 // NOLINTBEGIN(cppcoreguidelines-macro-usage)
25
26 #define INTEROP_LOG(level) LOG(level, ETS_INTEROP_JS)
27
28 #ifdef PANDA_TARGET_OHOS
29 #include <hilog/log.h>
30
31 #define INTEROP_LOG_DEBUG(msg) OH_LOG_Print(LOG_APP, LOG_DEBUG, 0xFF00, "ts2ets", "%s", msg)
32 #define INTEROP_LOG_DEBUG_A(msg, ...) OH_LOG_Print(LOG_APP, LOG_DEBUG, 0xFF00, "ts2ets", msg, __VA_ARGS__)
33 #define INTEROP_LOG_INFO(msg) OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "ts2ets", "%s", msg)
34 #define INTEROP_LOG_INFO_A(msg, ...) OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "ts2ets", msg, __VA_ARGS__)
35 #define INTEROP_LOG_ERROR(msg) OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "ts2ets", msg)
36 #define INTEROP_LOG_ERROR_A(msg, ...) OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "ts2ets", msg, __VA_ARGS__)
37
38 #else
39
EtsLogMakeString(const char * fmt,...)40 inline std::string EtsLogMakeString(const char *fmt, ...)
41 {
42 va_list ap; // NOLINT(cppcoreguidelines-pro-type-vararg)
43 va_list apCopy; // NOLINT(cppcoreguidelines-pro-type-vararg)
44 va_start(ap, fmt);
45 va_copy(apCopy, ap);
46
47 int len = vsnprintf_s(nullptr, 0, PAGE_SIZE, fmt, ap);
48 if (len < 0) {
49 LOG(FATAL, ETS) << "interop_js: Cannot convert message to log buffer";
50 UNREACHABLE();
51 }
52
53 std::string res;
54 res.resize(static_cast<size_t>(len));
55 if (vsnprintf_s(res.data(), len + 1, len, fmt, apCopy) < 0) {
56 LOG(FATAL, ETS) << "interop_js: Cannot convert message to result string";
57 UNREACHABLE();
58 }
59
60 va_end(apCopy);
61 va_end(ap);
62 return res;
63 }
64
65 #define TS2ETS_LOGGER(level, ...) \
66 do { \
67 std::string msgTs2etsLogger = EtsLogMakeString(__VA_ARGS__); \
68 LOG(level, ETS) << "interop_js: " << msgTs2etsLogger; \
69 } while (0)
70
71 #define INTEROP_LOG_DEBUG(msg) TS2ETS_LOGGER(DEBUG, msg)
72 #define INTEROP_LOG_DEBUG_A(msg, ...) TS2ETS_LOGGER(DEBUG, msg, __VA_ARGS__)
73 #define INTEROP_LOG_INFO(msg) TS2ETS_LOGGER(INFO, msg)
74 #define INTEROP_LOG_INFO_A(msg, ...) TS2ETS_LOGGER(INFO, msg, __VA_ARGS__)
75 #define INTEROP_LOG_ERROR(msg) TS2ETS_LOGGER(INFO, msg)
76 #define INTEROP_LOG_ERROR_A(msg, ...) TS2ETS_LOGGER(INFO, msg, __VA_ARGS__)
77 #endif
78
79 // NOLINTEND(cppcoreguidelines-macro-usage)
80
81 #endif // PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_LOGGER_H_
82