From 6bfaef5c4d48f8be8a6f917fea4384b15ad96c5e Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 1 Nov 2021 11:38:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=B9=E5=8F=98=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=9A=84=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= =?UTF-8?q?=EF=BC=8C=E4=BC=A0=E9=80=92=E7=BB=99=E6=B6=88=E6=81=AF=E9=98=9F?= =?UTF-8?q?=E5=88=97=E7=9A=84=E5=9B=9E=E8=B0=83=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E7=B1=BB=E9=87=8D=E6=96=B0=E8=AE=BE=E8=AE=A1=EF=BC=8C=E6=8A=8A?= =?UTF-8?q?requestData=E6=88=90=E5=91=98=E6=8C=87=E9=92=88=E6=94=B9?= =?UTF-8?q?=E6=88=90=E6=88=90=E5=91=98=E5=AF=B9=E8=B1=A1=EF=BC=8C=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=80=E6=AC=A1=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D?= =?UTF-8?q?=EF=BC=8C=E6=8A=8Ajs=E5=9B=9E=E8=B0=83=E6=94=BE=E8=BF=9Bstd:map?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E9=81=BF=E5=85=8D=E5=AF=B9js=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=9A=84=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=BA=86js=E5=AF=B9=E8=B1=A1=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- .../frameworks/js/builtin/CMakeLists.txt | 6 +- .../frameworks/js/builtin/fetch_module.cpp | 39 +--- .../frameworks/js/builtin/fetch_module.h | 1 - .../http_request/http_async_callback.cpp | 69 +++---- .../http_request/http_async_callback.h | 10 +- .../frameworks/js/builtin/test/CMakeLists.txt | 3 + .../{test_headers => test/include}/ace_log.h | 0 .../include}/ace_mem_base.h | 0 .../include}/graphic_config.h | 0 .../include}/memory_heap.h | 2 +- .../js/builtin/test/test_fetch_module.cpp | 170 ++++++++++++++++++ 11 files changed, 218 insertions(+), 82 deletions(-) create mode 100644 http_lite/frameworks/js/builtin/test/CMakeLists.txt rename http_lite/frameworks/js/builtin/{test_headers => test/include}/ace_log.h (100%) rename http_lite/frameworks/js/builtin/{test_headers => test/include}/ace_mem_base.h (100%) rename http_lite/frameworks/js/builtin/{test_headers => test/include}/graphic_config.h (100%) rename http_lite/frameworks/js/builtin/{test_headers => test/include}/memory_heap.h (85%) create mode 100644 http_lite/frameworks/js/builtin/test/test_fetch_module.cpp diff --git a/http_lite/frameworks/js/builtin/CMakeLists.txt b/http_lite/frameworks/js/builtin/CMakeLists.txt index 1214891c3..69f37f9cc 100644 --- a/http_lite/frameworks/js/builtin/CMakeLists.txt +++ b/http_lite/frameworks/js/builtin/CMakeLists.txt @@ -16,7 +16,7 @@ project(ohos_acelite) set(CMAKE_CXX_STANDARD 14) -include_directories(test_headers) +include_directories(test/include) include_directories(../../../../../../ace/ace_engine_lite/interfaces/innerkits/builtin/jsi) include_directories(../../../../../../ace/ace_engine_lite/interfaces/innerkits/builtin/async) @@ -59,6 +59,4 @@ target_link_libraries(test_fetch_module securec) add_compile_definitions(NO_SSL_CERTIFICATION=1) -add_executable(test_fetch_module_exec test_fetch_module.cpp) - -target_link_libraries(test_fetch_module_exec test_fetch_module) +add_subdirectory(test) \ No newline at end of file diff --git a/http_lite/frameworks/js/builtin/fetch_module.cpp b/http_lite/frameworks/js/builtin/fetch_module.cpp index daa34a2a2..e3d18da6d 100644 --- a/http_lite/frameworks/js/builtin/fetch_module.cpp +++ b/http_lite/frameworks/js/builtin/fetch_module.cpp @@ -38,46 +38,19 @@ JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_ return JSI::CreateUndefined(); } - auto requestData = new RequestData(); - if (JsObjectToRequestData(args[0], requestData)) { - auto asyncCallback = - new HttpAsyncCallback(const_cast(requestData), MakeCallback(args[0]), thisVal); + auto asyncCallback = new HttpAsyncCallback(thisVal); + if (JsObjectToRequestData(args[0], &asyncCallback->requestData)) { + asyncCallback->responseCallback[CB_SUCCESS] = JSI::GetNamedProperty(args[0], CB_SUCCESS); + asyncCallback->responseCallback[CB_FAIL] = JSI::GetNamedProperty(args[0], CB_FAIL); + asyncCallback->responseCallback[CB_COMPLETE] = JSI::GetNamedProperty(args[0], CB_COMPLETE); JsAsyncWork::DispatchAsyncWork(HttpAsyncCallback::AsyncExecHttpRequest, static_cast(asyncCallback)); } else { - delete requestData; + delete asyncCallback; } return JSI::CreateUndefined(); } -JSIValue FetchModule::MakeCallback(JSIValue options) -{ - JSIValue obj = JSI::CreateObject(); - if (obj == nullptr) { - return nullptr; - } - - std::unique_ptr successCallback(JSI::GetNamedProperty(options, CB_SUCCESS), - JSI::ReleaseValue); - std::unique_ptr failCallback(JSI::GetNamedProperty(options, CB_FAIL), - JSI::ReleaseValue); - std::unique_ptr completeCallback(JSI::GetNamedProperty(options, CB_COMPLETE), - JSI::ReleaseValue); - if ((successCallback == nullptr || JSI::ValueIsUndefined(successCallback.get()) || - !JSI::ValueIsFunction(successCallback.get())) && - (failCallback == nullptr || JSI::ValueIsUndefined(failCallback.get()) || - !JSI::ValueIsFunction(failCallback.get())) && - (completeCallback == nullptr || JSI::ValueIsUndefined(completeCallback.get()) || - !JSI::ValueIsFunction(completeCallback.get()))) { - return nullptr; - } - - JSI::SetNamedProperty(obj, CB_SUCCESS, successCallback.get()); - JSI::SetNamedProperty(obj, CB_FAIL, failCallback.get()); - JSI::SetNamedProperty(obj, CB_COMPLETE, completeCallback.get()); - return obj; -} - bool FetchModule::JsObjectToRequestData(JSIValue options, RequestData *req) { if (options == nullptr || JSI::ValueIsUndefined(options) || !JSI::ValueIsObject(options)) { diff --git a/http_lite/frameworks/js/builtin/fetch_module.h b/http_lite/frameworks/js/builtin/fetch_module.h index 53bc0690c..fa239919f 100644 --- a/http_lite/frameworks/js/builtin/fetch_module.h +++ b/http_lite/frameworks/js/builtin/fetch_module.h @@ -41,7 +41,6 @@ public: static const char *const HTTP_API_KEY_STRING_TO_ARRAY_BUFFER; private: - static JSIValue MakeCallback(JSIValue options); static bool JsObjectToRequestData(JSIValue options, RequestData *req); static void GetNameValue(JSIValue nv, std::map &map); static void GetRequestBody(JSIValue options, RequestData *requestData); diff --git a/http_lite/frameworks/js/builtin/http_request/http_async_callback.cpp b/http_lite/frameworks/js/builtin/http_request/http_async_callback.cpp index d41b5d878..bde304eca 100644 --- a/http_lite/frameworks/js/builtin/http_request/http_async_callback.cpp +++ b/http_lite/frameworks/js/builtin/http_request/http_async_callback.cpp @@ -20,48 +20,26 @@ #include #include -template void DefaultDelete(T *ptr) -{ - HTTP_REQUEST_INFO("delete %s", typeid(ptr).name()); - delete ptr; -} - namespace OHOS { namespace ACELite { -HttpAsyncCallback::HttpAsyncCallback(const RequestData *&requestData, JSIValue responseCallback, JSIValue thisVal) +HttpAsyncCallback::HttpAsyncCallback(JSIValue thisVal) { - this->requestData = const_cast(requestData); - this->responseCallback = responseCallback; this->thisVal = thisVal; } void HttpAsyncCallback::AsyncExecHttpRequest(void *data) { - std::unique_ptr)> asyncCallback( - static_cast(data), DefaultDelete); + std::unique_ptr asyncCallback( + static_cast(data), HttpAsyncCallback::AsyncCallbackDeleter); if (asyncCallback == nullptr) { return; } - std::unique_ptr)> requestData(asyncCallback->requestData, - DefaultDelete); - if (requestData == nullptr) { - return; - } ResponseData responseData; - bool success = HttpRequest::Request(requestData.get(), &responseData); - std::unique_ptr responseCallback(asyncCallback->responseCallback, - JSI::ReleaseValue); - if (responseCallback == nullptr || JSI::ValueIsUndefined(responseCallback.get()) || - !JSI::ValueIsObject(responseCallback.get())) { - if (success) { - HTTP_REQUEST_INFO("http status line: %s", responseData.GetStatusLine().c_str()); - } - return; - } - + bool success = HttpRequest::Request(&asyncCallback->requestData, &responseData); if (success) { + HTTP_REQUEST_INFO("http status line: %s", responseData.GetStatusLine().c_str()); asyncCallback->OnSuccess(responseData); } else { asyncCallback->OnFail(responseData.GetErrString().c_str(), responseData.GetCode()); @@ -69,6 +47,20 @@ void HttpAsyncCallback::AsyncExecHttpRequest(void *data) asyncCallback->OnComplete(); } +void HttpAsyncCallback::AsyncCallbackDeleter(HttpAsyncCallback *asyncCallback) +{ + if (asyncCallback->responseCallback[CB_SUCCESS]) { + JSI::ReleaseValue(asyncCallback->responseCallback[CB_SUCCESS]); + } + if (asyncCallback->responseCallback[CB_FAIL]) { + JSI::ReleaseValue(asyncCallback->responseCallback[CB_FAIL]); + } + if (asyncCallback->responseCallback[CB_COMPLETE]) { + JSI::ReleaseValue(asyncCallback->responseCallback[CB_COMPLETE]); + } + delete asyncCallback; +} + JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseData) { JSIValue object = JSI::CreateObject(); @@ -79,7 +71,7 @@ JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseDa JSI::SetNumberProperty(object, HttpConstant::KEY_HTTP_RESPONSE_CODE, responseData.GetCode()); HTTP_REQUEST_INFO("response body size = %zu", responseData.GetData().size()); - std::string responseType = requestData->GetResponseType(); + std::string responseType = requestData.GetResponseType(); std::transform(responseType.begin(), responseType.end(), responseType.begin(), tolower); if (responseType == HttpConstant::HTTP_RESPONSE_TYPE_JSON) { @@ -107,9 +99,8 @@ JSIValue HttpAsyncCallback::ResponseDataToJsValue(const ResponseData &responseDa void HttpAsyncCallback::OnSuccess(const ResponseData &responseData) { - std::unique_ptr success(JSI::GetNamedProperty(responseCallback, CB_SUCCESS), - JSI::ReleaseValue); - if (success == nullptr || JSI::ValueIsUndefined(success.get()) || !JSI::ValueIsFunction(success.get())) { + JSIValue success = responseCallback[CB_SUCCESS]; + if (success == nullptr || JSI::ValueIsUndefined(success) || !JSI::ValueIsFunction(success)) { return; } @@ -119,31 +110,29 @@ void HttpAsyncCallback::OnSuccess(const ResponseData &responseData) } JSIValue arg[ARGC_ONE] = {obj.get()}; - JSI::CallFunction(success.get(), thisVal, arg, ARGC_ONE); + JSI::CallFunction(success, thisVal, arg, ARGC_ONE); } void HttpAsyncCallback::OnFail(const char *errData, int32_t errCode) { - std::unique_ptr fail(JSI::GetNamedProperty(responseCallback, CB_FAIL), - JSI::ReleaseValue); - if (fail == nullptr || JSI::ValueIsUndefined(fail.get()) || !JSI::ValueIsFunction(fail.get())) { + JSIValue fail = responseCallback[CB_FAIL]; + if (fail == nullptr || JSI::ValueIsUndefined(fail) || !JSI::ValueIsFunction(fail)) { return; } std::unique_ptr errInfo(JSI::CreateString(errData), JSI::ReleaseValue); std::unique_ptr retCode(JSI::CreateNumber(errCode), JSI::ReleaseValue); JSIValue argv[ARGC_TWO] = {errInfo.get(), retCode.get()}; - JSI::CallFunction(fail.get(), thisVal, argv, ARGC_TWO); + JSI::CallFunction(fail, thisVal, argv, ARGC_TWO); } void HttpAsyncCallback::OnComplete() { - std::unique_ptr complete(JSI::GetNamedProperty(responseCallback, CB_COMPLETE), - JSI::ReleaseValue); - if (complete == nullptr || JSI::ValueIsUndefined(complete.get()) || !JSI::ValueIsFunction(complete.get())) { + JSIValue complete = responseCallback[CB_COMPLETE]; + if (complete == nullptr || JSI::ValueIsUndefined(complete) || !JSI::ValueIsFunction(complete)) { return; } - JSI::CallFunction(complete.get(), thisVal, nullptr, 0); + JSI::CallFunction(complete, thisVal, nullptr, 0); } } // namespace ACELite diff --git a/http_lite/frameworks/js/builtin/http_request/http_async_callback.h b/http_lite/frameworks/js/builtin/http_request/http_async_callback.h index 2f3c314af..5dc7875de 100644 --- a/http_lite/frameworks/js/builtin/http_request/http_async_callback.h +++ b/http_lite/frameworks/js/builtin/http_request/http_async_callback.h @@ -16,6 +16,7 @@ #ifndef OHOS_ACELITE_HTTP_ASYNC_CALLBACK_H #define OHOS_ACELITE_HTTP_ASYNC_CALLBACK_H +#include "../fetch_module.h" #include "jsi.h" #include "non_copyable.h" #include "request_data.h" @@ -28,17 +29,20 @@ class HttpAsyncCallback : public MemoryHeap { public: ACE_DISALLOW_COPY_AND_MOVE(HttpAsyncCallback); - HttpAsyncCallback(const RequestData *&requestData, JSIValue responseCallback, JSIValue thisVal); + explicit HttpAsyncCallback(JSIValue thisVal); public: static void AsyncExecHttpRequest(void *data); + friend JSIValue FetchModule::Fetch(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); private: - RequestData *requestData; - JSIValue responseCallback; + RequestData requestData; + std::map responseCallback; JSIValue thisVal; private: + static void AsyncCallbackDeleter(HttpAsyncCallback *asyncCallback); + JSIValue ResponseDataToJsValue(const ResponseData &responseData); void OnSuccess(const ResponseData &responseData); void OnFail(const char *errData, int32_t errCode); diff --git a/http_lite/frameworks/js/builtin/test/CMakeLists.txt b/http_lite/frameworks/js/builtin/test/CMakeLists.txt new file mode 100644 index 000000000..dacebb3a3 --- /dev/null +++ b/http_lite/frameworks/js/builtin/test/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(test_fetch_module_exec test_fetch_module.cpp) + +target_link_libraries(test_fetch_module_exec test_fetch_module) \ No newline at end of file diff --git a/http_lite/frameworks/js/builtin/test_headers/ace_log.h b/http_lite/frameworks/js/builtin/test/include/ace_log.h similarity index 100% rename from http_lite/frameworks/js/builtin/test_headers/ace_log.h rename to http_lite/frameworks/js/builtin/test/include/ace_log.h diff --git a/http_lite/frameworks/js/builtin/test_headers/ace_mem_base.h b/http_lite/frameworks/js/builtin/test/include/ace_mem_base.h similarity index 100% rename from http_lite/frameworks/js/builtin/test_headers/ace_mem_base.h rename to http_lite/frameworks/js/builtin/test/include/ace_mem_base.h diff --git a/http_lite/frameworks/js/builtin/test_headers/graphic_config.h b/http_lite/frameworks/js/builtin/test/include/graphic_config.h similarity index 100% rename from http_lite/frameworks/js/builtin/test_headers/graphic_config.h rename to http_lite/frameworks/js/builtin/test/include/graphic_config.h diff --git a/http_lite/frameworks/js/builtin/test_headers/memory_heap.h b/http_lite/frameworks/js/builtin/test/include/memory_heap.h similarity index 85% rename from http_lite/frameworks/js/builtin/test_headers/memory_heap.h rename to http_lite/frameworks/js/builtin/test/include/memory_heap.h index b7d7fdb18..276a38fc0 100644 --- a/http_lite/frameworks/js/builtin/test_headers/memory_heap.h +++ b/http_lite/frameworks/js/builtin/test/include/memory_heap.h @@ -13,4 +13,4 @@ * limitations under the License. */ -#include "../../../../../../../ace/ace_engine_lite/interfaces/innerkits/builtin/base/memory_heap.h" \ No newline at end of file +#include "../../../../../../../../ace/ace_engine_lite/interfaces/innerkits/builtin/base/memory_heap.h" \ No newline at end of file diff --git a/http_lite/frameworks/js/builtin/test/test_fetch_module.cpp b/http_lite/frameworks/js/builtin/test/test_fetch_module.cpp new file mode 100644 index 000000000..ce62ac0e5 --- /dev/null +++ b/http_lite/frameworks/js/builtin/test/test_fetch_module.cpp @@ -0,0 +1,170 @@ +/* + * 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 "../fetch_module.h" +#include "../http_request/http_async_callback.h" +#include "../http_request/http_request_utils.h" +#include "jerryscript-core.h" +#include "js_async_work.h" +#include "message_queue_utils.h" + +#define FUNC_BEGIN() \ + do { \ + HTTP_REQUEST_INFO("%s BEGIN ##########", __FUNCTION__); \ + } while (0) + +#define FUNC_END_NO_NEW_LINE() \ + do { \ + HTTP_REQUEST_INFO("%s END ##########", __FUNCTION__); \ + } while (0) + +#define FUNC_END() \ + do { \ + HTTP_REQUEST_INFO("%s END ##########\n\n\n", __FUNCTION__); \ + } while (0) + +namespace OHOS { +namespace ACELite { +void InitFetchModule(JSIValue exports); +class JerryInitializer { +private: + int *temp; + JSIValue exports; + +public: + JerryInitializer() + { + temp = new int; + jerry_init(JERRY_INIT_EMPTY); + JsAsyncWork::SetAppQueueHandler(temp); + exports = JSI::CreateObject(); + InitFetchModule(exports); + } + + ~JerryInitializer() + { + jerry_cleanup(); + delete temp; + JSI::ReleaseValue(exports); + } +}; + +void TestPutMessage(void *data) +{ + auto msg = static_cast(data); + auto asyncWork = static_cast(msg->data); + asyncWork->workHandler(asyncWork->data); +} + +JSIValue TestCallbackOnSuccess(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + FUNC_BEGIN(); + (void)thisVal; + (void)argsNum; + + JSIValue para = args[0]; + HTTP_REQUEST_INFO("code = %d", + static_cast(JSI::GetNumberProperty(para, HttpConstant::KEY_HTTP_RESPONSE_CODE))); + + size_t size = 0; + char *data = JSI::GetStringProperty(para, HttpConstant::KEY_HTTP_RESPONSE_DATA, size); + std::string body; + for (uint32_t index = 0; index < size; ++index) { + if (data[index] != 0) { + body += data[index]; + } else { + body += "0"; + } + } + HTTP_REQUEST_INFO("%s", body.c_str()); + + JSIValue head = JSI::GetNamedProperty(para, HttpConstant::KEY_HTTP_RESPONSE_HEADERS); + + JSIValue keys = JSI::GetObjectKeys(head); + uint32_t length = JSI::GetArrayLength(keys); + for (uint32_t i = 0; i < length; ++i) { + JSIValue k = JSI::GetPropertyByIndex(keys, i); + char *s = JSI::ValueToString(k); + char *v = JSI::GetStringProperty(head, s); + HTTP_REQUEST_INFO("%s ---------------- %s", s, v); + } + + FUNC_END_NO_NEW_LINE(); + return JSI::CreateUndefined(); +} + +JSIValue TestCallbackOnFail(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + FUNC_BEGIN(); + (void)thisVal; + (void)argsNum; + + HTTP_REQUEST_INFO("err = %s", JSI::ValueToString(args[0])); + HTTP_REQUEST_INFO("code = %d", static_cast(JSI::ValueToNumber(args[1]))); + + FUNC_END_NO_NEW_LINE(); + return JSI::CreateUndefined(); +} + +JSIValue TestCallbackOnComplete(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + FUNC_BEGIN(); + (void)thisVal; + (void)args; + (void)argsNum; + + HTTP_REQUEST_INFO("request complete"); + + FUNC_END_NO_NEW_LINE(); + return JSI::CreateUndefined(); +} + +void TestHttpModuleMethodAndHeaderByDefault() +{ + FUNC_BEGIN(); + + JSIValue object = JSI::CreateObject(); + if (object == nullptr) { + return; + } + + JSIValue header = JSI::CreateObject(); + JSI::SetStringProperty(header, "no-use", "test value"); + JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_HEADER, header); + + JSIValue url = JSI::CreateString("https://www.zhihu.com"); + JSI::SetNamedProperty(object, HttpConstant::KEY_HTTP_REQUEST_URL, url); + + JSI::SetNamedProperty(object, CB_SUCCESS, JSI::CreateFunction(TestCallbackOnSuccess)); + JSI::SetNamedProperty(object, CB_FAIL, JSI::CreateFunction(TestCallbackOnFail)); + JSI::SetNamedProperty(object, CB_COMPLETE, JSI::CreateFunction(TestCallbackOnComplete)); + + JSIValue arg[1] = {object}; + FetchModule::Fetch(nullptr, arg, 1); + + FUNC_END(); +} + +} // namespace ACELite +} // namespace OHOS + +int main() +{ + OHOS::ACELite::JerryInitializer jerryInitializer; + + OHOS::ACELite::TestHttpModuleMethodAndHeaderByDefault(); + + return 0; +} \ No newline at end of file -- Gitee From ae4ba541b76e46e782ef90dc6e9686ed6fbeb8bc Mon Sep 17 00:00:00 2001 From: maosiping Date: Mon, 1 Nov 2021 16:46:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=94=B9=E6=88=90Hilog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maosiping --- http_lite/frameworks/js/builtin/BUILD.gn | 4 +++ .../builtin/http_request/http_request_utils.h | 33 ++++--------------- .../js/builtin/test/include/ace_log.h | 30 ++++++++++++----- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/http_lite/frameworks/js/builtin/BUILD.gn b/http_lite/frameworks/js/builtin/BUILD.gn index 8fde39101..e016eb2a0 100644 --- a/http_lite/frameworks/js/builtin/BUILD.gn +++ b/http_lite/frameworks/js/builtin/BUILD.gn @@ -36,6 +36,9 @@ http_lite_include_dirs = [ "//foundation/ace/ace_engine_lite/frameworks/common/utils", "//foundation/ace/ace_engine_lite/frameworks/common/log", + + "//base/hiviewdfx/hilog_lite/interfaces/native/kits", + "//foundation/ace/ace_engine_lite/frameworks/targets/liteos_a", ] config("http_lite_config") { @@ -51,6 +54,7 @@ lite_library("http_lite_shared") { deps = [ "//third_party/curl:libcurl_shared", "//third_party/bounds_checking_function:libsec_shared", + "//foundation/ace/ace_engine_lite/frameworks/common:ace_common_lite", ] } diff --git a/http_lite/frameworks/js/builtin/http_request/http_request_utils.h b/http_lite/frameworks/js/builtin/http_request/http_request_utils.h index 86ea4aef5..d3332e194 100644 --- a/http_lite/frameworks/js/builtin/http_request/http_request_utils.h +++ b/http_lite/frameworks/js/builtin/http_request/http_request_utils.h @@ -16,39 +16,20 @@ #ifndef OHOS_ACELITE_HTTP_REQUEST_UTILS_H #define OHOS_ACELITE_HTTP_REQUEST_UTILS_H +#include "ace_log.h" #include "curl/curl.h" #include "securec.h" #include #include -#define HTTP_PRINT_TIME() \ - do { \ - struct timeval now = {0}; \ - gettimeofday(&now, NULL); \ - struct tm *tmp_time = localtime(&now.tv_sec); \ - char timeString[100] = {0}; \ - strftime(timeString, sizeof(timeString), "%04Y/%02m/%02d %H:%M:%S", tmp_time); \ - long double uSec = now.tv_usec; \ - printf("[%s%.3Lf] ", timeString, uSec / 1000.0L / 1000.0L); \ - fflush(stdout); \ +#define HTTP_REQUEST_INFO(...) \ + do { \ + HILOG_INFO(HILOG_MODULE_ACE, "[HTTP][INFO] " __VA_ARGS__); \ } while (0) -#define HTTP_REQUEST_INFO(...) \ - do { \ - HTTP_PRINT_TIME(); \ - printf("[HTTP][INFO] " __VA_ARGS__); \ - fflush(stdout); \ - puts(""); \ - fflush(stdout); \ - } while (0) - -#define HTTP_REQUEST_ERROR(...) \ - do { \ - HTTP_PRINT_TIME(); \ - printf("[HTTP][ERROR] " __VA_ARGS__); \ - fflush(stdout); \ - puts(""); \ - fflush(stdout); \ +#define HTTP_REQUEST_ERROR(...) \ + do { \ + HILOG_ERROR(HILOG_MODULE_ACE, "[HTTP][ERROR] " __VA_ARGS__); \ } while (0) namespace OHOS { diff --git a/http_lite/frameworks/js/builtin/test/include/ace_log.h b/http_lite/frameworks/js/builtin/test/include/ace_log.h index 5594f8c34..9f298047a 100644 --- a/http_lite/frameworks/js/builtin/test/include/ace_log.h +++ b/http_lite/frameworks/js/builtin/test/include/ace_log.h @@ -20,16 +20,30 @@ #define __LITEOS__ -#define HILOG_ERROR(...) \ - do { \ +#define HILOG_ERROR(mod, ...) \ + do { \ + printf(__VA_ARGS__); \ + fflush(stdout); \ + puts(""); \ + fflush(stdout); \ } while (0) -#define HILOG_WARN(num, s, ...) \ - do { \ - if (strcmp(s, "todo call linux putmsg interface here!") == 0) { \ - OHOS::ACELite::TestPutMessage(const_cast(msgPtr)); \ - return MSGQ_OK; \ - } \ +#define HILOG_INFO(mod, ...) \ + do { \ + printf(__VA_ARGS__); \ + fflush(stdout); \ + puts(""); \ + fflush(stdout); \ + } while (0) + +typedef enum { HILOG_MODULE_ACE = 1 } HiLogModuleType; + +#define HILOG_WARN(mod, format, ...) \ + do { \ + if (strcmp(format, "todo call linux putmsg interface here!") == 0) { \ + OHOS::ACELite::TestPutMessage(const_cast(msgPtr)); \ + return MSGQ_OK; \ + } \ } while (0) static void *msgPtr; -- Gitee