diff --git a/DSView/main.cpp b/DSView/main.cpp index dce0339f..60ae81b0 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/appcontrol.cpp b/DSView/pv/appcontrol.cpp index b34dfd1f..911b496a 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -25,13 +25,13 @@ #include #include #include -#include +#include +#include #include "sigsession.h" #include "dsvdef.h" #include "config/appconfig.h" #include "log.h" -#include -#include +#include "utility/path.h" AppControl::AppControl() { @@ -66,7 +66,7 @@ bool AppControl::Init() { _session->init(); - srd_log_set_context(dsv_log_context()); + srd_log_set_context(dsv_log_context()); #if defined(_WIN32) && defined(DEBUG_INFO) //able run debug with qtcreator 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..6f151b0f 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -23,10 +23,13 @@ #include #include #include "config/appconfig.h" +#include "utility/path.h" +#include 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 +55,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 +71,22 @@ 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); + 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!"); - } + 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/sigsession.cpp b/DSView/pv/sigsession.cpp index c97f3f98..cf612281 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -60,6 +60,7 @@ #include "dsvdef.h" #include "log.h" #include "config/appconfig.h" +#include "utility/path.h" namespace pv { @@ -110,8 +111,6 @@ SigSession::SigSession() _group_data = new data::Group(); _group_cnt = 0; - _sr_ctx = NULL; - _feed_timer.Stop(); _feed_timer.SetCallback(std::bind(&SigSession::feed_timeout, this)); } @@ -145,8 +144,7 @@ void SigSession::set_device(DevInst *dev_inst) RELEASE_ARRAY(_group_traces); - if (_dev_inst) { - sr_session_datafeed_callback_remove_all(); + if (_dev_inst) { _dev_inst->release(); _dev_inst = NULL; } @@ -1887,6 +1885,17 @@ void SigSession::set_stop_scale(float scale) _bDecodeRunning = false; } + void SigSession::get_device_list(std::vector &devices) + { + struct ds_device_info *array = NULL; + int num = 0; + + if (ds_get_device_list(&array, &num) != SR_OK){ + dsv_err("%s", "Failed to get device list!"); + return; + } + } + void SigSession::device_lib_event_callback(int event) { struct ds_device_info *array = NULL; @@ -1917,16 +1926,10 @@ void SigSession::set_stop_scale(float scale) ds_set_datafeed_callback(data_feed_callback); + // 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 - ds_set_firmware_resource_dir(res_path); + std::string res_path = pv::path::ToUnicodePath(resdir); + ds_set_firmware_resource_dir(res_path.c_str()); if (ds_lib_init() != SR_OK) { @@ -1946,7 +1949,7 @@ void SigSession::set_stop_scale(float scale) int SigSession::get_device_work_mode() { - return ds_get_selected_device_index() + return ds_get_actived_device_mode(); } } // namespace pv diff --git a/DSView/pv/sigsession.h b/DSView/pv/sigsession.h index 85605820..00a0f407 100644 --- a/DSView/pv/sigsession.h +++ b/DSView/pv/sigsession.h @@ -310,6 +310,7 @@ private: void feed_timeout(); void repeat_update(); + private: /** * Attempts to autodetect the format. Failing that @@ -332,6 +333,8 @@ private: void data_feed_in(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet); + void get_device_list(std::vector &devices); + static void data_feed_callback(const struct sr_dev_inst *sdi, const struct sr_datafeed_packet *packet); 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 8a244b4b..2a9ee401 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/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); } } diff --git a/common/log/xlog.c b/common/log/xlog.c index 8093840a..f28ec57e 100644 --- a/common/log/xlog.c +++ b/common/log/xlog.c @@ -249,7 +249,7 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receive_callback rev, int * 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; @@ -275,7 +275,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 2ecd170c..8d02fca0 100644 --- a/common/log/xlog.h +++ b/common/log/xlog.h @@ -90,7 +90,7 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receive_callback rev, int * 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.