diff --git a/DSView/main.cpp b/DSView/main.cpp index 0e266bc1..7332c87a 100644 --- a/DSView/main.cpp +++ b/DSView/main.cpp @@ -35,6 +35,8 @@ #include "pv/appcontrol.h" #include "pv/log.h" #include "pv/ui/langresource.h" +#include +#include #ifdef _WIN32 #include @@ -199,7 +201,7 @@ bool bHighScale = true; LangResource::Instance()->Load(app._frameOptions.language); if (app._appOptions.ableSaveLog){ - dsv_log_enalbe_logfile(false); + dsv_log_enalbe_logfile(app._appOptions.appendLogMode); if (app._appOptions.logLevel >= logLevel){ dsv_log_level(app._appOptions.logLevel); @@ -209,6 +211,10 @@ bool bHighScale = true; //----------------------run dsv_info("----------------- version: %s-----------------", DS_VERSION_STRING); dsv_info("Qt:%s", QT_VERSION_STR); + + QDateTime dateTime = QDateTime::currentDateTime(); + std::string strTime = dateTime .toString("yyyy-MM-dd hh:mm:ss").toStdString(); + dsv_info("%s", strTime.c_str()); //init core if (!control->Init()){ diff --git a/DSView/pv/config/appconfig.cpp b/DSView/pv/config/appconfig.cpp index 53306ba8..a637095f 100644 --- a/DSView/pv/config/appconfig.cpp +++ b/DSView/pv/config/appconfig.cpp @@ -95,6 +95,7 @@ void _loadApp(AppOptions &o, QSettings &st){ getFiled("warnofMultiTrig", st, o.warnofMultiTrig, true); getFiled("originalData", st, o.originalData, false); getFiled("ableSaveLog", st, o.ableSaveLog, false); + getFiled("appendLogMode", st, o.appendLogMode, false); getFiled("logLevel", st, o.logLevel, 3); getFiled("transDecoderDlg", st, o.transDecoderDlg, true); @@ -113,6 +114,7 @@ void _saveApp(AppOptions &o, QSettings &st){ setFiled("warnofMultiTrig", st, o.warnofMultiTrig); setFiled("originalData", st, o.originalData); setFiled("ableSaveLog", st, o.ableSaveLog); + setFiled("appendLogMode", st, o.appendLogMode); setFiled("logLevel", st, o.logLevel); setFiled("transDecoderDlg", st, o.transDecoderDlg); diff --git a/DSView/pv/config/appconfig.h b/DSView/pv/config/appconfig.h index 6eb06571..13be468b 100644 --- a/DSView/pv/config/appconfig.h +++ b/DSView/pv/config/appconfig.h @@ -58,6 +58,7 @@ struct AppOptions bool warnofMultiTrig; bool originalData; bool ableSaveLog; + bool appendLogMode; int logLevel; bool transDecoderDlg; diff --git a/DSView/pv/log.cpp b/DSView/pv/log.cpp index 6f151b0f..6999cf5c 100644 --- a/DSView/pv/log.cpp +++ b/DSView/pv/log.cpp @@ -29,7 +29,7 @@ xlog_writer *dsv_log = nullptr; static xlog_context *log_ctx = nullptr; static bool b_logfile = false; -static int log_file_index = -1; +static int log_file_index = -1; void dsv_log_init() { @@ -61,16 +61,10 @@ void dsv_log_level(int l) void dsv_log_enalbe_logfile(bool append) { if (!b_logfile && log_ctx){ - b_logfile = true; + b_logfile = true; + + QString lf = get_dsv_log_path(); - QString lf; - - #ifdef Q_OS_LINUX - lf = QDir::homePath() + "/DSView.log"; - #else - lf = GetAppDataDir() + "/DSView.log"; - #endif - dsv_info("%s\"%s\"", "Store log to file: ", lf.toUtf8().data()); std::string log_file = pv::path::ToUnicodePath(lf); @@ -82,11 +76,36 @@ void dsv_log_enalbe_logfile(bool append) } } +void dsv_clear_log_file() +{ + QString lf = get_dsv_log_path(); + std::string log_file = pv::path::ToUnicodePath(lf); + int ret = xlog_reset_log_file(log_ctx, log_file_index, log_file.c_str()); + + if (ret != 0){ + dsv_err("%s", "Clear 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); + log_file_index = -1; } +} + +QString get_dsv_log_path() +{ + QString lf; + + #ifdef Q_OS_LINUX + lf = QDir::homePath() + "/DSView.log"; + #else + lf = GetAppDataDir() + "/DSView.log"; + #endif + + return lf; } \ No newline at end of file diff --git a/DSView/pv/log.h b/DSView/pv/log.h index ae96da1f..0dc30cc3 100644 --- a/DSView/pv/log.h +++ b/DSView/pv/log.h @@ -23,6 +23,7 @@ #define _DSV_LOG_H_ #include +#include extern xlog_writer *dsv_log; @@ -34,6 +35,9 @@ void dsv_log_level(int l); void dsv_log_enalbe_logfile(bool append); void dsv_remove_log_file(); +void dsv_clear_log_file(); + +QString get_dsv_log_path(); #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 89b04171..192d7260 100644 --- a/DSView/pv/toolbars/logobar.cpp +++ b/DSView/pv/toolbars/logobar.cpp @@ -32,6 +32,11 @@ #include #include #include +#include +#include +#include +#include +#include #include "logobar.h" #include "../dialogs/about.h" @@ -41,6 +46,7 @@ #include "../appcontrol.h" #include "../log.h" #include "../ui/langresource.h" +#include "../ui/msgbox.h" namespace pv { @@ -228,7 +234,7 @@ void LogoBar::on_action_setting_log() auto *topWind = AppControl::Instance()->GetTopWindow(); dialogs::DSDialog dlg(topWind, false, true); dlg.setTitle(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_LOG_OPTIONS), "Log Options")); - dlg.setMinimumSize(260, 120); + dlg.setMinimumSize(460, 300); QWidget *panel = new QWidget(&dlg); dlg.layout()->addWidget(panel); panel->setMinimumSize(250, 110); @@ -245,19 +251,49 @@ void LogoBar::on_action_setting_log() } cbBox->setCurrentIndex(app._appOptions.logLevel); - QCheckBox *ckBox = new QCheckBox(); - ckBox->setChecked(app._appOptions.ableSaveLog); - lay->addRow(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVE_FILE), "Save To File"), ckBox); + QCheckBox *ckSave = new QCheckBox(); + ckSave->setChecked(app._appOptions.ableSaveLog); + lay->addRow(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_SAVE_FILE), "Save To File"), ckSave); + + QCheckBox *ckRebuild = new QCheckBox(); + ckRebuild->setChecked(app._appOptions.appendLogMode); + lay->addRow(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_APPEND_MODE), "Append mode"), ckRebuild); + + QLineEdit *etPath = new QLineEdit(); + etPath->setReadOnly(true); + etPath->setText(get_dsv_log_path()); + lay->addRow(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_LOG_PATH), "File Path"), etPath); + + QPushButton *btOpen = new QPushButton(); + btOpen->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_OPEN), "Open")); + connect(btOpen, SIGNAL(released()), this, SLOT(on_open_log_file())); + + QPushButton *btClear = new QPushButton(); + btClear->setText(L_S(STR_PAGE_DLG, S_ID(IDS_DLG_CLEARE), "Clear")); + connect(btClear, SIGNAL(released()), this, SLOT(on_clear_log_file())); + + QWidget *btWid = new QWidget(); + QHBoxLayout *btLay = new QHBoxLayout(); + btWid->setLayout(btLay); + btLay->setSpacing(10); + btLay->addWidget(btOpen); + btLay->addWidget(btClear); + + lay->addRow("", btWid); dlg.exec(); if (dlg.IsClickYes()){ - bool ableSave = ckBox->isChecked(); + bool ableSave = ckSave->isChecked(); int level = cbBox->currentIndex(); + bool appendLogMode = ckRebuild->isChecked(); - if (ableSave != app._appOptions.ableSaveLog || level != app._appOptions.logLevel){ + if (ableSave != app._appOptions.ableSaveLog + || level != app._appOptions.logLevel + || appendLogMode != app._appOptions.appendLogMode){ app._appOptions.ableSaveLog = ableSave; app._appOptions.logLevel = level; + app._appOptions.appendLogMode = appendLogMode; app.SaveApp(); dsv_log_level(level); @@ -270,5 +306,32 @@ void LogoBar::on_action_setting_log() } } +void LogoBar::on_open_log_file() +{ + QFile qf(get_dsv_log_path()); + if (qf.exists()){ + QDesktopServices::openUrl( QUrl("file:///" + get_dsv_log_path())); + } + else{ + QString strMsg(L_S(STR_PAGE_MSG, S_ID(IDS_MSG_FILE_NOT_EXIST), "Not exist!")); + MsgBox::Show(strMsg); + } +} + +void LogoBar::on_clear_log_file() +{ + QFile qf(get_dsv_log_path()); + if (qf.exists()){ + QString strMsg(L_S(STR_PAGE_MSG, S_ID(IDS_MSG_TO_CLEAR_LOG), "Confirm!")); + if (MsgBox::Confirm(strMsg)){ + dsv_clear_log_file(); + } + } + else{ + QString strMsg(L_S(STR_PAGE_MSG, S_ID(IDS_MSG_FILE_NOT_EXIST), "Not exist!")); + MsgBox::Show(strMsg); + } +} + } // namespace toolbars } // namespace pv diff --git a/DSView/pv/toolbars/logobar.h b/DSView/pv/toolbars/logobar.h index e4c49ccc..513c9a42 100644 --- a/DSView/pv/toolbars/logobar.h +++ b/DSView/pv/toolbars/logobar.h @@ -71,6 +71,8 @@ private slots: void on_actionIssue_triggered(); void on_action_update(); void on_action_setting_log(); + void on_open_log_file(); + void on_clear_log_file(); private: bool _enable; diff --git a/common/log/xlog.c b/common/log/xlog.c index 22f8bab8..6aa3bfa0 100644 --- a/common/log/xlog.c +++ b/common/log/xlog.c @@ -300,6 +300,43 @@ XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_pat return 0; } +/** + * Clear the log file, and reopen from the path. +*/ +XLOG_API int xlog_reset_log_file(xlog_context* ctx, int receiver_index, const char *file_path) +{ + FILE *fh = NULL; + int ret = 0; + + if (ctx == NULL || file_path == NULL || *file_path == 0){ + return -1; + } + + pthread_mutex_lock(&ctx->_mutext); + + if (receiver_index < ctx->_count && ctx->_receivers[receiver_index]._file != NULL) + { + fclose(ctx->_receivers[receiver_index]._file); + ctx->_receivers[receiver_index]._file = NULL; + + fh = fopen(file_path, "w+"); + + if (fh == NULL){ + strcpy(ctx->_error, "open file error"); + ret = -1; + } + else{ + ctx->_receivers[receiver_index]._file = fh; + } + } + else{ + ret = -1; + } + + pthread_mutex_unlock(&ctx->_mutext); + return ret; +} + /** * remove a log data receiver,return 0 if success. */ diff --git a/common/log/xlog.h b/common/log/xlog.h index 8d02fca0..0977cbaa 100644 --- a/common/log/xlog.h +++ b/common/log/xlog.h @@ -92,6 +92,11 @@ XLOG_API int xlog_add_receiver(xlog_context* ctx, xlog_receive_callback rev, int */ XLOG_API int xlog_add_receiver_from_file(xlog_context* ctx, const char *file_path, int *out_index, int bAppend); +/** + * Clear the log file, and reopen from the path. +*/ +XLOG_API int xlog_reset_log_file(xlog_context* ctx, int receiver_index, const char *file_path); + /** * remove a log data receiver,return 0 if success. */ diff --git a/lang/cn/dlg.json b/lang/cn/dlg.json index a745246b..13ffe0e3 100644 --- a/lang/cn/dlg.json +++ b/lang/cn/dlg.json @@ -666,5 +666,21 @@ { "id": "IDS_DLG_DECODER_IF_TRANS", "text": "翻译参数名称" + }, + { + "id": "IDS_DLG_LOG_PATH", + "text": "文件位置" + }, + { + "id": "IDS_DLG_OPEN", + "text": "打开" + }, + { + "id": "IDS_DLG_CLEARE", + "text": "清除" + }, + { + "id": "IDS_DLG_APPEND_MODE", + "text": "追加模式" } ] \ No newline at end of file diff --git a/lang/cn/msg.json b/lang/cn/msg.json index 78359aca..a53b5cb5 100644 --- a/lang/cn/msg.json +++ b/lang/cn/msg.json @@ -357,5 +357,13 @@ { "id": "IDS_MSG_NO_ENABLED_CHANNEL", "text": "未启用通道!" + }, + { + "id": "IDS_MSG_FILE_NOT_EXIST", + "text": "文件不存在!" + }, + { + "id": "IDS_MSG_TO_CLEAR_LOG", + "text": "您确定要清除日志文件?" } ] \ No newline at end of file diff --git a/lang/en/dlg.json b/lang/en/dlg.json index e7b3be81..ae086159 100644 --- a/lang/en/dlg.json +++ b/lang/en/dlg.json @@ -666,5 +666,21 @@ { "id": "IDS_DLG_DECODER_IF_TRANS", "text": "Translate param names" + }, + { + "id": "IDS_DLG_LOG_PATH", + "text": "File Path" + }, + { + "id": "IDS_DLG_OPEN", + "text": "Open" + }, + { + "id": "IDS_DLG_CLEARE", + "text": "Clear" + }, + { + "id": "IDS_DLG_APPEND_MODE", + "text": "Append mode" } ] \ No newline at end of file diff --git a/lang/en/msg.json b/lang/en/msg.json index bcc6b084..be122302 100644 --- a/lang/en/msg.json +++ b/lang/en/msg.json @@ -358,5 +358,13 @@ { "id": "IDS_MSG_NO_ENABLED_CHANNEL", "text": "No channels enabled!" + }, + { + "id": "IDS_MSG_FILE_NOT_EXIST", + "text": "File does not exist!" + }, + { + "id": "IDS_MSG_TO_CLEAR_LOG", + "text": "Are you sure to clear log file?" } ] \ No newline at end of file