From b486761fa01d84b21e9358ec3419e254313d3dc8 Mon Sep 17 00:00:00 2001 From: liyu Date: Wed, 21 Sep 2022 13:52:19 +0800 Subject: [PATCH 1/3] + windows_implement Change-Id: Ic048c6017ad859a992d20698c99433616a6c7483 Signed-off-by: liyu --- frameworks/libhilog/BUILD.gn | 14 +++ frameworks/libhilog/mingw/hilog.cpp | 104 +++++++++++++++++++++ frameworks/libhilog/mingw/hilog_base.cpp | 76 +++++++++++++++ frameworks/libhilog/mingw/hilog_printf.cpp | 103 ++++++++++++++++++++ interfaces/native/innerkits/BUILD.gn | 8 +- 5 files changed, 299 insertions(+), 6 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 fe1a662..0f65c54 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -112,6 +112,13 @@ foreach(item, platforms) { platform = item } } +group("libhilog_source") { + platform = current_os + if (current_os == "mingw") { + platform = "windows" + } + public_deps = [ ":libhilog_source_$platform" ] +} config("libhilog_base_config") { visibility = [ "*:*" ] @@ -129,6 +136,13 @@ ohos_source_set("libhilog_base_source") { sources = [ "$libhilog_base_root/hilog_base.cpp" ] sources += vsnprintf_sources + if (current_os == "mingw" || current_os == "mac") { + sources -= [ "$libhilog_base_root/hilog_base.cpp" ] + sources += [ "mingw/hilog_base.cpp" ] + deps = ["//third_party/bounds_checking_function:libsec_shared"] + cflags = [ "-Wno-inconsistent-dllimport" ] + } + defines = [ "__RECV_MSG_WITH_UCRED_", "HILOG_PROHIBIT_ALLOCATION", diff --git a/frameworks/libhilog/mingw/hilog.cpp b/frameworks/libhilog/mingw/hilog.cpp new file mode 100644 index 0000000..8fcfba2 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog.cpp @@ -0,0 +1,104 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include + +#include "vsnprintf_s_p.h" + +#ifndef _WIN32 +#define _snprintf_s snprintf_s +#endif + +namespace OHOS::HiviewDFX { +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); +} + +#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, ...) +{ + GENERATE_HILOG_CODE("I", label, fmt); + return 0; +} + +int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) +{ + GENERATE_HILOG_CODE("W", label, fmt); + return 0; +} + +int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) +{ + GENERATE_HILOG_CODE("E", label, fmt); + return 0; +} + +int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) +{ + 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 new file mode 100644 index 0000000..63bf8d7 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_base.cpp @@ -0,0 +1,76 @@ +/* + * 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_base/log_base.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include "vsnprintf_s_p.h" + +#ifdef __APPLE__ +#define _snprintf_s snprintf_s +#endif + +namespace { +constexpr uint32_t MAX_TIME_SIZE = 32; +} // 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, ...) +{ + std::string newFmt(fmt); + + char buf[MAX_LOG_LEN]; + 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 %d] %s %-6d"); + char timeBuf[MAX_LOG_LEN]; + 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 new file mode 100644 index 0000000..4e5b48c --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_printf.cpp @@ -0,0 +1,103 @@ +/* + * 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 "hilog_inner.h" + +#include +#include +#include +#include +#include +#include + +#include + +#include "vsnprintf_s_p.h" + +#ifndef _WIN32 +#define _snprintf_s snprintf_s +#endif + +namespace { +constexpr uint32_t MAX_TIME_SIZE = 32; +constexpr uint32_t MAX_LOG_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 HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, + const char *fmt, va_list ap) +{ + std::string newFmt(fmt); + + char buf[MAX_LOG_BUFFER_SIZE]; + if (vsnprintfp_s(buf, sizeof(buf), sizeof(buf) - 1, false, newFmt.c_str(), ap) < 0 && errno == EINVAL) { + va_end(ap); + return EINVAL; + } + + std::string newTimeFmt("[%-3s %d] %s %-6d"); + char timeBuf[MAX_LOG_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; +} + +int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) +{ + int ret; + va_list ap; + va_start(ap, fmt); + ret = HiLogPrintArgs(type, level, domain, tag, fmt, ap); + va_end(ap); + return ret; +} + +int HiLogRegisterGetIdFun(RegisterFunc func) +{ + (void)func; + return 0; +} + +void HiLogUnregisterGetIdFun(RegisterFunc func) +{ + (void)func; +} + +bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level) +{ + return true; +} diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index 3666081..5da3c34 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -24,11 +24,9 @@ template("libhilog") { ohos_shared_library(target_name) { public_configs = [ ":libhilog_pub_config" ] - deps = [ - "//base/hiviewdfx/hilog/frameworks/libhilog:libhilog_source_$platform", - ] + deps = [ "//base/hiviewdfx/hilog/frameworks/libhilog:libhilog_source" ] - if (platform == "ohos") { + if (current_os == "ohos") { output_extension = "so" } @@ -45,12 +43,10 @@ template("libhilog") { foreach(item, platforms) { if (item == "ohos") { libhilog("libhilog") { - platform = item } } if (item == "windows" || item == "mac" || item == "linux") { libhilog("libhilog_" + item) { - platform = item } } } -- Gitee From 053e77b3876f25878bac0ebdc8a1f1a01f74e251 Mon Sep 17 00:00:00 2001 From: liyu Date: Wed, 21 Sep 2022 13:52:20 +0800 Subject: [PATCH 2/3] * copyright Change-Id: I933c88c3a021023b1eda2452c0d2889bb352120a Signed-off-by: liyu --- frameworks/libhilog/BUILD.gn | 2 +- interfaces/native/innerkits/BUILD.gn | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index 0f65c54..97a928f 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index 5da3c34..e13f1cb 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 -- Gitee From 07af79bf9d44436d3f6399095c7167662233c84d Mon Sep 17 00:00:00 2001 From: liyu Date: Wed, 21 Sep 2022 13:52:20 +0800 Subject: [PATCH 3/3] + add target proxy Change-Id: I4979035ca30927ed148febd5628b3e5edd675230 Signed-off-by: liyu --- interfaces/native/innerkits/BUILD.gn | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index e13f1cb..5348a23 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -41,14 +41,16 @@ template("libhilog") { } foreach(item, platforms) { - if (item == "ohos") { - libhilog("libhilog") { - } + libhilog("libhilog_" + item) { } - if (item == "windows" || item == "mac" || item == "linux") { - libhilog("libhilog_" + item) { - } +} + +group("libhilog") { + platform = current_os + if (current_os == "mingw") { + platform = "windows" } + public_deps = [ ":libhilog_$platform" ] } config("libhilog_base_pub_cfg") { -- Gitee