diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d164bbc..8903837f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,13 @@ en-US: - Fix the main window error raise - Add the ability to add custom data to the log +- Improve the log file path setting option zh-CN: - 修复主窗口错误的提升 - 增加日志中允许添加自定义数据功能 +- 改进记录日志文件路径设置选项 ## [[V0.5.1](https://github.com/QQxiaoming/quardCRT/releases/tag/V0.5.1)] - 2024-09-26 diff --git a/docs/changelog.md b/docs/changelog.md index 3dbdb5df..e13f1edb 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ - Fix the main window error raise - Add the ability to add custom data to the log +- Improve the log file path setting option ## [[V0.5.1](https://github.com/QQxiaoming/quardCRT/releases/tag/V0.5.1)] - 2024-09-26 diff --git a/src/globaloptions/globaloptionslogfilewidget.ui b/src/globaloptions/globaloptionslogfilewidget.ui index b182495c..be01bb13 100644 --- a/src/globaloptions/globaloptionslogfilewidget.ui +++ b/src/globaloptions/globaloptionslogfilewidget.ui @@ -6,30 +6,68 @@ 0 0 - 494 - 428 + 521 + 541 - + - Custom log data + Log file path - - - + + + + + + + + + - On each line + ... - - + + + + ... + + + + + + + 0 + 0 + + + + + + + + ... + + + + + + + + + Custom log data + + + + + @@ -37,6 +75,9 @@ + + + @@ -44,12 +85,19 @@ - - - + + + + On each line + + + + + + diff --git a/src/globaloptions/globaloptionswindow.cpp b/src/globaloptions/globaloptionswindow.cpp index 907c3ab9..83804882 100644 --- a/src/globaloptions/globaloptionswindow.cpp +++ b/src/globaloptions/globaloptionswindow.cpp @@ -136,6 +136,7 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) : model = new GlobalOptionsModel(ui->treeView); model->setOnlyName(true); model->setDistinguishType(false); + ui->treeView->setAnimated(true); ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers); emptyWidget = new QWidget(this); ui->stackedWidget->addWidget(emptyWidget); @@ -228,6 +229,9 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) : globalOptionsTerminalWidget->ui->checkBoxTrimPastedTrailingNewlines->setChecked(settings.value("TrimPastedTrailingNewlines", true).toBool()); globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool()); globalOptionsLogFileWidget->ui->lineEditOnEachLine->setText(settings.value("OnEachLine", "%h:%m:%s-%t:").toString()); + globalOptionsLogFileWidget->ui->lineEditLogFilePath->setText(settings.value("LogFilePath", QDir::homePath() + "/%S-%Y-%M-%D-%h:%m:%s.log").toString()); + globalOptionsLogFileWidget->ui->lineEditRawLogFilePath->setText(settings.value("RawLogFilePath", QDir::homePath() + "/%S-%Y-%M-%D-%h:%m:%s.bin").toString()); + globalOptionsLogFileWidget->ui->lineEditScriptLogFilePath->setText(settings.value("ScriptLogFilePath", QDir::homePath() + "/script-%S-%Y-%M-%D-%h:%m:%s.py").toString()); #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool()); #endif @@ -745,6 +749,18 @@ QString GlobalOptionsWindow::getLogOnEachLine(void){ return globalOptionsLogFileWidget->ui->lineEditOnEachLine->text(); } +QString GlobalOptionsWindow::getLogFilePath(void){ + return globalOptionsLogFileWidget->ui->lineEditLogFilePath->text(); +} + +QString GlobalOptionsWindow::getLogRawFilePath(void){ + return globalOptionsLogFileWidget->ui->lineEditRawLogFilePath->text(); +} + +QString GlobalOptionsWindow::getScriptLogFilePath(void){ + return globalOptionsLogFileWidget->ui->lineEditScriptLogFilePath->text(); +} + #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) bool GlobalOptionsWindow::getEnableCtrlC(void) { @@ -826,6 +842,9 @@ void GlobalOptionsWindow::buttonBoxAccepted(void) settings.setValue("TrimPastedTrailingNewlines", globalOptionsTerminalWidget->ui->checkBoxTrimPastedTrailingNewlines->isChecked()); settings.setValue("Echo", globalOptionsTerminalWidget->ui->checkBoxEcho->isChecked()); settings.setValue("OnEachLine", globalOptionsLogFileWidget->ui->lineEditOnEachLine->text()); + settings.setValue("LogFilePath", globalOptionsLogFileWidget->ui->lineEditLogFilePath->text()); + settings.setValue("RawLogFilePath", globalOptionsLogFileWidget->ui->lineEditRawLogFilePath->text()); + settings.setValue("ScriptLogFilePath", globalOptionsLogFileWidget->ui->lineEditScriptLogFilePath->text()); #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) settings.setValue("EnableCtrlC", globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->isChecked()); #endif @@ -914,6 +933,9 @@ void GlobalOptionsWindow::buttonBoxRejected(void) globalOptionsTerminalWidget->ui->checkBoxTrimPastedTrailingNewlines->setChecked(settings.value("TrimPastedTrailingNewlines", true).toBool()); globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool()); globalOptionsLogFileWidget->ui->lineEditOnEachLine->setText(settings.value("OnEachLine", "%h:%m:%s-%t:").toString()); + globalOptionsLogFileWidget->ui->lineEditLogFilePath->setText(settings.value("LogFilePath", QDir::homePath() + "/%S-%Y-%M-%D-%h:%m:%s.log").toString()); + globalOptionsLogFileWidget->ui->lineEditRawLogFilePath->setText(settings.value("RawLogFilePath", QDir::homePath() + "/%S-%Y-%M-%D-%h:%m:%s.bin").toString()); + globalOptionsLogFileWidget->ui->lineEditScriptLogFilePath->setText(settings.value("ScriptLogFilePath", QDir::homePath() + "/script-%S-%Y-%M-%D-%h:%m:%s.py").toString()); #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool()); #endif diff --git a/src/globaloptions/globaloptionswindow.h b/src/globaloptions/globaloptionswindow.h index cb330194..2c069b38 100644 --- a/src/globaloptions/globaloptionswindow.h +++ b/src/globaloptions/globaloptionswindow.h @@ -120,6 +120,9 @@ class GlobalOptionsWindow : public QDialog bool getEcho(void); QColor getCursorColor(void); QString getLogOnEachLine(void); + QString getLogFilePath(void); + QString getLogRawFilePath(void); + QString getScriptLogFilePath(void); #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) bool getEnableCtrlC(void); #endif diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c83c4698..96050871 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -3814,6 +3814,9 @@ void CentralWidget::setGlobalOptions(SessionsWindow *window) { window->setEcho(globalOptionsWindow->getEcho()); window->setCursorColor(globalOptionsWindow->getCursorColor()); window->setLogOnEachLine(globalOptionsWindow->getLogOnEachLine()); + window->setLogFilePath(globalOptionsWindow->getLogFilePath()); + window->setRawLogFilePath(globalOptionsWindow->getLogRawFilePath()); + window->setScriptLogFilePath(globalOptionsWindow->getScriptLogFilePath()); #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) window->setEnableHandleCtrlC(globalOptionsWindow->getEnableCtrlC()); #endif diff --git a/src/sessionswindow/sessionswindow.cpp b/src/sessionswindow/sessionswindow.cpp index d162d85b..d6bca8cc 100644 --- a/src/sessionswindow/sessionswindow.cpp +++ b/src/sessionswindow/sessionswindow.cpp @@ -46,7 +46,7 @@ #include "misc.h" #include "qextserialenumerator.h" -QString SessionsWindow::saveRecordingPath = QDir::homePath(); +QString SessionsWindow::saveRecordingTempDirPath = QDir::homePath(); SessionsWindow::SessionsWindow(SessionType tp, QWidget *parent) : QObject(parent) @@ -1005,19 +1005,41 @@ void SessionsWindow::setWorkingDirectory(const QString &dir) workingDirectory = dir; } +void SessionsWindow::prepareString(QString &str) { + //%H - hostname + //%S - session name + //%Y - 4-digit year + //%M - 2-digit month + //%D - 2-digit day + //%h - 2-digit hour + //%m - 2-digit minute + //%s - 2-digit second + //%t - 3-digit millisecond + QDateTime now = QDateTime::currentDateTime(); + str.replace("%H", getHostname()); + str.replace("%S", getName()); + str.replace("%Y", now.toString("yyyy")); + str.replace("%M", now.toString("MM")); + str.replace("%D", now.toString("dd")); + str.replace("%h", now.toString("hh")); + str.replace("%m", now.toString("mm")); + str.replace("%s", now.toString("ss")); + str.replace("%t", now.toString("zzz")); +} + int SessionsWindow::setLog(bool enable) { int ret = -1; log_file_mutex.lock(); if(enable) { if(log_file == nullptr) { - QFileInfo path(saveRecordingPath); - if(!path.isDir()) { - saveRecordingPath = QDir::homePath(); + QString savefile_name = logPath; + prepareString(savefile_name); + if(savefile_name.isEmpty()) { + savefile_name = FileDialog::getSaveFileName(term, tr("Save log..."), + saveRecordingTempDirPath + "/" + getName() + QDate::currentDate().toString("-yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".log", tr("log files (*.log)")); } - QString savefile_name = FileDialog::getSaveFileName(term, tr("Save log..."), - saveRecordingPath + QDate::currentDate().toString("/yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".log", tr("log files (*.log)")); if (!savefile_name.isEmpty()) { - saveRecordingPath = QFileInfo(savefile_name).absolutePath(); + saveRecordingTempDirPath = QFileInfo(savefile_name).absolutePath(); log_file = new QFile(savefile_name); if (!log_file->open(QIODevice::WriteOnly|QIODevice::Text)) { QMessageBox::warning(messageParentWidget, tr("Save log"), tr("Cannot write file %1:\n%2.").arg(savefile_name).arg(log_file->errorString())); @@ -1060,14 +1082,14 @@ int SessionsWindow::setRawLog(bool enable) { raw_log_file_mutex.lock(); if(enable) { if(raw_log_file == nullptr) { - QFileInfo path(saveRecordingPath); - if(!path.isDir()) { - saveRecordingPath = QDir::homePath(); + QString savefile_name = rawLogPath; + prepareString(savefile_name); + if(savefile_name.isEmpty()) { + savefile_name = FileDialog::getSaveFileName(term, tr("Save Raw log..."), + saveRecordingTempDirPath + "/" + getName() + QDate::currentDate().toString("-yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".bin", tr("binary files (*.bin)")); } - QString savefile_name = FileDialog::getSaveFileName(term, tr("Save Raw log..."), - saveRecordingPath + QDate::currentDate().toString("/yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".bin", tr("binary files (*.bin)")); if (!savefile_name.isEmpty()) { - saveRecordingPath = QFileInfo(savefile_name).absolutePath(); + saveRecordingTempDirPath = QFileInfo(savefile_name).absolutePath(); raw_log_file = new QFile(savefile_name); if (!raw_log_file->open(QIODevice::WriteOnly)) { QMessageBox::warning(messageParentWidget, tr("Save Raw log"), tr("Cannot write file %1:\n%2.").arg(savefile_name).arg(log_file->errorString())); @@ -1134,14 +1156,14 @@ void SessionsWindow::addToRecordingScript(int type, QByteArray ba) { int SessionsWindow::stopRecordingScript(void) { int ret = -1; - QFileInfo path(saveRecordingPath); - if(!path.isDir()) { - saveRecordingPath = QDir::homePath(); + QString savefile_name = scriptLogPath; + prepareString(savefile_name); + if(savefile_name.isEmpty()) { + savefile_name = FileDialog::getSaveFileName(term, tr("Save script..."), + saveRecordingTempDirPath + "/script-" + getName() + QDate::currentDate().toString("-yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".py", tr("Python files (*.py)")); } - QString savefile_name = FileDialog::getSaveFileName(term, tr("Save script..."), - saveRecordingPath + QDate::currentDate().toString("/script-yyyy-MM-dd-") + QTime::currentTime().toString("hh-mm-ss") + ".py", tr("Python files (*.py)")); if (!savefile_name.isEmpty()) { - saveRecordingPath = QFileInfo(savefile_name).absolutePath(); + saveRecordingTempDirPath = QFileInfo(savefile_name).absolutePath(); QFile scriptFile(savefile_name); if (!scriptFile.open(QIODevice::WriteOnly|QIODevice::Text)) { QMessageBox::warning(messageParentWidget, tr("Save script"), tr("Cannot write file %1:\n%2.").arg(savefile_name).arg(scriptFile.errorString())); @@ -1218,26 +1240,8 @@ int SessionsWindow::saveLog(const char *data, int size) { if(log_file_mutex.tryLock()) { if(log_file != nullptr) { if(!add_time_on_each_line.isEmpty()) { - //%H - hostname - //%S - session name - //%Y - 4-digit year - //%M - 2-digit month - //%D - 2-digit day - //%h - 2-digit hour - //%m - 2-digit minute - //%s - 2-digit second - //%t - 3-digit millisecond - QDateTime now = QDateTime::currentDateTime(); QString lineText = add_time_on_each_line; - lineText.replace("%H", getHostname()); - lineText.replace("%S", getName()); - lineText.replace("%Y", now.toString("yyyy")); - lineText.replace("%M", now.toString("MM")); - lineText.replace("%D", now.toString("dd")); - lineText.replace("%h", now.toString("hh")); - lineText.replace("%m", now.toString("mm")); - lineText.replace("%s", now.toString("ss")); - lineText.replace("%t", now.toString("zzz")); + prepareString(lineText); log_file->write(lineText.toUtf8()); } ret = log_file->write(data, size); diff --git a/src/sessionswindow/sessionswindow.h b/src/sessionswindow/sessionswindow.h index b44a12bd..895a8ccc 100644 --- a/src/sessionswindow/sessionswindow.h +++ b/src/sessionswindow/sessionswindow.h @@ -157,6 +157,9 @@ class SessionsWindow : public QObject int setLog(bool enable); bool isLog(void) { return enableLog; } void setLogOnEachLine(QString onEachLine) {add_time_on_each_line = onEachLine;}; + void setLogFilePath(QString logFilePath) {logPath = logFilePath;}; + void setRawLogFilePath(QString logFilePath) {rawLogPath = logFilePath;}; + void setScriptLogFilePath(QString logFilePath) {scriptLogPath = logFilePath;}; QString getLogFileName(void); int setRawLog(bool enable); bool isRawLog(void) { return enableRawLog; } @@ -456,6 +459,7 @@ class SessionsWindow : public QObject bool doRecvData(QByteArray &data); void addToRecordingScript(int type, QString str); void addToRecordingScript(int type, QByteArray ba); + void prepareString(QString &str); private: SessionType type; @@ -541,7 +545,10 @@ class SessionsWindow : public QObject QString m_username; QString m_password; - static QString saveRecordingPath; + QString logPath; + QString rawLogPath; + QString scriptLogPath; + static QString saveRecordingTempDirPath; }; #endif // SESSIONSWINDOW_H