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 namespace OHOS::JsSysModule { 29 30 enum class LogLevel : uint32_t { 31 DEBUG = 0, 32 INFO, 33 WARN, 34 ERROR, 35 FATAL, 36 }; 37 38 class Console { 39 public: 40 Console() = default; 41 ~Console() = default; 42 static void InitConsoleModule(napi_env env); 43 friend class ConsoleTest; 44 45 private: 46 template<LogLevel LEVEL> 47 static napi_value ConsoleLog(napi_env env, napi_callback_info info); 48 static napi_value Count(napi_env env, napi_callback_info info); 49 static napi_value CountReset(napi_env env, napi_callback_info info); 50 static napi_value Dir(napi_env env, napi_callback_info info); 51 static napi_value Group(napi_env env, napi_callback_info info); 52 static napi_value GroupEnd(napi_env env, napi_callback_info info); 53 static napi_value ProcessTabularData(napi_env env, napi_value tabularData); 54 static napi_value Table(napi_env env, napi_callback_info info); 55 static napi_value Time(napi_env env, napi_callback_info info); 56 static napi_value TimeLog(napi_env env, napi_callback_info info); 57 static napi_value TimeEnd(napi_env env, napi_callback_info info); 58 static napi_value Trace(napi_env env, napi_callback_info info); 59 static napi_value TraceHybridStack(napi_env env, napi_callback_info info); 60 static napi_value Assert(napi_env env, napi_callback_info info); 61 62 static void LogPrint(LogLevel level, const char* content); 63 static std::string ParseLogContent(const std::vector<std::string>& params); 64 static std::string MakeLogContent(napi_env env, napi_callback_info info, size_t& argc, 65 size_t startIdx, bool format = true); 66 static std::string GetTimerOrCounterName(napi_env env, napi_callback_info info, size_t argc); 67 static void PrintTime(std::string timerName, double time, const std::string& log); 68 static void GraphTable(napi_env env, napi_value head, napi_value columns, const size_t& length); 69 static std::string RenderHead(napi_env env, napi_value Head, std::vector<size_t> columnWidths); 70 static void PrintRows(napi_env env, napi_value Rows, std::vector<size_t> columnWidths, size_t indexNum); 71 static std::string StringRepeat(size_t number, const std::string& tableChars); 72 static std::string ArrayJoin(std::vector<std::string> rowDivider, const std::string& tableChars); 73 static std::string GetStringAndStringWidth(napi_env env, napi_value element, size_t& stringLen); 74 75 static thread_local std::map<std::string, int64_t> timerMap; 76 static thread_local std::map<std::string, uint32_t> counterMap; 77 static thread_local std::string groupIndent; 78 }; 79 } // namespace Commonlibrary::JsSysModule 80 #endif // CONSOLE_CONSOLE_H