diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index c1407fa959321e7c9ee41804d8cecdb2e6ab46f5..f8ad451192fd5310c74956a40c50d3a78cac09b9 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -76,10 +76,30 @@ 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 += [ "//utils/native/base:utilsecurec_ace_allplatforms" ] + + deps -= [ "//third_party/bounds_checking_function:libsec_shared" ] + } part_name = "hilog_native" subsystem_name = "hiviewdfx" @@ -108,6 +128,13 @@ 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" ] + configs += [ "//utils/native/base:utils_config" ] + } + 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 0000000000000000000000000000000000000000..de25517625605fa306e1049a45c80de4b39d1778 --- /dev/null +++ b/frameworks/libhilog/mingw/hilog.cpp @@ -0,0 +1,99 @@ +/* + * 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 + +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 0000000000000000000000000000000000000000..24d552aba52f566be82e2fad6c01130e67a51d7b --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_base.cpp @@ -0,0 +1,72 @@ +/* + * 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 + +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, ...) +{ + 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 %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 new file mode 100644 index 0000000000000000000000000000000000000000..bdaf378e0f2dc59bb41807d7471acfa74113413d --- /dev/null +++ b/frameworks/libhilog/mingw/hilog_printf.cpp @@ -0,0 +1,84 @@ +/* + * 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 +#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, ...) +{ + 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 %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; +} + +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 98b833faa5664fac4d62c9e8745da7bbd3061c2a..e47515c3ea0514f3bf4e75908ac017b38ac222c0 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",