From a3cc839d4298c13683c12aab5d49bc1df1c008d3 Mon Sep 17 00:00:00 2001 From: Torusrxxx Date: Sun, 17 Jul 2016 10:52:29 +0000 Subject: [PATCH] Log redirection (#849) * Log redirection * invalid handle is INVALID_HANDLE_VALUE not NULL * Use cstdio * fix issue with Unicode file name * update icons * update error message --- src/gui/Src/Gui/CPUMultiDump.cpp | 2 +- src/gui/Src/Gui/LogView.cpp | 50 ++++++++++++++++++++++++++++++- src/gui/Src/Gui/LogView.h | 6 ++++ src/gui/Src/Gui/MainWindow.ui | 2 +- src/gui/images/animal-dog.png | Bin 0 -> 1701 bytes src/gui/resource.qrc | 1 + 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/gui/images/animal-dog.png diff --git a/src/gui/Src/Gui/CPUMultiDump.cpp b/src/gui/Src/Gui/CPUMultiDump.cpp index 0a6ba193..ab4deab9 100644 --- a/src/gui/Src/Gui/CPUMultiDump.cpp +++ b/src/gui/Src/Gui/CPUMultiDump.cpp @@ -23,7 +23,7 @@ CPUMultiDump::CPUMultiDump(CPUDisassembly* disas, int nbCpuDumpTabs, QWidget* pa mWatch = new WatchView(this); //mMaxCPUDumpTabs++; - this->addTabEx(mWatch, DIcon("geolocation-goto.png"), tr("Watch ") + QString::number(1), QString("Watch 1")); + this->addTabEx(mWatch, DIcon("animal-dog.png"), tr("Watch ") + QString::number(1), QString("Watch 1")); mWatch->loadColumnFromConfig("Watch1"); connect(this, SIGNAL(currentChanged(int)), this, SLOT(updateCurrentTabSlot(int))); diff --git a/src/gui/Src/Gui/LogView.cpp b/src/gui/Src/Gui/LogView.cpp index 69f06fad..beb99ebd 100644 --- a/src/gui/Src/Gui/LogView.cpp +++ b/src/gui/Src/Gui/LogView.cpp @@ -1,8 +1,9 @@ #include "LogView.h" #include "Configuration.h" #include "Bridge.h" +#include "BrowseDialog.h" -LogView::LogView(QWidget* parent) : QTextEdit(parent) +LogView::LogView(QWidget* parent) : QTextEdit(parent), logRedirection(NULL) { updateStyle(); this->setUndoRedoEnabled(false); @@ -17,6 +18,13 @@ LogView::LogView(QWidget* parent) : QTextEdit(parent) setupContextMenu(); } +LogView::~LogView() +{ + if(logRedirection != NULL) + fclose(logRedirection); + logRedirection = NULL; +} + void LogView::updateStyle() { setFont(ConfigFont("Log")); @@ -45,6 +53,9 @@ void LogView::setupContextMenu() actionToggleLogging->setShortcutContext(Qt::WidgetShortcut); connect(actionToggleLogging, SIGNAL(triggered()), this, SLOT(toggleLoggingSlot())); this->addAction(actionToggleLogging); + actionRedirectLog = new QAction(tr("&Redirect Log..."), this); + connect(actionRedirectLog, SIGNAL(triggered()), this, SLOT(redirectLogSlot())); + this->addAction(actionRedirectLog); refreshShortcutsSlot(); connect(Config(), SIGNAL(shortcutsUpdated()), this, SLOT(refreshShortcutsSlot())); @@ -68,12 +79,26 @@ void LogView::contextMenuEvent(QContextMenuEvent* event) else actionToggleLogging->setText(tr("Enable &Logging")); wMenu.addAction(actionToggleLogging); + wMenu.addAction(actionRedirectLog); wMenu.exec(event->globalPos()); } void LogView::addMsgToLogSlot(QString msg) { + // fix Unix-style line endings. + msg.replace(QString("\r\n"), QString("\n")); + msg.replace(QChar('\n'), QString("\r\n")); + // redirect the log + if(logRedirection != NULL) + { + if(!fwrite(msg.data_ptr()->data(), msg.size() * 2, 1, logRedirection)) + { + fclose(logRedirection); + logRedirection = NULL; + msg += tr("fwrite() failed (GetLastError()= %1 ). Log redirection stopped.\r\n").arg(GetLastError()); + } + } if(!loggingEnabled) return; if(this->document()->characterCount() > 10000 * 100) //limit the log to ~100mb @@ -87,6 +112,29 @@ void LogView::clearLogSlot() this->clear(); } +void LogView::redirectLogSlot() +{ + if(logRedirection != NULL) + fclose(logRedirection); + logRedirection = NULL; + BrowseDialog browse(this, tr("Redirect log to file"), tr("Enter the file to which you want to redirect log messages."), tr("Log files(*.txt);;All files(*.*)"), QCoreApplication::applicationDirPath(), true); + if(browse.exec() == QDialog::Accepted) + { + logRedirection = _wfopen(browse.path.toStdWString().c_str(), L"ab"); + if(logRedirection == NULL) + addMsgToLogSlot(tr("_wfopen() failed. Log will not be redirected to %1.\n").arg(browse.path)); + else + { + if(ftell(logRedirection) == 0) + { + unsigned short BOM = 0xfeff; + fwrite(&BOM, 2, 1, logRedirection); + } + addMsgToLogSlot(tr("Log will be redirected to %1.\n").arg(browse.path)); + } + } +} + void LogView::setLoggingEnabled(bool enabled) { loggingEnabled = enabled; diff --git a/src/gui/Src/Gui/LogView.h b/src/gui/Src/Gui/LogView.h index ab28a4f9..007c86d2 100644 --- a/src/gui/Src/Gui/LogView.h +++ b/src/gui/Src/Gui/LogView.h @@ -2,12 +2,14 @@ #define LOGVIEW_H #include +#include class LogView : public QTextEdit { Q_OBJECT public: explicit LogView(QWidget* parent = 0); + ~LogView(); void setupContextMenu(); void contextMenuEvent(QContextMenuEvent* event); @@ -15,6 +17,7 @@ public slots: void refreshShortcutsSlot(); void updateStyle(); void addMsgToLogSlot(QString msg); + void redirectLogSlot(); void setLoggingEnabled(bool enabled); bool getLoggingEnabled(); @@ -29,6 +32,9 @@ private: QAction* actionClear; QAction* actionSave; QAction* actionToggleLogging; + QAction* actionRedirectLog; + + FILE* logRedirection; }; #endif // LOGVIEW_H diff --git a/src/gui/Src/Gui/MainWindow.ui b/src/gui/Src/Gui/MainWindow.ui index 44af5af0..1b78885d 100644 --- a/src/gui/Src/Gui/MainWindow.ui +++ b/src/gui/Src/Gui/MainWindow.ui @@ -894,7 +894,7 @@ - :/icons/images/recentfiles.png:/icons/images/recentfiles.png + :/icons/images/runtouser.png:/icons/images/runtouser.png Run to &user code diff --git a/src/gui/images/animal-dog.png b/src/gui/images/animal-dog.png new file mode 100644 index 0000000000000000000000000000000000000000..01457548153b5b0514eec3f7cf71ba9ece961f7c GIT binary patch literal 1701 zcmaJ?e^AqA7_K^*B2Lsf2gf89LELDY^mhXl+lG#+#R_QA-8MrTXa`M7n%aV>!wm-F z55Pl)I|Wg2Ls4`n6E__KW1@C|o+v5^ZUyH=fvKA(b14Jl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images/runtouser.png images/undo.png images/binary_save.png + images/animal-dog.png