From ef4132e2a7072f79798111fe539b6caaaef30e73 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 17 Aug 2022 14:34:07 +0800 Subject: [PATCH] update: Log file path supports unicode string --- DSView/pv/appcontrol.cpp | 16 +++++----------- DSView/pv/log.cpp | 8 ++++++-- DSView/pv/utility/path.cpp | 18 ++++++++++++++++++ DSView/pv/utility/path.h | 2 ++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/DSView/pv/appcontrol.cpp b/DSView/pv/appcontrol.cpp index c1e6ac17..4c058460 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -26,14 +26,14 @@ #include #include #include +#include #include "devicemanager.h" #include "sigsession.h" #include "dsvdef.h" #include "config/appconfig.h" #include "log.h" -#include -#include +#include "utility/path.h" AppControl::AppControl() { @@ -81,16 +81,10 @@ bool AppControl::Init() } _session->set_sr_context(sr_ctx); + // firmware resource directory QString resdir = GetResourceDir(); - char res_path[256] = {0}; -#ifdef _WIN32 - QTextCodec *codec = QTextCodec::codecForName("System"); - QByteArray str_tmp = codec->fromUnicode(resdir); - strncpy(res_path, str_tmp.data(), sizeof(res_path) - 1); -#else - strncpy(res_path, resdir.toUtf8().data(), sizeof(res_path) - 1); -#endif - sr_set_firmware_resource_dir(res_path); + std::string res_path = pv::path::ToUnicodePath(resdir); + sr_set_firmware_resource_dir(res_path.c_str()); #if defined(_WIN32) && defined(DEBUG_INFO) //able run debug with qtcreator diff --git a/DSView/pv/log.cpp b/DSView/pv/log.cpp index 695fffec..6f151b0f 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -23,6 +23,8 @@ #include #include #include "config/appconfig.h" +#include "utility/path.h" +#include xlog_writer *dsv_log = nullptr; static xlog_context *log_ctx = nullptr; @@ -71,10 +73,12 @@ void dsv_log_enalbe_logfile(bool append) dsv_info("%s\"%s\"", "Store log to file: ", lf.toUtf8().data()); - int ret = xlog_add_receiver_from_file(log_ctx, lf.toUtf8().data(), &log_file_index, append); + std::string log_file = pv::path::ToUnicodePath(lf); + + int ret = xlog_add_receiver_from_file(log_ctx, log_file.c_str(), &log_file_index, append); if (ret != 0){ dsv_err("%s", "Create log file error!"); - } + } } } diff --git a/DSView/pv/utility/path.cpp b/DSView/pv/utility/path.cpp index 602563d1..44dcc2b7 100644 --- a/DSView/pv/utility/path.cpp +++ b/DSView/pv/utility/path.cpp @@ -20,6 +20,9 @@ */ #include "path.h" +#ifdef _WIN32 +#include +#endif namespace pv{ namespace path{ @@ -38,5 +41,20 @@ namespace path{ } return path; } + + std::string ToUnicodePath(QString path) + { + std::string str; + +#ifdef _WIN32 + QTextCodec *codec = QTextCodec::codecForName("System"); + QByteArray str_tmp = codec->fromUnicode(path); + str = str_tmp.data(); +#else + str = path.toUtf8().data(); +#endif + + return str; + } } } \ No newline at end of file diff --git a/DSView/pv/utility/path.h b/DSView/pv/utility/path.h index 7d6e40a7..f5841f29 100644 --- a/DSView/pv/utility/path.h +++ b/DSView/pv/utility/path.h @@ -30,6 +30,8 @@ namespace path{ std::string ConvertPath(QString fileName); QString GetDirectoryName(QString path); + + std::string ToUnicodePath(QString path); } }