capture stdout and display in dialog window
This commit is contained in:
@ -61,6 +61,7 @@ set(PROJECT_SOURCES
|
||||
lammpsgui.cpp
|
||||
lammpsgui.h
|
||||
lammpsgui.ui
|
||||
stdcapture.cpp
|
||||
)
|
||||
|
||||
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
|
||||
@ -13,12 +13,15 @@
|
||||
|
||||
#include "lammpsgui.h"
|
||||
#include "highlighter.h"
|
||||
#include "stdcapture.h"
|
||||
#include "ui_lammpsgui.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QFileInfo>
|
||||
#include <QFont>
|
||||
#include <QMessageBox>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QShortcut>
|
||||
#include <QTextStream>
|
||||
#include <string>
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
119
tools/lammps-gui/stdcapture.cpp
Normal file
119
tools/lammps-gui/stdcapture.cpp
Normal file
@ -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 <io.h>
|
||||
#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 <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <fcntl.h>
|
||||
#include <thread>
|
||||
|
||||
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:
|
||||
40
tools/lammps-gui/stdcapture.h
Normal file
40
tools/lammps-gui/stdcapture.h
Normal file
@ -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 <string>
|
||||
|
||||
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:
|
||||
Reference in New Issue
Block a user