• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
40 // CC-OFFNXT(G.FUD.06) solid logic, ODR
EtsLogMakeString(const char * fmt,...)41 inline std::string EtsLogMakeString(const char *fmt, ...)
42 {
43     va_list ap;      // NOLINT(cppcoreguidelines-pro-type-vararg)
44     va_list apCopy;  // NOLINT(cppcoreguidelines-pro-type-vararg)
45     va_start(ap, fmt);
46     va_copy(apCopy, ap);
47 
48     int len = vsnprintf_s(nullptr, 0, PAGE_SIZE, fmt, ap);
49     if (len < 0) {
50         LOG(FATAL, ETS) << "interop_js: Cannot convert message to log buffer";
51         UNREACHABLE();
52     }
53 
54     std::string res;
55     res.resize(static_cast<size_t>(len));
56     if (vsnprintf_s(res.data(), len + 1, len, fmt, apCopy) < 0) {
57         LOG(FATAL, ETS) << "interop_js: Cannot convert message to result string";
58         UNREACHABLE();
59     }
60 
61     va_end(apCopy);
62     va_end(ap);
63     return res;
64 }
65 
66 #define TS2ETS_LOGGER(level, ...)                                    \
67     do {                                                             \
68         std::string msgTs2etsLogger = EtsLogMakeString(__VA_ARGS__); \
69         LOG(level, ETS) << "interop_js: " << msgTs2etsLogger;        \
70     } while (0)
71 
72 #define INTEROP_LOG_DEBUG(msg) TS2ETS_LOGGER(DEBUG, msg)
73 #define INTEROP_LOG_DEBUG_A(msg, ...) TS2ETS_LOGGER(DEBUG, msg, __VA_ARGS__)
74 #define INTEROP_LOG_INFO(msg) TS2ETS_LOGGER(INFO, msg)
75 #define INTEROP_LOG_INFO_A(msg, ...) TS2ETS_LOGGER(INFO, msg, __VA_ARGS__)
76 #define INTEROP_LOG_ERROR(msg) TS2ETS_LOGGER(INFO, msg)
77 #define INTEROP_LOG_ERROR_A(msg, ...) TS2ETS_LOGGER(INFO, msg, __VA_ARGS__)
78 #endif
79 
80 // NOLINTEND(cppcoreguidelines-macro-usage)
81 
82 #endif  // PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_LOGGER_H_
83