From 0df2da582d441423ce5b43d9b230808494be53ad Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 17 Aug 2022 13:46:02 +0800 Subject: [PATCH 1/2] add: Log setting dialog --- DSView/main.cpp | 18 +++++++--- DSView/pv/config/appconfig.cpp | 6 ++++ DSView/pv/config/appconfig.h | 2 ++ DSView/pv/log.cpp | 22 +++++++++---- DSView/pv/log.h | 3 +- DSView/pv/toolbars/logobar.cpp | 60 ++++++++++++++++++++++++++++++++++ DSView/pv/toolbars/logobar.h | 2 ++ common/log/xlog.c | 8 +++-- common/log/xlog.h | 2 +- 9 files changed, 109 insertions(+), 14 deletions(-) diff --git a/DSView/main.cpp b/DSView/main.cpp index be706e98..f4844098 100644 --- a/DSView/main.cpp +++ b/DSView/main.cpp @@ -185,20 +185,30 @@ bool bHighScale = true; #ifdef DEBUG_INFO if (XLOG_LEVEL_INFO > logLevel){ dsv_log_level(XLOG_LEVEL_INFO); // on develop mode, set the default log ldevel + logLevel = XLOG_LEVEL_INFO; } #endif if (bStoreLog){ - dsv_log_enalbe_logfile(); + dsv_log_enalbe_logfile(true); } + AppControl *control = AppControl::Instance(); + AppConfig &app = AppConfig::Instance(); + app.LoadAll(); //load app config + + if (app._appOptions.ableSaveLog){ + dsv_log_enalbe_logfile(false); + + if (app._appOptions.logLevel >= logLevel){ + dsv_log_level(app._appOptions.logLevel); + } + } + //----------------------run dsv_info("----------------- version: %s-----------------", DS_VERSION_STRING); dsv_info("Qt:%s", QT_VERSION_STR); - AppControl *control = AppControl::Instance(); - AppConfig::Instance().LoadAll(); //load app config - //init core if (!control->Init()){ dsv_err("%s", "init error!"); diff --git a/DSView/pv/config/appconfig.cpp b/DSView/pv/config/appconfig.cpp index 3d72b46a..78ad1470 100644 --- a/DSView/pv/config/appconfig.cpp +++ b/DSView/pv/config/appconfig.cpp @@ -94,6 +94,8 @@ void _loadApp(AppOptions &o, QSettings &st){ getFiled("quickScroll", st, o.quickScroll, true); getFiled("warnofMultiTrig", st, o.warnofMultiTrig, true); getFiled("originalData", st, o.originalData, false); + getFiled("ableSaveLog", st, o.ableSaveLog, false); + getFiled("logLevel", st, o.logLevel, 3); QString fmt; getFiled("protocalFormats", st, fmt, ""); @@ -109,6 +111,8 @@ void _saveApp(AppOptions &o, QSettings &st){ setFiled("quickScroll", st, o.quickScroll); setFiled("warnofMultiTrig", st, o.warnofMultiTrig); setFiled("originalData", st, o.originalData); + setFiled("ableSaveLog", st, o.ableSaveLog); + setFiled("logLevel", st, o.logLevel); QString fmt = FormatArrayToString(o.m_protocolFormats); setFiled("protocalFormats", st, fmt); @@ -238,6 +242,8 @@ void AppConfig::LoadAll() _loadApp(_appOptions, st); _loadHistory(_userHistory, st); _loadFrame(_frameOptions, st); + + //dsv_dbg("Config file path:\"%s\"", st.fileName().toUtf8().data()); } void AppConfig::SaveApp() diff --git a/DSView/pv/config/appconfig.h b/DSView/pv/config/appconfig.h index bf300a97..49eca089 100644 --- a/DSView/pv/config/appconfig.h +++ b/DSView/pv/config/appconfig.h @@ -57,6 +57,8 @@ struct AppOptions bool quickScroll; bool warnofMultiTrig; bool originalData; + bool ableSaveLog; + int logLevel; std::vector m_protocolFormats; }; diff --git a/DSView/pv/log.cpp b/DSView/pv/log.cpp index a5eb4bee..695fffec 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -26,7 +26,8 @@ xlog_writer *dsv_log = nullptr; static xlog_context *log_ctx = nullptr; -bool b_logfile = false; +static bool b_logfile = false; +static int log_file_index = -1; void dsv_log_init() { @@ -52,10 +53,10 @@ xlog_context* dsv_log_context() void dsv_log_level(int l) { xlog_set_level(log_ctx, l); - dsv_info("%s%d", "set log level: ", l); + dsv_info("%s%d", "Set log level: ", l); } -void dsv_log_enalbe_logfile() +void dsv_log_enalbe_logfile(bool append) { if (!b_logfile && log_ctx){ b_logfile = true; @@ -68,11 +69,20 @@ void dsv_log_enalbe_logfile() lf = GetAppDataDir() + "/DSView.log"; #endif - dsv_info("%s\"%s\"", "store log to file: ", lf.toUtf8().data()); + dsv_info("%s\"%s\"", "Store log to file: ", lf.toUtf8().data()); - int ret = xlog_add_receiver_from_file(log_ctx, lf.toUtf8().data(), 0); + int ret = xlog_add_receiver_from_file(log_ctx, lf.toUtf8().data(), &log_file_index, append); if (ret != 0){ - dsv_err("%s", "create log file error!"); + dsv_err("%s", "Create log file error!"); } } +} + +void dsv_remove_log_file() +{ + if (b_logfile && log_ctx) + { + b_logfile = false; + xlog_remove_receiver_by_index(log_ctx, log_file_index); + } } \ No newline at end of file diff --git a/DSView/pv/log.h b/DSView/pv/log.h index d3ca912b..ae96da1f 100644 --- a/DSView/pv/log.h +++ b/DSView/pv/log.h @@ -32,7 +32,8 @@ void dsv_log_uninit(); xlog_context* dsv_log_context(); void dsv_log_level(int l); -void dsv_log_enalbe_logfile(); +void dsv_log_enalbe_logfile(bool append); +void dsv_remove_log_file(); #define LOG_PREFIX "" #define dsv_err(fmt, args...) xlog_err(dsv_log, LOG_PREFIX fmt, ## args) diff --git a/DSView/pv/toolbars/logobar.cpp b/DSView/pv/toolbars/logobar.cpp index 3617c17d..48ff6271 100644 --- a/DSView/pv/toolbars/logobar.cpp +++ b/DSView/pv/toolbars/logobar.cpp @@ -28,11 +28,18 @@ #include #include #include +#include +#include +#include +#include #include "logobar.h" #include "../dialogs/about.h" #include "../dialogs/dsmessagebox.h" #include "../config/appconfig.h" +#include "../dialogs/dsdialog.h" +#include "../appcontrol.h" +#include "../log.h" namespace pv { namespace toolbars { @@ -76,6 +83,7 @@ LogoBar::LogoBar(SigSession *session, QWidget *parent) : _logo_button.addAction(_issue); _update = new QAction(this); + _log = new QAction(this); _menu = new QMenu(this); _menu->addMenu(_language); @@ -83,6 +91,7 @@ LogoBar::LogoBar(SigSession *session, QWidget *parent) : _menu->addAction(_manual); _menu->addAction(_issue); _menu->addAction(_update); + _menu->addAction(_log); _logo_button.setMenu(_menu); _logo_button.setToolButtonStyle(Qt::ToolButtonTextUnderIcon); @@ -104,6 +113,7 @@ LogoBar::LogoBar(SigSession *session, QWidget *parent) : connect(_manual, SIGNAL(triggered()), this, SIGNAL(sig_open_doc())); connect(_issue, SIGNAL(triggered()), this, SLOT(on_actionIssue_triggered())); connect(_update, SIGNAL(triggered()), this, SLOT(on_action_update())); + connect(_log, SIGNAL(triggered()), this, SLOT(on_action_setting_log())); } void LogoBar::changeEvent(QEvent *event) @@ -125,6 +135,7 @@ void LogoBar::retranslateUi() _manual->setText(tr("&Manual")); _issue->setText(tr("&Bug Report")); _update->setText(tr("&Update")); + _log->setText(tr("L&og Options")); AppConfig &app = AppConfig::Instance(); if (app._frameOptions.language == LAN_CN) @@ -141,6 +152,7 @@ void LogoBar::reStyle() _manual->setIcon(QIcon(iconPath+"/manual.svg")); _issue->setIcon(QIcon(iconPath+"/bug.svg")); _update->setIcon(QIcon(iconPath+"/update.svg")); + _log->setIcon(QIcon(iconPath+"/file.svg")); if (_connected) _logo_button.setIcon(QIcon(iconPath+"/logo_color.svg")); @@ -226,5 +238,53 @@ void LogoBar::enable_toggle(bool enable) _logo_button.setDisabled(!enable); } +void LogoBar::on_action_setting_log() +{ + AppConfig &app = AppConfig::Instance(); + auto *topWind = AppControl::Instance()->GetTopWindow(); + dialogs::DSDialog dlg(topWind, false, true); + dlg.setTitle(tr("Log Options")); + dlg.setMinimumSize(260, 120); + QWidget *panel = new QWidget(&dlg); + dlg.layout()->addWidget(panel); + panel->setMinimumSize(250, 110); + QFormLayout *lay = new QFormLayout(); + panel->setLayout(lay); + lay->setVerticalSpacing(15); + + QComboBox *cbBox = new QComboBox(); + cbBox->setMinimumWidth(40); + lay->addRow(tr("Log Level"), cbBox); + + for (int i=0; i<=5; i++){ + cbBox->addItem(QString::number(i)); + } + cbBox->setCurrentIndex(app._appOptions.logLevel); + + QCheckBox *ckBox = new QCheckBox(); + ckBox->setChecked(app._appOptions.ableSaveLog); + lay->addRow(tr("Save To File"), ckBox); + + dlg.exec(); + + if (dlg.IsClickYes()){ + bool ableSave = ckBox->isChecked(); + int level = cbBox->currentIndex(); + + if (ableSave != app._appOptions.ableSaveLog || level != app._appOptions.logLevel){ + app._appOptions.ableSaveLog = ableSave; + app._appOptions.logLevel = level; + app.SaveApp(); + + dsv_log_level(level); + + if (ableSave) + dsv_log_enalbe_logfile(false); + else + dsv_remove_log_file(); + } + } +} + } // namespace toolbars } // namespace pv diff --git a/DSView/pv/toolbars/logobar.h b/DSView/pv/toolbars/logobar.h index 2ec47acc..ef1bee86 100644 --- a/DSView/pv/toolbars/logobar.h +++ b/DSView/pv/toolbars/logobar.h @@ -75,6 +75,7 @@ private slots: void on_actionManual_triggered(); void on_actionIssue_triggered(); void on_action_update(); + void on_action_setting_log(); private: bool _enable; @@ -93,6 +94,7 @@ private: QAction *_manual; QAction *_issue; QAction *_update; + QAction *_log; IMainForm *_mainForm; }; diff --git a/common/log/xlog.c b/common/log/xlog.c index c8f86b27..246aeda5 100644 --- a/common/log/xlog.c +++ b/common/log/xlog.c @@ -232,7 +232,7 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receiver rev, int *out_in * append a log data receiver, return 0 if success. * the log data will be writed to file. */ -XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_path, int *out_index) +XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_path, int *out_index, int bAppend) { int i; FILE *fh = NULL; @@ -258,7 +258,11 @@ XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_pat return -1; } - fh = fopen(file_path, "a+"); + if (bAppend) + fh = fopen(file_path, "a+"); + else + fh = fopen(file_path, "w+"); + if (fh == NULL){ strcpy(ctx->_error, "open file error"); pthread_mutex_unlock(&ctx->_mutext); diff --git a/common/log/xlog.h b/common/log/xlog.h index 947113c0..bb3c7b7a 100644 --- a/common/log/xlog.h +++ b/common/log/xlog.h @@ -85,7 +85,7 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receiver rev, int *out_in * append a log data receiver, return 0 if success. * the log data will be writed to file. */ -XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_path, int *out_index); +XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_path, int *out_index, int bAppend); /** * remove a log data receiver,return 0 if success. From ef4132e2a7072f79798111fe539b6caaaef30e73 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 17 Aug 2022 14:34:07 +0800 Subject: [PATCH 2/2] 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); } }