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#ifndef CONSOLE_CONSOLE_H
16#define CONSOLE_CONSOLE_H
17
18#include <map>
19#include <string>
20#include <thread>
21
22#include "commonlibrary/ets_utils/js_concurrent_module/common/helper/napi_helper.h"
23#include "commonlibrary/ets_utils/js_concurrent_module/common/helper/object_helper.h"
24#include "commonlibrary/ets_utils/js_concurrent_module/common/helper/error_helper.h"
25#include "napi/native_api.h"
26#include "napi/native_node_api.h"
27
28#ifdef WINDOWS_PLATFORM
29#define CONSOLE_PUBLIC_API __declspec(dllexport)
30#else
31#define CONSOLE_PUBLIC_API __attribute__((visibility ("default")))
32#endif
33
34namespace OHOS::JsSysModule {
35
36enum class LogLevel : uint32_t {
37    DEBUG = 0,
38    INFO,
39    WARN,
40    ERROR,
41    FATAL,
42};
43
44class Console {
45public:
46    Console() = default;
47    ~Console() = default;
48    CONSOLE_PUBLIC_API static void InitConsoleModule(napi_env env);
49    friend class ConsoleTest;
50
51private:
52    template<LogLevel LEVEL>
53    static napi_value ConsoleLog(napi_env env, napi_callback_info info);
54    static napi_value Count(napi_env env, napi_callback_info info);
55    static napi_value CountReset(napi_env env, napi_callback_info info);
56    static napi_value Dir(napi_env env, napi_callback_info info);
57    static napi_value Group(napi_env env, napi_callback_info info);
58    static napi_value GroupEnd(napi_env env, napi_callback_info info);
59    static napi_value ProcessTabularData(napi_env env, napi_value tabularData);
60    static napi_value Table(napi_env env, napi_callback_info info);
61    static napi_value Time(napi_env env, napi_callback_info info);
62    static napi_value TimeLog(napi_env env, napi_callback_info info);
63    static napi_value TimeEnd(napi_env env, napi_callback_info info);
64    static napi_value Trace(napi_env env, napi_callback_info info);
65    static napi_value TraceHybridStack(napi_env env, napi_callback_info info);
66    static napi_value Assert(napi_env env, napi_callback_info info);
67
68    static void LogPrint(LogLevel level, const char* content);
69    static std::string ParseLogContent(const std::vector<std::string>& params);
70    static std::string MakeLogContent(napi_env env, napi_callback_info info, size_t& argc,
71                                      size_t startIdx, bool format = true);
72    static std::string GetTimerOrCounterName(napi_env env, napi_callback_info info, size_t argc);
73    static void PrintTime(std::string timerName, double time, const std::string& log);
74    static void GraphTable(napi_env env, napi_value head, napi_value columns, const size_t& length);
75    static std::string RenderHead(napi_env env, napi_value Head, std::vector<size_t> columnWidths);
76    static void PrintRows(napi_env env, napi_value Rows, std::vector<size_t> columnWidths, size_t indexNum);
77    static std::string StringRepeat(size_t number, const std::string& tableChars);
78    static std::string ArrayJoin(std::vector<std::string> rowDivider, const std::string& tableChars);
79    static std::string GetStringAndStringWidth(napi_env env, napi_value element, size_t& stringLen);
80
81    static thread_local std::map<std::string, int64_t> timerMap;
82    static thread_local std::map<std::string, uint32_t> counterMap;
83    static thread_local std::string groupIndent;
84};
85} // namespace Commonlibrary::JsSysModule
86#endif // CONSOLE_CONSOLE_H