From 8f17652cb538fd476eefe4e1b72e0869d2773730 Mon Sep 17 00:00:00 2001 From: karl1864 <1632083718@qq.com> Date: Wed, 16 Mar 2022 18:01:05 +0800 Subject: [PATCH 1/3] adapt mingw Change-Id: I191d9de1d6ff145bae55e3c2d0e6c8ad65af8504 Signed-off-by: luwh0708 <1632083718@qq.com> --- frameworks/libhilog/BUILD.gn | 32 +++++++++++-- frameworks/libhilog/mingw/hilog.cpp | 53 ++++++++++++++++++++++ frameworks/libhilog/mingw/hilog_base.cpp | 26 +++++++++++ frameworks/libhilog/mingw/hilog_printf.cpp | 23 ++++++++++ interfaces/native/innerkits/BUILD.gn | 4 +- 5 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 frameworks/libhilog/mingw/hilog.cpp create mode 100644 frameworks/libhilog/mingw/hilog_base.cpp create mode 100644 frameworks/libhilog/mingw/hilog_printf.cpp diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index c1407fa..1c6c6a5 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -76,10 +76,28 @@ ohos_source_set("libhilog_source") { deps = [ "//third_party/bounds_checking_function:libsec_shared" ] - external_deps = [ - "init:libbegetutil", - "startup_l2:syspara", - ] + if (current_os != "mingw") { + external_deps = [ + "init:libbegetutil", + "startup_l2:syspara", + ] + } else { + sources -= [ + "hilog.cpp", + "hilog_printf.cpp", + ] + sources -= param_sources + sources -= socket_sources + sources -= utils_sources + sources -= vsnprintf_sources + + sources += [ + "mingw/hilog.cpp", + "mingw/hilog_printf.cpp", + ] + + deps -= [ "//third_party/bounds_checking_function:libsec_shared" ] + } part_name = "hilog_native" subsystem_name = "hiviewdfx" @@ -108,6 +126,12 @@ ohos_source_set("libhilog_base_source") { public_configs = [ ":libhilog_base_config" ] configs = [ ":libhilog_base_config" ] + if (current_os == "mingw") { + sources -= [ "$libhilog_base_root/hilog_base.cpp" ] + sources -= vsnprintf_sources + sources += [ "mingw/hilog_base.cpp" ] + } + part_name = "hilog_native" subsystem_name = "hiviewdfx" } diff --git a/frameworks/libhilog/mingw/hilog.cpp b/frameworks/libhilog/mingw/hilog.cpp new file mode 100644 index 0000000..6bab636 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog.cpp @@ -0,0 +1,53 @@ +/* + * 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 "hilog/log.h" + +namespace OHOS::HiviewDFX { +int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...) +{ + (void)label; + (void)fmt; + return 0; +} + +int HiLog::Info(const HiLogLabel &label, const char *fmt, ...) +{ + (void)label; + (void)fmt; + return 0; +} + +int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) +{ + (void)label; + (void)fmt; + return 0; +} + +int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) +{ + (void)label; + (void)fmt; + return 0; +} + +int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) +{ + (void)label; + (void)fmt; + return 0; +} +} // namespace OHOS::HiviewDFX diff --git a/frameworks/libhilog/mingw/hilog_base.cpp b/frameworks/libhilog/mingw/hilog_base.cpp new file mode 100644 index 0000000..9f8c220 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_base.cpp @@ -0,0 +1,26 @@ +/* + * 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 "hilog_base/log_base.h" + +int HiLogBasePrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) +{ + (void)type; + (void)level; + (void)domain; + (void)tag; + (void)fmt; + return 0; +} diff --git a/frameworks/libhilog/mingw/hilog_printf.cpp b/frameworks/libhilog/mingw/hilog_printf.cpp new file mode 100644 index 0000000..941efe6 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_printf.cpp @@ -0,0 +1,23 @@ +#include "hilog/log.h" +#include "hilog_trace.h" + +int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) +{ + (void)type; + (void)level; + (void)domain; + (void)tag; + (void)fmt; + return 0; +} + +int HiLogRegisterGetIdFun(RegisterFunc func) +{ + (void)func; + return 0; +} + +void HiLogUnregisterGetIdFun(RegisterFunc func) +{ + (void)func; +} diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index 98b833f..e47515c 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -23,7 +23,9 @@ ohos_shared_library("libhilog") { deps = [ "//base/hiviewdfx/hilog/frameworks/libhilog:libhilog_source" ] - output_extension = "so" + if (current_os != "mingw") { + output_extension = "so" + } install_enable = true install_images = [ "system", -- Gitee From c5142fed15a2b2e6eab355dcff94394df2f87912 Mon Sep 17 00:00:00 2001 From: luwh0708 Date: Wed, 30 Mar 2022 09:54:26 +0800 Subject: [PATCH 2/3] impl hilog for mingw Change-Id: I11a52a216df574153f992e2be45c0f1de7519bd7 Signed-off-by: luwh0708 <1632083718@qq.com> --- frameworks/libhilog/mingw/hilog.cpp | 50 +++++++++++++++++----- frameworks/libhilog/mingw/hilog_base.cpp | 17 +++++--- frameworks/libhilog/mingw/hilog_printf.cpp | 30 ++++++++++--- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/frameworks/libhilog/mingw/hilog.cpp b/frameworks/libhilog/mingw/hilog.cpp index 6bab636..132c632 100644 --- a/frameworks/libhilog/mingw/hilog.cpp +++ b/frameworks/libhilog/mingw/hilog.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -15,39 +15,67 @@ #include "hilog/log.h" +#include +#include + namespace OHOS::HiviewDFX { int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...) { - (void)label; - (void)fmt; + printf("[HLG D] %s:", label.tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } int HiLog::Info(const HiLogLabel &label, const char *fmt, ...) { - (void)label; - (void)fmt; + printf("[HLG I] %s:", label.tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) { - (void)label; - (void)fmt; + printf("[HLG W] %s:", label.tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) { - (void)label; - (void)fmt; + printf("[HLG E] %s:", label.tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) { - (void)label; - (void)fmt; + printf("[HLG F] %s:", label.tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } } // namespace OHOS::HiviewDFX diff --git a/frameworks/libhilog/mingw/hilog_base.cpp b/frameworks/libhilog/mingw/hilog_base.cpp index 9f8c220..a7c50e8 100644 --- a/frameworks/libhilog/mingw/hilog_base.cpp +++ b/frameworks/libhilog/mingw/hilog_base.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -15,12 +15,17 @@ #include "hilog_base/log_base.h" +#include +#include + int HiLogBasePrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) { - (void)type; - (void)level; - (void)domain; - (void)tag; - (void)fmt; + printf("[HLG %d] %s:", level, tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } diff --git a/frameworks/libhilog/mingw/hilog_printf.cpp b/frameworks/libhilog/mingw/hilog_printf.cpp index 941efe6..0dbc5cd 100644 --- a/frameworks/libhilog/mingw/hilog_printf.cpp +++ b/frameworks/libhilog/mingw/hilog_printf.cpp @@ -1,13 +1,33 @@ +/* + * Copyright (c) 2022 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 "hilog/log.h" #include "hilog_trace.h" +#include +#include + int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) { - (void)type; - (void)level; - (void)domain; - (void)tag; - (void)fmt; + printf("[HLG %d] %s:", level, tag); + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\r\n"); + fflush(stdout); return 0; } -- Gitee From 8cb1c05e412cfb60380d7cd932f48cb4d15b48ea Mon Sep 17 00:00:00 2001 From: luwh0708 <1632083718@qq.com> Date: Sun, 1 May 2022 00:27:25 +0800 Subject: [PATCH 3/3] // update log Change-Id: I6475720f22ea06d1a878c5297b8565614aae4e74 --- frameworks/libhilog/BUILD.gn | 3 + frameworks/libhilog/mingw/hilog.cpp | 88 +++++++++++++--------- frameworks/libhilog/mingw/hilog_base.cpp | 47 +++++++++++- frameworks/libhilog/mingw/hilog_printf.cpp | 47 +++++++++++- 4 files changed, 144 insertions(+), 41 deletions(-) diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index 1c6c6a5..f8ad451 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -96,6 +96,8 @@ ohos_source_set("libhilog_source") { "mingw/hilog_printf.cpp", ] + deps += [ "//utils/native/base:utilsecurec_ace_allplatforms" ] + deps -= [ "//third_party/bounds_checking_function:libsec_shared" ] } @@ -130,6 +132,7 @@ ohos_source_set("libhilog_base_source") { sources -= [ "$libhilog_base_root/hilog_base.cpp" ] sources -= vsnprintf_sources sources += [ "mingw/hilog_base.cpp" ] + configs += [ "//utils/native/base:utils_config" ] } part_name = "hilog_native" diff --git a/frameworks/libhilog/mingw/hilog.cpp b/frameworks/libhilog/mingw/hilog.cpp index 132c632..de25517 100644 --- a/frameworks/libhilog/mingw/hilog.cpp +++ b/frameworks/libhilog/mingw/hilog.cpp @@ -15,67 +15,85 @@ #include "hilog/log.h" +#include #include #include +#include +#include +#include + +#include +#include namespace OHOS::HiviewDFX { -int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...) +namespace { +constexpr uint32_t MAX_TIME_SIZE = 32; +constexpr uint32_t MAX_BUFFER_SIZE = 4000; +} // namespace + +std::string GetTimeStamp() { - printf("[HLG D] %s:", label.tag); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - printf("\r\n"); + time_t tt = time(nullptr); + tm* t = localtime(&tt); + char time[MAX_TIME_SIZE]; + + int64_t nowMs = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + + if (sprintf_s(time, MAX_TIME_SIZE, " %02d/%02d %02d:%02d:%02d.%03d", t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, + t->tm_sec, nowMs % 1000) < 0) { + return std::string(); + } + return std::string(time); +} + +#define GENERATE_HILOG_CODE(level, label, fmt) \ + std::string newFmt(fmt); \ + char buf[MAX_BUFFER_SIZE]; \ + va_list ap; \ + va_start(ap, fmt); \ + if (vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, newFmt.c_str(), ap) < 0 && errno == EINVAL) { \ + va_end(ap); \ + return EINVAL; \ + } \ + va_end(ap); \ + std::string newTimeFmt("[%-3s " level "] %s %-6d"); \ + char timeBuf[MAX_BUFFER_SIZE]; \ + if (_snprintf_s(timeBuf, sizeof(timeBuf), sizeof(timeBuf) - 1, newTimeFmt.c_str(), \ + "HLG", GetTimeStamp().c_str(), \ + std::this_thread::get_id()) < 0) { \ + return 1; \ + } \ + printf("%s %s\n", timeBuf, buf); \ fflush(stdout); + +int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...) +{ + GENERATE_HILOG_CODE("D", label, fmt); return 0; } int HiLog::Info(const HiLogLabel &label, const char *fmt, ...) { - printf("[HLG I] %s:", label.tag); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - printf("\r\n"); - fflush(stdout); + GENERATE_HILOG_CODE("I", label, fmt); return 0; } int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) { - printf("[HLG W] %s:", label.tag); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - printf("\r\n"); - fflush(stdout); + GENERATE_HILOG_CODE("W", label, fmt); return 0; } int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) { - printf("[HLG E] %s:", label.tag); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - printf("\r\n"); - fflush(stdout); + GENERATE_HILOG_CODE("E", label, fmt); return 0; } int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) { - printf("[HLG F] %s:", label.tag); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - printf("\r\n"); - fflush(stdout); + GENERATE_HILOG_CODE("F", label, fmt); return 0; } } // namespace OHOS::HiviewDFX diff --git a/frameworks/libhilog/mingw/hilog_base.cpp b/frameworks/libhilog/mingw/hilog_base.cpp index a7c50e8..24d552a 100644 --- a/frameworks/libhilog/mingw/hilog_base.cpp +++ b/frameworks/libhilog/mingw/hilog_base.cpp @@ -15,17 +15,58 @@ #include "hilog_base/log_base.h" +#include #include #include +#include +#include +#include + +#include +#include + +namespace { +constexpr uint32_t MAX_TIME_SIZE = 32; +constexpr uint32_t MAX_BUFFER_SIZE = 4000; +} // namespace + +std::string GetTimeStamp() +{ + time_t tt = time(nullptr); + tm* t = localtime(&tt); + char time[MAX_TIME_SIZE]; + + int64_t nowMs = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + + if (sprintf_s(time, MAX_TIME_SIZE, " %02d/%02d %02d:%02d:%02d.%03d", t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, + t->tm_sec, nowMs % 1000) < 0) { + return std::string(); + } + return std::string(time); +} int HiLogBasePrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) { - printf("[HLG %d] %s:", level, tag); + std::string newFmt(fmt); + + char buf[MAX_BUFFER_SIZE]; va_list ap; va_start(ap, fmt); - vprintf(fmt, ap); + if (vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, newFmt.c_str(), ap) < 0 && errno == EINVAL) { + va_end(ap); + return EINVAL; + } va_end(ap); - printf("\r\n"); + + std::string newTimeFmt("[%-3s %d] %s %-6d"); + char timeBuf[MAX_BUFFER_SIZE]; + if (_snprintf_s(timeBuf, sizeof(timeBuf), sizeof(timeBuf) - 1, newTimeFmt.c_str(), + "HLG", level, GetTimeStamp().c_str(), + std::this_thread::get_id()) < 0) { + return 1; + } + printf("%s %s\n", timeBuf, buf); fflush(stdout); return 0; } diff --git a/frameworks/libhilog/mingw/hilog_printf.cpp b/frameworks/libhilog/mingw/hilog_printf.cpp index 0dbc5cd..bdaf378 100644 --- a/frameworks/libhilog/mingw/hilog_printf.cpp +++ b/frameworks/libhilog/mingw/hilog_printf.cpp @@ -16,17 +16,58 @@ #include "hilog/log.h" #include "hilog_trace.h" +#include #include #include +#include +#include +#include + +#include +#include + +namespace { +constexpr uint32_t MAX_TIME_SIZE = 32; +constexpr uint32_t MAX_BUFFER_SIZE = 4000; +} // namespace + +std::string GetTimeStamp() +{ + time_t tt = time(nullptr); + tm* t = localtime(&tt); + char time[MAX_TIME_SIZE]; + + int64_t nowMs = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + + if (sprintf_s(time, MAX_TIME_SIZE, " %02d/%02d %02d:%02d:%02d.%03d", t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, + t->tm_sec, nowMs % 1000) < 0) { + return std::string(); + } + return std::string(time); +} int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) { - printf("[HLG %d] %s:", level, tag); + std::string newFmt(fmt); + + char buf[MAX_BUFFER_SIZE]; va_list ap; va_start(ap, fmt); - vprintf(fmt, ap); + if (vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, newFmt.c_str(), ap) < 0 && errno == EINVAL) { + va_end(ap); + return EINVAL; + } va_end(ap); - printf("\r\n"); + + std::string newTimeFmt("[%-3s %d] %s %-6d"); + char timeBuf[MAX_BUFFER_SIZE]; + if (_snprintf_s(timeBuf, sizeof(timeBuf), sizeof(timeBuf) - 1, newTimeFmt.c_str(), + "HLG", level, GetTimeStamp().c_str(), + std::this_thread::get_id()) < 0) { + return 1; + } + printf("%s %s\n", timeBuf, buf); fflush(stdout); return 0; } -- Gitee