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