Skip to content

Commit

Permalink
Improve the log file path setting option
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Nov 5, 2024
1 parent bda4222 commit 1592d3a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
74 changes: 61 additions & 13 deletions src/globaloptions/globaloptionslogfilewidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,98 @@
<rect>
<x>0</x>
<y>0</y>
<width>494</width>
<height>428</height>
<width>521</width>
<height>541</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_10">
<widget class="QLabel" name="label_15">
<property name="text">
<string>Custom log data</string>
<string>Log file path</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="2" column="0">
<widget class="QLabel" name="label">
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<widget class="QLineEdit" name="lineEditRawLogFilePath"/>
</item>
<item row="3" column="0">
<widget class="QLineEdit" name="lineEditScriptLogFilePath"/>
</item>
<item row="3" column="2">
<widget class="QToolButton" name="toolButtonScriptLogFilePath">
<property name="text">
<string>On each line</string>
<string>...</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditOnEachLine"/>
<item row="1" column="2">
<widget class="QToolButton" name="toolButtonRawLogFilePath">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLineEdit" name="lineEditLogFilePath">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="toolButtonLogFilePath">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>Custom log data</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Upon connect</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditUponConnect"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Upon disconnect</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineEditUponConnect"/>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEditUponDisconnect"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>On each line</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEditOnEachLine"/>
</item>
</layout>
</item>
<item>
Expand Down
22 changes: 22 additions & 0 deletions src/globaloptions/globaloptionswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/globaloptions/globaloptionswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 42 additions & 38 deletions src/sessionswindow/sessionswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/sessionswindow/sessionswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

0 comments on commit 1592d3a

Please sign in to comment.