From 2249f2e3e95abc2b84358a45a7332c758963b5cf Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 11 Jan 2022 04:24:27 +0000 Subject: [PATCH 1/3] add hilog js-api Signed-off-by: aajwy <13051180828@163.com> --- interfaces/js/kits/napi/BUILD.gn | 51 +++ .../js/kits/napi/src/common/napi/n_class.cpp | 61 ++++ .../js/kits/napi/src/common/napi/n_class.h | 83 +++++ .../js/kits/napi/src/common/napi/n_exporter.h | 46 +++ .../kits/napi/src/common/napi/n_func_arg.cpp | 104 ++++++ .../js/kits/napi/src/common/napi/n_func_arg.h | 96 ++++++ .../js/kits/napi/src/common/napi/n_val.cpp | 319 ++++++++++++++++++ .../js/kits/napi/src/common/napi/n_val.h | 117 +++++++ .../js/kits/napi/src/common/napi/uni_header.h | 25 ++ .../js/kits/napi/src/common/napi/util.h | 112 ++++++ .../src/hilog/include/context/hilog_napi.h | 36 ++ .../hilog/include/context/hilog_napi_base.h | 45 +++ interfaces/js/kits/napi/src/hilog/module.cpp | 40 +++ .../js/kits/napi/src/hilog/src/hilog_napi.cpp | 66 ++++ .../napi/src/hilog/src/hilog_napi_base.cpp | 218 ++++++++++++ .../native/innerkits/include/hilog/log_cpp.h | 30 +- ohos.build | 3 +- 17 files changed, 1436 insertions(+), 16 deletions(-) create mode 100644 interfaces/js/kits/napi/BUILD.gn create mode 100644 interfaces/js/kits/napi/src/common/napi/n_class.cpp create mode 100644 interfaces/js/kits/napi/src/common/napi/n_class.h create mode 100644 interfaces/js/kits/napi/src/common/napi/n_exporter.h create mode 100644 interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp create mode 100644 interfaces/js/kits/napi/src/common/napi/n_func_arg.h create mode 100644 interfaces/js/kits/napi/src/common/napi/n_val.cpp create mode 100644 interfaces/js/kits/napi/src/common/napi/n_val.h create mode 100644 interfaces/js/kits/napi/src/common/napi/uni_header.h create mode 100644 interfaces/js/kits/napi/src/common/napi/util.h create mode 100644 interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h create mode 100644 interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h create mode 100644 interfaces/js/kits/napi/src/hilog/module.cpp create mode 100644 interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp create mode 100644 interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp diff --git a/interfaces/js/kits/napi/BUILD.gn b/interfaces/js/kits/napi/BUILD.gn new file mode 100644 index 0000000..bf73d0c --- /dev/null +++ b/interfaces/js/kits/napi/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("libhilog_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ "include" ] +} + +ohos_source_set("libhilognapi_src") { + include_dirs = [ + "//third_party/node/src", + "//utils/native/base/include", + ] + sources = [ + "src/common/napi/n_class.cpp", + "src/common/napi/n_func_arg.cpp", + "src/common/napi/n_val.cpp", + "src/hilog/module.cpp", + "src/hilog/src/hilog_napi.cpp", + "src/hilog/src/hilog_napi_base.cpp", + #"src/hilog/src/js_hilog.cpp", + ] + + deps = [ "//utils/native/base:utilsecurec" ] + + external_deps = [ + "hilog_native:libhilog", + "napi:ace_napi", + ] +} + +ohos_shared_library("libhilognapi") { + deps = [ ":libhilognapi_src" ] + output_name = "libhilog" + relative_install_dir = "module" + subsystem_name = "hiviewdfx" + part_name = "hilog_native" +} \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_class.cpp b/interfaces/js/kits/napi/src/common/napi/n_class.cpp new file mode 100644 index 0000000..c69f08d --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_class.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "n_class.h" + +#include +#include + +namespace OHOS { +namespace HiviewDFX { +using namespace std; +NClass &NClass::GetInstance() +{ + static NClass nClass; + return nClass; +} + +tuple NClass::DefineClass(napi_env env, string className, napi_callback constructor, + vector &&properties) +{ + napi_value classVal = nullptr; + napi_status stat = napi_define_class(env, className.c_str(), className.length(), constructor, + nullptr, properties.size(), properties.data(), &classVal); + return { stat == napi_ok, classVal }; +} + +napi_value NClass::InstantiateClass(napi_env env, string className, vector args) +{ + NClass &nClass = NClass::GetInstance(); + lock_guard(nClass.exClassMapLock); + + auto it = nClass.exClassMap.find(className); + if (it == nClass.exClassMap.end()) { + return nullptr; + } + napi_value cons = nullptr; + napi_status status = napi_get_reference_value(env, it->second, &cons); + if (status != napi_ok) { + return nullptr; + } + napi_value instance = nullptr; + status = napi_new_instance(env, cons, args.size(), args.data(), &instance); + if (status != napi_ok) { + return nullptr; + } + return instance; +} +} // namespace HiviewDFX +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_class.h b/interfaces/js/kits/napi/src/common/napi/n_class.h new file mode 100644 index 0000000..cb68f97 --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_class.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HIVIEWDFX_NAPI_NCALSS +#define HIVIEWDFX_NAPI_NCALSS + +#pragma once + +#include "uni_header.h" + +#include +#include +#include +#include +#include +#include + +namespace OHOS { +namespace HiviewDFX { +class NClass { +public: + NClass(const NClass &) = delete; + NClass &operator = (const NClass &) = delete; + ~NClass() {}; + static NClass &GetInstance(); + + static std::tuple DefineClass(napi_env env, + std::string className, + napi_callback constructor, + std::vector &&properties); + static bool SaveClass(napi_env env, std::string className, napi_value exClass); + static napi_value InstantiateClass(napi_env env, std::string className, std::vector args); + + template static T *GetEntityOf(napi_env env, napi_value objStat) + { + if (!env || !objStat) { + return nullptr; + } + T *t = nullptr; + napi_status status = napi_unwrap(env, objStat, (void **)&t); + if (status != napi_ok) { + return nullptr; + } + return t; + } + + template static bool SetEntityFor(napi_env env, napi_value obj, std::unique_ptr entity) + { + napi_status status = napi_wrap( + env, + obj, + entity.get(), + [](napi_env env, void *data, void *hint) { + auto entity = static_cast(data); + delete entity; + }, + nullptr, + nullptr); + entity.release(); + return status == napi_ok; + } + +private: + NClass() = default; + std::map exClassMap; + std::mutex exClassMapLock; +}; +} // namespace HiviewDFX +} // namespace OHOS + +#endif // HIVIEWDFX_NAPI_NCALSS \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_exporter.h b/interfaces/js/kits/napi/src/common/napi/n_exporter.h new file mode 100644 index 0000000..03ee878 --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_exporter.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HIVIEWDFX_NAPI_EXPOTER +#define HIVIEWDFX_NAPI_EXPOTER + +#pragma once + +#include +#include + +#include "n_val.h" + +namespace OHOS { +namespace HiviewDFX { +class NExporter { +public: + NExporter() : exports_(nullptr, nullptr) {}; + + virtual ~NExporter() = default; + + NExporter(napi_env env, napi_value exports) : exports_(env, exports) {}; + + virtual bool Export(napi_env env, napi_value exports) = 0; + + virtual std::string GetClassName() = 0; + +protected: + NVal exports_; +}; +} // namespace HiviewDFX +} // namespace OHOS + +#endif // HIVIEWDFX_NAPI_EXPOTER \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp b/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp new file mode 100644 index 0000000..45f773b --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "n_func_arg.h" +#include +#include + +namespace OHOS { +namespace HiviewDFX { +using namespace std; + +NFuncArg::NFuncArg(napi_env env, napi_callback_info info) : env_(env), info_(info) {} + +NFuncArg::~NFuncArg() {} + +void NFuncArg::SetArgc(size_t argc) +{ + argc_ = argc; +} + +void NFuncArg::SetThisVar(napi_value thisVar) +{ + thisVar_ = thisVar; +} + +size_t NFuncArg::GetArgc(void) const +{ + return argc_; +} + +napi_value NFuncArg::GetThisVar(void) const +{ + return thisVar_; +} + +napi_value NFuncArg::GetArg(size_t argPos) const +{ + return (argPos < GetArgc()) ? argv_[argPos] : nullptr; +} + +napi_value NFuncArg::operator[](size_t argPos) const +{ + return GetArg(argPos); +} + +bool NFuncArg::InitArgs(std::function argcChecker) +{ + SetArgc(0); + argv_.reset(); + + size_t argc; + napi_value thisVar; + napi_status status = napi_get_cb_info(env_, info_, &argc, nullptr, &thisVar, nullptr); + if (status != napi_ok) { + return false; + } + if (argc) { + argv_ = make_unique(argc); + status = napi_get_cb_info(env_, info_, &argc, argv_.get(), &thisVar, nullptr); + if (status != napi_ok) { + return false; + } + } + SetArgc(argc); + SetThisVar(thisVar); + + return argcChecker(); +} + +bool NFuncArg::InitArgs(size_t argc) +{ + return InitArgs([argc, this]() { + size_t realArgc = GetArgc(); + if (argc != realArgc) { + return false; + } + return true; + }); +} + +bool NFuncArg::InitArgs(size_t minArgc, size_t maxArgc) +{ + return InitArgs([minArgc, maxArgc, this]() { + size_t realArgc = GetArgc(); + if (minArgc > realArgc || maxArgc < realArgc) { + return false; + } + return true; + }); +} +} // namespace HiviewDFX +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_func_arg.h b/interfaces/js/kits/napi/src/common/napi/n_func_arg.h new file mode 100644 index 0000000..0785903 --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_func_arg.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HIVIEWDFX_NAPI_ARG +#define HIVIEWDFX_NAPI_ARG + +#pragma once + +#include +#include +#include +#include +#include "n_val.h" +#include "uni_header.h" + +namespace OHOS { +namespace HiviewDFX { +enum NARG_CNT { + ZERO = 0, + ONE = 1, + TWO = 2, + THREE = 3, + FOUR = 4, + FIVE = 5, + SIX = 6, + SEVEN = 7, + EIGHT = 8, + NINE = 9, + TEN = 10, + ELEVEN = 11, + TWELVE = 12, +}; + +enum NARG_POS { + FIRST = 0, + SECOND = 1, + THIRD = 2, + FOURTH = 3, + FIFTH = 4, + SIXTH = 5, + SEVENTH = 6, + EIGHTH = 7, + NINTH = 8, + TENTH = 9, + ELEVENTH = 10, + TWELVETH = 11, +}; + +class NFuncArg final { +public: + NFuncArg(napi_env env, napi_callback_info info); + + virtual ~NFuncArg(); + + bool InitArgs(size_t argc); + + bool InitArgs(size_t minArgc, size_t maxArgc); + + size_t GetArgc() const; + + napi_value GetThisVar() const; + + napi_value operator[](size_t idx) const; + + napi_value GetArg(size_t argPos) const; + +private: + napi_env env_ = nullptr; + napi_callback_info info_ = nullptr; + + size_t argc_ = 0; + std::unique_ptr argv_ = {nullptr}; + napi_value thisVar_ = nullptr; + + bool InitArgs(std::function argcChecker); + + void SetArgc(size_t argc); + + void SetThisVar(napi_value thisVar); +}; +} // namespace HiviewDFX +} // namespace OHOS + +#endif // HIVIEWDFX_NAPI_ARG \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.cpp b/interfaces/js/kits/napi/src/common/napi/n_val.cpp new file mode 100644 index 0000000..e97f317 --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_val.cpp @@ -0,0 +1,319 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "n_val.h" + +#include +#include + +namespace OHOS { +namespace HiviewDFX { +using namespace std; + +NVal::NVal(napi_env nEnv, napi_value nVal = nullptr) : env_(nEnv), val_(nVal) {} + +NVal::operator bool() const +{ + return env_ && val_; +} + +bool NVal::TypeIs(napi_valuetype expType) const +{ + if (!*this) { + return false; + } + + napi_valuetype valueType; + napi_typeof(env_, val_, &valueType); + + if (expType != valueType) { + return false; + } + return true; +} + +tuple NVal::IsArray() const +{ + bool res = false; + napi_status status = napi_is_array(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple, size_t> NVal::ToUTF8String() const +{ + size_t strLen = 0; + napi_status status = napi_get_value_string_utf8(env_, val_, nullptr, -1, &strLen); + if (status != napi_ok) { + return {false, nullptr, 0}; + } + + size_t bufLen = strLen + 1; + unique_ptr str = make_unique(bufLen); + status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen); + return make_tuple(status == napi_ok, move(str), strLen); +} + +tuple, size_t> NVal::ToUTF16String() const +{ +#ifdef FILE_SUBSYSTEM_DEV_ON_PC + size_t strLen = 0; + napi_status status = napi_get_value_string_utf16(env_, val_, nullptr, -1, &strLen); + if (status != napi_ok) { + return { false, nullptr, 0 }; + } + + auto str = make_unique(++strLen); + status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr); + if (status != napi_ok) { + return { false, nullptr, 0 }; + } + + strLen = reinterpret_cast(str.get() + strLen) - reinterpret_cast(str.get()); + auto strRet = unique_ptr(reinterpret_cast(str.release())); + return { true, move(strRet), strLen }; +#else + // Note that quickjs doesn't support utf16 + return ToUTF8String(); +#endif +} + +tuple NVal::ToPointer() const +{ + void *res = nullptr; + napi_status status = napi_get_value_external(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple NVal::IsTypeArray() const +{ + bool res = false; + napi_status status = napi_is_typedarray(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple NVal::ToBool() const +{ + bool flag = false; + napi_status status = napi_get_value_bool(env_, val_, &flag); + return make_tuple(status == napi_ok, flag); +} + +tuple NVal::ToDouble() const +{ + double res = 0.0; + napi_status status = napi_get_value_double(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple NVal::ToInt32() const +{ + int32_t res = 0; + napi_status status = napi_get_value_int32(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple NVal::ToInt64() const +{ + int64_t res = 0; + napi_status status = napi_get_value_int64(env_, val_, &res); + return make_tuple(status == napi_ok, res); +} + +tuple NVal::ToArraybuffer() const +{ + void *buf = nullptr; + size_t bufLen = 0; + bool status = napi_get_arraybuffer_info(env_, val_, &buf, &bufLen); + return make_tuple(status == napi_ok, buf, bufLen); +} + +tuple NVal::ToTypedArray() const +{ + napi_typedarray_type type; + napi_value in_array_buffer = nullptr; + size_t byte_offset; + size_t length; + void *data = nullptr; + napi_status status = napi_get_typedarray_info(env_, val_, &type, &length, (void **) &data, &in_array_buffer, + &byte_offset); + return make_tuple(status == napi_ok, type, data, length); +} + +tuple NVal::ToDataview() const +{ + size_t bufLen = 0; + void *buf = nullptr; + napi_value arraybuffer = nullptr; + size_t byteoff = 0; + bool status = napi_get_dataview_info(env_, val_, &bufLen, &buf, &arraybuffer, &byteoff); + return make_tuple(status == napi_ok, buf, bufLen); +} + +tuple NVal::ToTypedArrayInfo() const +{ + napi_typedarray_type type; + napi_value in_array_buffer = nullptr; + size_t byte_offset; + size_t length; + void *data = nullptr; + napi_status status = + napi_get_typedarray_info(env_, val_, &type, &length, (void **)&data, &in_array_buffer, &byte_offset); + return make_tuple(status == napi_ok, data, length, byte_offset, type); +} + +bool NVal::HasProp(string propName) const +{ + bool res = false; + + if (!env_ || !val_ || !TypeIs(napi_object)) + return false; + napi_status status = napi_has_named_property(env_, val_, propName.c_str(), &res); + return (status == napi_ok) && res; +} + +NVal NVal::GetProp(string propName) const +{ + if (!HasProp(propName)) { + return {env_, nullptr}; + } + napi_value prop = nullptr; + napi_status status = napi_get_named_property(env_, val_, propName.c_str(), &prop); + if (status != napi_ok) { + return {env_, nullptr}; + } + return NVal(env_, prop); +} + +bool NVal::AddProp(vector &&propVec) const +{ + if (!TypeIs(napi_valuetype::napi_object)) { + return false; + } + napi_status status = napi_define_properties(env_, val_, propVec.size(), propVec.data()); + if (status != napi_ok) { + return false; + } + return true; +} + +bool NVal::AddProp(string propName, napi_value val) const +{ + if (!TypeIs(napi_valuetype::napi_object) || HasProp(propName)) { + return false; + } + + napi_status status = napi_set_named_property(env_, val_, propName.c_str(), val); + if (status != napi_ok) { + return false; + } + return true; +} + +NVal NVal::CreateUndefined(napi_env env) +{ + napi_value res = nullptr; + napi_get_undefined(env, &res); + return {env, res}; +} + +NVal NVal::CreateNull(napi_env env) +{ + napi_value res = nullptr; + napi_get_null(env, &res); + return {env, res}; +} + +NVal NVal::CreateInt64(napi_env env, int64_t val) +{ + napi_value res = nullptr; + napi_create_int64(env, val, &res); + return {env, res}; +} + +NVal NVal::CreateObject(napi_env env) +{ + napi_value res = nullptr; + napi_create_object(env, &res); + return {env, res}; +} + +NVal NVal::CreateBool(napi_env env, bool val) +{ + napi_value res = nullptr; + napi_get_boolean(env, val, &res); + return {env, res}; +} + +NVal NVal::CreateUTF8String(napi_env env, std::string str) +{ + napi_value res = nullptr; + napi_create_string_utf8(env, str.c_str(), str.length(), &res); + return {env, res}; +} + +NVal NVal::CreateUint8Array(napi_env env, void *buf, size_t bufLen) +{ + napi_value output_buffer = nullptr; + napi_create_external_arraybuffer(env, buf, bufLen, + [](napi_env env, void *finalize_data, void *finalize_hint) { free(finalize_data); }, + NULL, &output_buffer); + napi_value output_array = nullptr; + napi_create_typedarray(env, napi_uint8_array, bufLen, output_buffer, 0, &output_array); + return {env, output_array}; +} + +NVal NVal::CreateDouble(napi_env env, double val) +{ + napi_value res = nullptr; + napi_create_double(env, val, &res); + return {env, res}; +} + +napi_property_descriptor NVal::DeclareNapiProperty(const char *name, napi_value val) +{ + return {(name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiStaticProperty(const char *name, napi_value val) +{ + return {(name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiFunction(const char *name, napi_callback func) +{ + return {(name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiStaticFunction(const char *name, napi_callback func) +{ + return {(name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiGetter(const char *name, napi_callback getter) +{ + return {(name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiSetter(const char *name, napi_callback setter) +{ + return {(name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr}; +} + +napi_property_descriptor NVal::DeclareNapiGetterSetter(const char *name, napi_callback getter, napi_callback setter) +{ + return {(name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr}; +} +} // namespace HiviewDFX +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.h b/interfaces/js/kits/napi/src/common/napi/n_val.h new file mode 100644 index 0000000..571f012 --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/n_val.h @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HIVIEWDFX_NAPI_VAL +#define HIVIEWDFX_NAPI_VAL + +#pragma once + +#include +#include +#include +#include "uni_header.h" + +namespace OHOS { +namespace HiviewDFX { +class NVal final { +public: + NVal() = default; + + NVal(napi_env nEnv, napi_value nVal); + + NVal &operator=(const NVal &) = default; + + virtual ~NVal() = default; + + // NOTE! env_ and val_ is LIKELY to be null + napi_env env_ = nullptr; + napi_value val_ = nullptr; + + explicit operator bool() const; + + bool TypeIs(napi_valuetype expType) const; + + /* SHOULD ONLY BE USED FOR EXPECTED TYPE */ + std::tuple, size_t> ToUTF8String() const; + + std::tuple, size_t> ToUTF16String() const; + + std::tuple ToPointer() const; + + std::tuple ToBool() const; + + std::tuple ToDouble() const; + + std::tuple ToInt32() const; + + std::tuple ToInt64() const; + + std::tuple ToArraybuffer() const; + + std::tuple ToTypedArray() const; + + std::tuple IsTypeArray() const; + + std::tuple ToDataview() const; + + std::tuple IsArray() const; + + std::tuple ToTypedArrayInfo() const; + + /* Static helpers to create js objects */ + static NVal CreateUndefined(napi_env env); + + static NVal CreateNull(napi_env env); + + static NVal CreateInt64(napi_env env, int64_t val); + + static NVal CreateObject(napi_env env); + + static NVal CreateBool(napi_env env, bool val); + + static NVal CreateUTF8String(napi_env env, std::string str); + + static NVal CreateUint8Array(napi_env env, void *buf, size_t bufLen); + + static NVal CreateDouble(napi_env env, double val); + + /* SHOULD ONLY BE USED FOR OBJECT */ + bool HasProp(std::string propName) const; + + NVal GetProp(std::string propName) const; + + bool AddProp(std::vector &&propVec) const; + + bool AddProp(std::string propName, napi_value nVal) const; + + /* Static helpers to create prop of js objects */ + static napi_property_descriptor DeclareNapiProperty(const char *name, napi_value val); + + static napi_property_descriptor DeclareNapiStaticProperty(const char *name, napi_value val); + + static napi_property_descriptor DeclareNapiFunction(const char *name, napi_callback func); + + static napi_property_descriptor DeclareNapiStaticFunction(const char *name, napi_callback func); + + static napi_property_descriptor DeclareNapiGetter(const char *name, napi_callback getter); + + static napi_property_descriptor DeclareNapiSetter(const char *name, napi_callback setter); + + static inline napi_property_descriptor DeclareNapiGetterSetter(const char *name, + napi_callback getter, napi_callback setter); +}; +} // namespace HiviewDFX +} // namespace OHOS + +#endif // HIVIEWDFX_NAPI_VAL \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/uni_header.h b/interfaces/js/kits/napi/src/common/napi/uni_header.h new file mode 100644 index 0000000..23d5fac --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/uni_header.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HIVIEW_NAPI_HEADER +#define HIVIEW_NAPI_HEADER + +#pragma once + +#include +#else +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#endif // HIVIEW_NAPI_HEADER \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/util.h b/interfaces/js/kits/napi/src/common/napi/util.h new file mode 100644 index 0000000..90132af --- /dev/null +++ b/interfaces/js/kits/napi/src/common/napi/util.h @@ -0,0 +1,112 @@ +#include +#include +#include + +namespace util { +template +struct function_traits_defs { + static constexpr size_t arity = sizeof...(Args); + + using result_type = ReturnType; + + template + struct arg { + using type = typename std::tuple_element>::type; + }; +}; + +template +struct function_traits_impl; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits_impl + : function_traits_defs {}; + +template +struct function_traits + : function_traits_impl {}; + +template +struct function_traits + : function_traits_impl {}; + +template +struct indices { + using next = indices; +}; +template +struct build_indices { + using type = typename build_indices::type::next; +}; +template <> +struct build_indices<0> { + using type = indices<>; +}; +template +using BuildIndices = typename build_indices::type; + +namespace details { +template , + typename ReturnT = typename Traits::result_type> +ReturnT do_call(FuncType& func, VecType& args, indices) { + return func(args[I]...); +} +} // namespace details + +template < + typename FuncType, + typename VecType, + typename Traits = function_traits, + typename ReturnT = typename Traits::result_type> +ReturnT unpack_caller(FuncType& func, VecType& args) +{ + return details::do_call(func, args, BuildIndices()); +} +} // namespace util \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h new file mode 100644 index 0000000..3949cd9 --- /dev/null +++ b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HIVIEWDFX_NAPI_HILOG +#define HIVIEWDFX_NAPI_HILOG + +#include "hilog_napi_base.h" +#include "../../../common/napi/n_exporter.h" + +namespace OHOS { +namespace HiviewDFX { +class HilogNapi : public HilogNapiBase, public NExporter { +public: + inline static const std::string className = "hilog"; + bool Export(napi_env env, napi_value exports) override; + std::string GetClassName() override; + HilogNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; + explicit HilogNapi() {}; + ~HilogNapi() {}; +}; +} // namespace HiviewDFX +} // namespace OHOS + +#endif // HIVIEWDFX_NAPI_HILOG \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h new file mode 100644 index 0000000..f17681f --- /dev/null +++ b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HIVIEWDFX_NAPI_HILOG_BASE +#define HIVIEWDFX_NAPI_HILOG_BASE + +#include "../../../common/napi/n_exporter.h" +#ifdef __cplusplus +extern "C" { +#endif + +namespace OHOS { +namespace HiviewDFX { +class HilogNapiBase { +public: + static napi_value debug(napi_env env, napi_callback_info info); + static napi_value HilogImpl(napi_env env, napi_callback_info info, int level); + static napi_value info(napi_env env, napi_callback_info info); + static napi_value error(napi_env env, napi_callback_info info); + static napi_value warn(napi_env env, napi_callback_info info); + static napi_value fatal(napi_env env, napi_callback_info info); + static napi_value isLoggable(napi_env env, napi_callback_info info); + static napi_value parseNapiValue(napi_env env, napi_callback_info info, + napi_value element, std::string& newString); +}; +} // namespace HiviewDFX +} // namespace OHOS + +#ifdef __cplusplus +} +#endif + +#endif // HIVIEWDFX_NAPI_HILOG_BASE \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/module.cpp b/interfaces/js/kits/napi/src/hilog/module.cpp new file mode 100644 index 0000000..f29453b --- /dev/null +++ b/interfaces/js/kits/napi/src/hilog/module.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "include/context/hilog_napi.h" +#include "../common/napi/n_val.h" +using namespace std; + +namespace OHOS { +namespace HiviewDFX { + +static napi_value Export(napi_env env, napi_value exports) +{ + std::vector> products; + products.emplace_back(make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export(env, exports)) { + return nullptr; + } + } + return exports; +} + +NAPI_MODULE(hilog, Export) +} // namespace HiviewDFX +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp new file mode 100644 index 0000000..87a979e --- /dev/null +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../include/context/hilog_napi.h" +#include "../../common/napi/n_class.h" +#include "../../common/napi/n_func_arg.h" +#include "hilog/log.h" +#include "hilog/log_c.h" + +#ifdef __cplusplus +extern "C" { +#endif + +namespace OHOS { +namespace HiviewDFX { +using namespace std; +bool HilogNapi::Export(napi_env env, napi_value exports) +{ + napi_value DEBUG = nullptr; + napi_value INFO = nullptr; + napi_value WARN = nullptr; + napi_value ERROR = nullptr; + napi_value FATAL = nullptr; + napi_create_int32(env, LogLevel::LOG_DEBUG, &DEBUG); + napi_create_int32(env, LogLevel::LOG_INFO, &INFO); + napi_create_int32(env, LogLevel::LOG_WARN, &WARN); + napi_create_int32(env, LogLevel::LOG_ERROR, &ERROR); + napi_create_int32(env, LogLevel::LOG_FATAL, &FATAL); + return exports_.AddProp( + { + NVal::DeclareNapiFunction("debug", HilogNapiBase::debug), + NVal::DeclareNapiFunction("info", HilogNapiBase::info), + NVal::DeclareNapiFunction("error", HilogNapiBase::error), + NVal::DeclareNapiFunction("warn", HilogNapiBase::warn), + NVal::DeclareNapiFunction("fatal", HilogNapiBase::fatal), + NVal::DeclareNapiFunction("isLoggable", HilogNapiBase::isLoggable), + NVal::DeclareNapiStaticProperty("DEBUG", DEBUG), + NVal::DeclareNapiStaticProperty("INFO", INFO), + NVal::DeclareNapiStaticProperty("WARN", WARN), + NVal::DeclareNapiStaticProperty("ERROR", ERROR), + NVal::DeclareNapiStaticProperty("FATAL", FATAL), + }); +} + +string HilogNapi::GetClassName() +{ + return HilogNapi::className; +} +} // namespace HiviewDFX +} // namespace OHOS + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp new file mode 100644 index 0000000..4bfff08 --- /dev/null +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "../include/context/hilog_napi_base.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "../../common/napi/n_func_arg.h" +#include "../../common/napi/n_class.h" +#include "hilog/log.h" +#include "hilog/log_c.h" +#include "securec.h" + +#ifdef __cplusplus +extern "C" { +#endif +namespace OHOS { +namespace HiviewDFX { +using namespace std; +#define DEFAULT_LOG_TYPE LOG_CORE +#define MIN_NUMBER 3 +#define MAX_NUMBER 100 + +void ReplaceOnce(string& base, string src, string dst, bool& flag) +{ + if (flag == true) { + return; + } + size_t pos = 0; + size_t srclen = src.size(); + size_t dstlen = dst.size(); + if ((pos = base.find(src, pos)) != string::npos) { + base.replace(pos, srclen, dst); + pos += dstlen; + flag = true; + } + return; +} + +napi_value HilogNapiBase::isLoggable(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + + if (!funcArg.InitArgs(NARG_CNT::THREE)) { + return nullptr; + } + bool succ = false; + int32_t domain; + tie(succ, domain) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + return nullptr; + } + int32_t level; + tie(succ, level) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + if (!succ) { + return nullptr; + } + unique_ptr tag; + tie(succ, tag, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + return nullptr; + } + bool res = HiLogIsLoggable(domain, tag.get(), static_cast(level)); + return NVal::CreateBool(env, res).val_; +} + +napi_value HilogNapiBase::debug(napi_env env, napi_callback_info info) +{ + return HilogImpl(env, info, LOG_DEBUG); +} + +napi_value HilogNapiBase::info(napi_env env, napi_callback_info info) +{ + return HilogImpl(env, info, LOG_INFO); +} + +napi_value HilogNapiBase::warn(napi_env env, napi_callback_info info) +{ + return HilogImpl(env, info, LOG_WARN); +} + +napi_value HilogNapiBase::error(napi_env env, napi_callback_info info) +{ + return HilogImpl(env, info, LOG_ERROR); +} + +napi_value HilogNapiBase::fatal(napi_env env, napi_callback_info info) +{ + return HilogImpl(env, info, LOG_FATAL); +} + +napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, + napi_value element, string& newString) +{ + bool succ = false; + napi_valuetype type; + napi_status typeStatus = napi_typeof(env, element, &type); + bool flag = false; + if (typeStatus != napi_ok) { + return nullptr; + } + if (type == napi_number) { + napi_value elmString; + napi_status objectStatus = napi_coerce_to_string(env, element, &elmString); + if (objectStatus != napi_ok) { + return nullptr; + } + unique_ptr name; + tie(succ, name, ignore) = NVal(env, elmString).ToUTF8String(); + if (!succ) { + return nullptr; + } + ReplaceOnce(newString, "%{public}d", name.get(), flag); + ReplaceOnce(newString, "%{private}d", name.get(), flag); + ReplaceOnce(newString, "%d", name.get(), flag); + ReplaceOnce(newString, "%{public}f", name.get(), flag); + ReplaceOnce(newString, "%{private}f", name.get(), flag); + ReplaceOnce(newString, "%f", name.get(), flag); + } else if (type == napi_string) { + unique_ptr name; + tie(succ, name, ignore) = NVal(env, element).ToUTF8String(); + if (!succ) { + return nullptr; + } + ReplaceOnce(newString, "%{public}s", name.get(), flag); + ReplaceOnce(newString, "%{private}s", name.get(), flag); + ReplaceOnce(newString, "%s", name.get(), flag); + } else if (type == napi_object) { + napi_value elmString; + napi_status objectStatus = napi_coerce_to_string(env, element, &elmString); + if (objectStatus != napi_ok) { + return nullptr; + } + unique_ptr name; + tie(succ, name, ignore) = NVal(env, elmString).ToUTF8String(); + if (!succ) { + return nullptr; + } + ReplaceOnce(newString, "%{public}o", name.get(), flag); + ReplaceOnce(newString, "%{private}o", name.get(), flag); + ReplaceOnce(newString, "%o", name.get(), flag); + } else { + NAPI_ASSERT(env, false, "type mismatch"); + } + return nullptr; +} +napi_value HilogNapiBase::HilogImpl(napi_env env, napi_callback_info info, int level) +{ + NFuncArg funcArg(env, info); + funcArg.InitArgs(MIN_NUMBER, MAX_NUMBER); + bool succ = false; + int32_t domain; + tie(succ, domain) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!succ) { + return nullptr; + } + unique_ptr tag; + tie(succ, tag, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); + if (!succ) { + return nullptr; + } + unique_ptr fmt; + tie(succ, fmt, ignore) = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String(); + if (!succ) { + return nullptr; + } + string fmtString = fmt.get(); + bool res = false; + napi_value array = funcArg[NARG_POS::FOURTH]; + napi_is_array(env, array, &res); + string newString = fmtString; + if (res == false) { + for (size_t i = MIN_NUMBER; i < funcArg.GetArgc(); i++) { + napi_value argsVal = funcArg[i]; + parseNapiValue(env, info, argsVal, newString); + } + } else { + if (funcArg.GetArgc() != MIN_NUMBER + 1) { + NAPI_ASSERT(env, false, "Argc mismatch"); + return nullptr; + } + uint32_t length; + napi_status lengthStatus = napi_get_array_length(env, array, &length); + if (lengthStatus != napi_ok) { + return nullptr; + } + uint32_t i; + for (i = 0; i < length; i++) { + napi_value element; + napi_status eleStatus = napi_get_element(env, array, i, &element); + if (eleStatus != napi_ok) { + return nullptr; + } + parseNapiValue(env, info, element, newString); + } + } + HiLogPrint(DEFAULT_LOG_TYPE, static_cast(level), domain, tag.get(), + newString.c_str(), ""); + return nullptr; +} +} // namespace HiviewDFX +} // namespace OHOS + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/interfaces/native/innerkits/include/hilog/log_cpp.h b/interfaces/native/innerkits/include/hilog/log_cpp.h index df8fea1..3bc9f9b 100644 --- a/interfaces/native/innerkits/include/hilog/log_cpp.h +++ b/interfaces/native/innerkits/include/hilog/log_cpp.h @@ -1,17 +1,17 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef HIVIEWDFX_HILOG_CPP_H #define HIVIEWDFX_HILOG_CPP_H @@ -43,4 +43,4 @@ public: #endif // __cplusplus -#endif // HIVIEWDFX_HILOG_CPP_H +#endif // HIVIEWDFX_HILOG_CPP_H \ No newline at end of file diff --git a/ohos.build b/ohos.build index 7457fa9..55fe0d2 100644 --- a/ohos.build +++ b/ohos.build @@ -14,7 +14,8 @@ "//base/hiviewdfx/hilog/frameworks/native/hilog_ndk:hilog_ndk", "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", - "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_base" + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_base", + "//base/hiviewdfx/hilog/interfaces/js/kits/napi:libhilognapi" ], "inner_kits": [ { -- Gitee From 175491c82974dd748e60a2617ad5e6536e220e27 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Thu, 13 Jan 2022 03:58:29 +0000 Subject: [PATCH 2/3] code check Signed-off-by: aajwy <13051180828@163.com> --- interfaces/js/kits/napi/BUILD.gn | 110 +++++++++-------- .../js/kits/napi/src/common/napi/n_class.cpp | 3 - .../kits/napi/src/common/napi/n_func_arg.cpp | 2 - .../js/kits/napi/src/common/napi/n_func_arg.h | 2 - .../js/kits/napi/src/common/napi/n_val.h | 1 - .../js/kits/napi/src/common/napi/uni_header.h | 6 +- .../js/kits/napi/src/common/napi/util.h | 41 +++++-- .../src/hilog/include/context/hilog_napi.h | 2 +- .../hilog/include/context/hilog_napi_base.h | 3 +- interfaces/js/kits/napi/src/hilog/module.cpp | 1 - .../js/kits/napi/src/hilog/src/hilog_napi.cpp | 3 +- .../napi/src/hilog/src/hilog_napi_base.cpp | 113 +++++++++++++----- 12 files changed, 176 insertions(+), 111 deletions(-) diff --git a/interfaces/js/kits/napi/BUILD.gn b/interfaces/js/kits/napi/BUILD.gn index bf73d0c..9579f64 100644 --- a/interfaces/js/kits/napi/BUILD.gn +++ b/interfaces/js/kits/napi/BUILD.gn @@ -1,51 +1,59 @@ -# Copyright (c) 2020 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/ohos.gni") - -config("libhilog_pub_config") { - visibility = [ ":*" ] - - include_dirs = [ "include" ] -} - -ohos_source_set("libhilognapi_src") { - include_dirs = [ - "//third_party/node/src", - "//utils/native/base/include", - ] - sources = [ - "src/common/napi/n_class.cpp", - "src/common/napi/n_func_arg.cpp", - "src/common/napi/n_val.cpp", - "src/hilog/module.cpp", - "src/hilog/src/hilog_napi.cpp", - "src/hilog/src/hilog_napi_base.cpp", - #"src/hilog/src/js_hilog.cpp", - ] - - deps = [ "//utils/native/base:utilsecurec" ] - - external_deps = [ - "hilog_native:libhilog", - "napi:ace_napi", - ] -} - -ohos_shared_library("libhilognapi") { - deps = [ ":libhilognapi_src" ] - output_name = "libhilog" - relative_install_dir = "module" - subsystem_name = "hiviewdfx" - part_name = "hilog_native" -} \ No newline at end of file +# Copyright (c) 2020 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("libhilog_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ "include" ] +} + +ohos_source_set("libhilognapi_src") { + include_dirs = [ + "//third_party/node/src", + "//utils/native/base/include", + "//base/hiviewdfx/hilog/adapter", + ] + sources = [ + "src/common/napi/n_class.cpp", + "src/common/napi/n_func_arg.cpp", + "src/common/napi/n_val.cpp", + "src/hilog/module.cpp", + "src/hilog/src/hilog_napi.cpp", + "src/hilog/src/hilog_napi_base.cpp", + ] + deps = [ + "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", + "//utils/native/base:utilsecurec", + ] + external_deps = [ + "hilog_native:libhilog", + "napi:ace_napi", + ] +} + +config("libhilog_js_cfg") { + visibility = [ "*:*" ] + include_dirs = [ "include" ] +} + +ohos_shared_library("libhilognapi") { + deps = [ ":libhilognapi_src" ] + output_name = "libhilog" + relative_install_dir = "module" + subsystem_name = "hiviewdfx" + part_name = "hilog_native" + public_configs = [ ":libhilog_js_cfg" ] + configs = [ ":libhilog_js_cfg" ] +} diff --git a/interfaces/js/kits/napi/src/common/napi/n_class.cpp b/interfaces/js/kits/napi/src/common/napi/n_class.cpp index c69f08d..45b22b2 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_class.cpp +++ b/interfaces/js/kits/napi/src/common/napi/n_class.cpp @@ -15,9 +15,6 @@ #include "n_class.h" -#include -#include - namespace OHOS { namespace HiviewDFX { using namespace std; diff --git a/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp b/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp index 45f773b..3c40bc9 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp +++ b/interfaces/js/kits/napi/src/common/napi/n_func_arg.cpp @@ -14,8 +14,6 @@ */ #include "n_func_arg.h" -#include -#include namespace OHOS { namespace HiviewDFX { diff --git a/interfaces/js/kits/napi/src/common/napi/n_func_arg.h b/interfaces/js/kits/napi/src/common/napi/n_func_arg.h index 0785903..91b6649 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_func_arg.h +++ b/interfaces/js/kits/napi/src/common/napi/n_func_arg.h @@ -20,8 +20,6 @@ #include #include -#include -#include #include "n_val.h" #include "uni_header.h" diff --git a/interfaces/js/kits/napi/src/common/napi/n_val.h b/interfaces/js/kits/napi/src/common/napi/n_val.h index 571f012..e2ed44d 100644 --- a/interfaces/js/kits/napi/src/common/napi/n_val.h +++ b/interfaces/js/kits/napi/src/common/napi/n_val.h @@ -18,7 +18,6 @@ #pragma once #include -#include #include #include "uni_header.h" diff --git a/interfaces/js/kits/napi/src/common/napi/uni_header.h b/interfaces/js/kits/napi/src/common/napi/uni_header.h index 23d5fac..d000d78 100644 --- a/interfaces/js/kits/napi/src/common/napi/uni_header.h +++ b/interfaces/js/kits/napi/src/common/napi/uni_header.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef HIVIEW_NAPI_HEADER -#define HIVIEW_NAPI_HEADER +#ifndef INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H +#define INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H #pragma once @@ -22,4 +22,4 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" -#endif // HIVIEW_NAPI_HEADER \ No newline at end of file +#endif // INTERFACES_JS_KITS_NAPI_SRC_COMMON_NAPI_UNI_HEADER_H \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/common/napi/util.h b/interfaces/js/kits/napi/src/common/napi/util.h index 90132af..b78a313 100644 --- a/interfaces/js/kits/napi/src/common/napi/util.h +++ b/interfaces/js/kits/napi/src/common/napi/util.h @@ -1,18 +1,32 @@ -#include +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HIVIEW_NAPI_UTIL +#define HIVIEW_NAPI_UTIL #include -#include namespace util { template struct function_traits_defs { - static constexpr size_t arity = sizeof...(Args); + static constexpr size_t arity = sizeof...(Args); - using result_type = ReturnType; + using result_type = ReturnType; - template - struct arg { - using type = typename std::tuple_element>::type; - }; + template + struct arg { + using type = typename std::tuple_element>::type; + }; }; template @@ -76,15 +90,15 @@ struct function_traits template struct indices { - using next = indices; + using next = indices; }; template struct build_indices { - using type = typename build_indices::type::next; + using type = typename build_indices::type::next; }; template <> struct build_indices<0> { - using type = indices<>; + using type = indices<>; }; template using BuildIndices = typename build_indices::type; @@ -96,7 +110,7 @@ template , typename ReturnT = typename Traits::result_type> ReturnT do_call(FuncType& func, VecType& args, indices) { - return func(args[I]...); + return func(args[I]...); } } // namespace details @@ -109,4 +123,5 @@ ReturnT unpack_caller(FuncType& func, VecType& args) { return details::do_call(func, args, BuildIndices()); } -} // namespace util \ No newline at end of file +} // namespace util +#endif // HIVIEW_NAPI_UTIL \ No newline at end of file diff --git a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h index 3949cd9..12a876b 100644 --- a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h +++ b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi.h @@ -28,7 +28,7 @@ public: std::string GetClassName() override; HilogNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; explicit HilogNapi() {}; - ~HilogNapi() {}; + ~HilogNapi() override {}; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h index f17681f..5faccaf 100644 --- a/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h +++ b/interfaces/js/kits/napi/src/hilog/include/context/hilog_napi_base.h @@ -32,8 +32,9 @@ public: static napi_value warn(napi_env env, napi_callback_info info); static napi_value fatal(napi_env env, napi_callback_info info); static napi_value isLoggable(napi_env env, napi_callback_info info); +private: static napi_value parseNapiValue(napi_env env, napi_callback_info info, - napi_value element, std::string& newString); + napi_value element, std::vector& params); }; } // namespace HiviewDFX } // namespace OHOS diff --git a/interfaces/js/kits/napi/src/hilog/module.cpp b/interfaces/js/kits/napi/src/hilog/module.cpp index f29453b..b9074bc 100644 --- a/interfaces/js/kits/napi/src/hilog/module.cpp +++ b/interfaces/js/kits/napi/src/hilog/module.cpp @@ -22,7 +22,6 @@ using namespace std; namespace OHOS { namespace HiviewDFX { - static napi_value Export(napi_env env, napi_value exports) { std::vector> products; diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp index 87a979e..3a0a77e 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi.cpp @@ -38,8 +38,7 @@ bool HilogNapi::Export(napi_env env, napi_value exports) napi_create_int32(env, LogLevel::LOG_WARN, &WARN); napi_create_int32(env, LogLevel::LOG_ERROR, &ERROR); napi_create_int32(env, LogLevel::LOG_FATAL, &FATAL); - return exports_.AddProp( - { + return exports_.AddProp({ NVal::DeclareNapiFunction("debug", HilogNapiBase::debug), NVal::DeclareNapiFunction("info", HilogNapiBase::info), NVal::DeclareNapiFunction("error", HilogNapiBase::error), diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp index 4bfff08..6d6e20a 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -13,8 +13,8 @@ * limitations under the License. */ +#include "properties.h" -#include "../include/context/hilog_napi_base.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "../../common/napi/n_func_arg.h" @@ -22,6 +22,7 @@ #include "hilog/log.h" #include "hilog/log_c.h" #include "securec.h" +#include "../include/context/hilog_napi_base.h" #ifdef __cplusplus extern "C" { @@ -30,21 +31,74 @@ namespace OHOS { namespace HiviewDFX { using namespace std; #define DEFAULT_LOG_TYPE LOG_CORE -#define MIN_NUMBER 3 -#define MAX_NUMBER 100 +const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "Hilog_JS" }; +static constexpr int MIN_NUMBER = 3; +static constexpr int MAX_NUMBER = 100; +static constexpr int PUBLIC_LEN = 6; +static constexpr int PRIVATE_LEN = 7; +static constexpr int PROPERTY_POS = 2; -void ReplaceOnce(string& base, string src, string dst, bool& flag) +void ParseLogContent(string& formatStr, vector& params, string& logContent) { - if (flag == true) { + string& ret = logContent; + if (params.empty()) { return; } - size_t pos = 0; - size_t srclen = src.size(); - size_t dstlen = dst.size(); - if ((pos = base.find(src, pos)) != string::npos) { - base.replace(pos, srclen, dst); - pos += dstlen; - flag = true; + int32_t size = params.size(); + int32_t len = formatStr.size(); + int32_t pos = 0; + int32_t count = 0; + bool debug = IsDebugOn(); + bool priv = (!debug) && IsPrivateSwitchOn(); + + for (; pos < len; ++pos) { + bool showPriv = false; + if (count > size) { + break; + } + if (formatStr[pos] == '%') { + if (formatStr[pos + 1] == '{') { + if (formatStr.substr(pos + PROPERTY_POS, PUBLIC_LEN) == "public") { + pos += (PUBLIC_LEN + PROPERTY_POS); + } + if (formatStr.substr(pos + PROPERTY_POS, PRIVATE_LEN) == "private") { + pos += (PRIVATE_LEN + PROPERTY_POS); + if (priv) { + showPriv = true ; + } + } + } + if (pos + 1 >= len) { + break; + } + string privStr = ""; + switch (formatStr[pos + 1]) { + case 's': + case 'j': + case 'd': + case 'O': + case 'o': + case 'i': + case 'f': + case 'c': + ret += showPriv ? privStr : params[count]; + count++; + ++pos; + break; + case '%': + ret += showPriv ? *privStr.data() : formatStr[pos]; + ++pos; + break; + default: + ret += showPriv ? *privStr.data() : formatStr[pos]; + break; + } + } else { + ret += formatStr[pos]; + } + } + if (pos < len) { + ret += formatStr.substr(pos, len - pos); } return; } @@ -102,12 +156,11 @@ napi_value HilogNapiBase::fatal(napi_env env, napi_callback_info info) } napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, - napi_value element, string& newString) + napi_value element, vector& params) { bool succ = false; napi_valuetype type; napi_status typeStatus = napi_typeof(env, element, &type); - bool flag = false; if (typeStatus != napi_ok) { return nullptr; } @@ -122,21 +175,14 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (!succ) { return nullptr; } - ReplaceOnce(newString, "%{public}d", name.get(), flag); - ReplaceOnce(newString, "%{private}d", name.get(), flag); - ReplaceOnce(newString, "%d", name.get(), flag); - ReplaceOnce(newString, "%{public}f", name.get(), flag); - ReplaceOnce(newString, "%{private}f", name.get(), flag); - ReplaceOnce(newString, "%f", name.get(), flag); + params.emplace_back(name.get()); } else if (type == napi_string) { unique_ptr name; tie(succ, name, ignore) = NVal(env, element).ToUTF8String(); if (!succ) { return nullptr; } - ReplaceOnce(newString, "%{public}s", name.get(), flag); - ReplaceOnce(newString, "%{private}s", name.get(), flag); - ReplaceOnce(newString, "%s", name.get(), flag); + params.emplace_back(name.get()); } else if (type == napi_object) { napi_value elmString; napi_status objectStatus = napi_coerce_to_string(env, element, &elmString); @@ -148,14 +194,13 @@ napi_value HilogNapiBase::parseNapiValue(napi_env env, napi_callback_info info, if (!succ) { return nullptr; } - ReplaceOnce(newString, "%{public}o", name.get(), flag); - ReplaceOnce(newString, "%{private}o", name.get(), flag); - ReplaceOnce(newString, "%o", name.get(), flag); + params.emplace_back(name.get()); } else { NAPI_ASSERT(env, false, "type mismatch"); } return nullptr; } + napi_value HilogNapiBase::HilogImpl(napi_env env, napi_callback_info info, int level) { NFuncArg funcArg(env, info); @@ -164,31 +209,36 @@ napi_value HilogNapiBase::HilogImpl(napi_env env, napi_callback_info info, int l int32_t domain; tie(succ, domain) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { + HiLog::Info(LABEL, "%{public}s", "domain mismatch"); return nullptr; } unique_ptr tag; tie(succ, tag, ignore) = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); if (!succ) { + HiLog::Info(LABEL, "%{public}s", "tag mismatch"); return nullptr; } unique_ptr fmt; tie(succ, fmt, ignore) = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String(); if (!succ) { + HiLog::Info(LABEL, "%{public}s", "Format mismatch"); return nullptr; } string fmtString = fmt.get(); bool res = false; napi_value array = funcArg[NARG_POS::FOURTH]; napi_is_array(env, array, &res); - string newString = fmtString; - if (res == false) { + string logContent; + vector params; + if (!res) { for (size_t i = MIN_NUMBER; i < funcArg.GetArgc(); i++) { napi_value argsVal = funcArg[i]; - parseNapiValue(env, info, argsVal, newString); + parseNapiValue(env, info, argsVal, params); } } else { if (funcArg.GetArgc() != MIN_NUMBER + 1) { NAPI_ASSERT(env, false, "Argc mismatch"); + HiLog::Info(LABEL, "%{public}s", "Argc mismatch"); return nullptr; } uint32_t length; @@ -203,11 +253,12 @@ napi_value HilogNapiBase::HilogImpl(napi_env env, napi_callback_info info, int l if (eleStatus != napi_ok) { return nullptr; } - parseNapiValue(env, info, element, newString); + parseNapiValue(env, info, element, params); } } + ParseLogContent(fmtString, params, logContent); HiLogPrint(DEFAULT_LOG_TYPE, static_cast(level), domain, tag.get(), - newString.c_str(), ""); + logContent.c_str(), ""); return nullptr; } } // namespace HiviewDFX -- Gitee From 6d6bdbfc8db73b75e02c2035be0bab5b45c8f4ec Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Tue, 18 Jan 2022 03:07:47 +0000 Subject: [PATCH 3/3] code check Signed-off-by: aajwy <13051180828@163.com> --- interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp index 6d6e20a..e686ac3 100644 --- a/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp +++ b/interfaces/js/kits/napi/src/hilog/src/hilog_napi_base.cpp @@ -42,6 +42,7 @@ void ParseLogContent(string& formatStr, vector& params, string& logConte { string& ret = logContent; if (params.empty()) { + ret += formatStr; return; } int32_t size = params.size(); @@ -86,11 +87,11 @@ void ParseLogContent(string& formatStr, vector& params, string& logConte ++pos; break; case '%': - ret += showPriv ? *privStr.data() : formatStr[pos]; + ret += formatStr[pos]; ++pos; break; default: - ret += showPriv ? *privStr.data() : formatStr[pos]; + ret += formatStr[pos]; break; } } else { -- Gitee