diff --git a/tools/lammps-gui/CMakeLists.txt b/tools/lammps-gui/CMakeLists.txt index c47cb03ce5..f2f56a6c07 100644 --- a/tools/lammps-gui/CMakeLists.txt +++ b/tools/lammps-gui/CMakeLists.txt @@ -61,6 +61,7 @@ set(PROJECT_SOURCES lammpsgui.cpp lammpsgui.h lammpsgui.ui + stdcapture.cpp ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/tools/lammps-gui/lammpsgui.cpp b/tools/lammps-gui/lammpsgui.cpp index a4970dc5e5..ffbfc73bb6 100644 --- a/tools/lammps-gui/lammpsgui.cpp +++ b/tools/lammps-gui/lammpsgui.cpp @@ -13,12 +13,15 @@ #include "lammpsgui.h" #include "highlighter.h" +#include "stdcapture.h" #include "ui_lammpsgui.h" #include #include #include #include +#include +#include #include #include @@ -30,12 +33,14 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) : ui->setupUi(this); this->setCentralWidget(ui->textEdit); current_file.clear(); + capturer = new StdCapture; QFont text_font; text_font.setFamily("Consolas"); text_font.setFixedPitch(true); text_font.setStyleHint(QFont::TypeWriter); ui->textEdit->document()->setDefaultFont(text_font); + ui->textEdit->setMinimumSize(800, 600); highlighter = new Highlighter(ui->textEdit->document()); connect(ui->actionNew, &QAction::triggered, this, &LammpsGui::new_document); @@ -68,6 +73,7 @@ LammpsGui::~LammpsGui() { delete ui; delete highlighter; + delete capturer; } void LammpsGui::new_document() @@ -187,15 +193,34 @@ void LammpsGui::run_buffer() start_lammps(); if (!lammps_handle) return; clear(); + capturer->BeginCapture(); std::string buffer = ui->textEdit->toPlainText().toStdString(); lammps_commands_string(lammps_handle, buffer.c_str()); + capturer->EndCapture(); + auto log = capturer->GetCapture(); + auto box = new QPlainTextEdit(); + box->document()->setPlainText(log.c_str()); + box->setReadOnly(true); + + QFont text_font; + text_font.setFamily("Consolas"); + text_font.setFixedPitch(true); + text_font.setStyleHint(QFont::TypeWriter); + box->document()->setDefaultFont(text_font); + box->setLineWrapMode(QPlainTextEdit::NoWrap); + box->setMinimumSize(800, 600); + QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), box); +// QObject::connect(shortcut, &QShortcut::activated, box, &QPlainTextEdit::close()); + + box->show(); if (lammps_has_error(lammps_handle)) { constexpr int BUFLEN = 1024; char errorbuf[BUFLEN]; lammps_get_last_error_message(lammps_handle, errorbuf, BUFLEN); - QMessageBox::warning(this, "LAMMPS-GUI Error", QString("Error running LAMMPS:\n\n") + errorbuf); + QMessageBox::warning(this, "LAMMPS-GUI Error", + QString("Error running LAMMPS:\n\n") + errorbuf); } } @@ -212,7 +237,8 @@ void LammpsGui::about() start_lammps(); std::string version = "This is LAMMPS-GUI version 0.1\n"; - if (lammps_handle) version += "using LAMMPS Version " + std::to_string(lammps_version(lammps_handle)); + if (lammps_handle) + version += "using LAMMPS Version " + std::to_string(lammps_version(lammps_handle)); QMessageBox::information(this, "About LAMMPS-GUI", version.c_str()); } diff --git a/tools/lammps-gui/lammpsgui.h b/tools/lammps-gui/lammpsgui.h index 965c2d2636..3ee82b97a8 100644 --- a/tools/lammps-gui/lammpsgui.h +++ b/tools/lammps-gui/lammpsgui.h @@ -26,6 +26,7 @@ class LammpsGui; QT_END_NAMESPACE class Highlighter; +class StdCapture; class LammpsGui : public QMainWindow { Q_OBJECT @@ -57,6 +58,7 @@ private slots: private: Ui::LammpsGui *ui; Highlighter *highlighter; + StdCapture *capturer; QString current_file; QString current_dir; diff --git a/tools/lammps-gui/stdcapture.cpp b/tools/lammps-gui/stdcapture.cpp new file mode 100644 index 0000000000..4932ef3d27 --- /dev/null +++ b/tools/lammps-gui/stdcapture.cpp @@ -0,0 +1,119 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/, Sandia National Laboratories + LAMMPS development team: developers@lammps.org + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. + ------------------------------------------------------------------------- */ + +// adapted from: https://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string + +#include "stdcapture.h" + +#ifdef _MSC_VER +#include +#define popen _popen +#define pclose _pclose +#define stat _stat +#define dup _dup +#define dup2 _dup2 +#define fileno _fileno +#define close _close +#define pipe _pipe +#define read _read +#define eof _eof +#else +#include +#endif + +#include +#include +#include +#include + +StdCapture::StdCapture() : m_capturing(false), m_oldStdOut(0) +{ + // make stdout unbuffered so that we don't need to flush the stream + setvbuf(stdout, NULL, _IONBF, 0); + + m_pipe[READ] = 0; + m_pipe[WRITE] = 0; +#if _MSC_VER + if (pipe(m_pipe, 65536, O_BINARY) == -1) return; +#else + if (pipe(m_pipe) == -1) return; +#endif + m_oldStdOut = dup(fileno(stdout)); + if (m_oldStdOut == -1) return; +} + +StdCapture::~StdCapture() +{ + if (m_capturing) { + EndCapture(); + } + if (m_oldStdOut > 0) close(m_oldStdOut); + if (m_pipe[READ] > 0) close(m_pipe[READ]); + if (m_pipe[WRITE] > 0) close(m_pipe[WRITE]); +} + +void StdCapture::BeginCapture() +{ + if (m_capturing) EndCapture(); + dup2(m_pipe[WRITE], fileno(stdout)); + m_capturing = true; +} + +bool StdCapture::EndCapture() +{ + if (!m_capturing) return false; + dup2(m_oldStdOut, fileno(stdout)); + m_captured.clear(); + + constexpr int bufSize = 1025; + char buf[bufSize]; + int bytesRead; + bool fd_blocked; + + do { + bytesRead = 0; + fd_blocked = false; + +#ifdef _MSC_VER + if (!eof(m_pipe[READ])) { + bytesRead = read(m_pipe[READ], buf, bufSize - 1); + } +#else + bytesRead = read(m_pipe[READ], buf, bufSize - 1); +#endif + if (bytesRead > 0) { + buf[bytesRead] = 0; + m_captured += buf; + } else if (bytesRead < 0) { + fd_blocked = ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINTR)); + + if (fd_blocked) std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } while (fd_blocked || (bytesRead == (bufSize - 1))); + m_capturing = false; + return true; +} + +std::string StdCapture::GetCapture() const +{ + std::string::size_type idx = m_captured.find_last_not_of("\r\n"); + if (idx == std::string::npos) { + return m_captured; + } else { + return m_captured.substr(0, idx + 1); + } +} + +// Local Variables: +// c-basic-offset: 4 +// End: diff --git a/tools/lammps-gui/stdcapture.h b/tools/lammps-gui/stdcapture.h new file mode 100644 index 0000000000..2afd494a36 --- /dev/null +++ b/tools/lammps-gui/stdcapture.h @@ -0,0 +1,40 @@ +/* -*- c++ -*- ---------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/, Sandia National Laboratories + LAMMPS development team: developers@lammps.org + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#ifndef STDCAPTURE_H +#define STDCAPTURE_H + +#include + +class StdCapture { +public: + StdCapture(); + virtual ~StdCapture(); + + void BeginCapture(); + bool EndCapture(); + std::string GetCapture() const; + +private: + enum PIPES { READ, WRITE }; + int m_pipe[2]; + int m_oldStdOut; + bool m_capturing; + bool m_init; + std::string m_captured; +}; + +#endif +// Local Variables: +// c-basic-offset: 4 +// End: