mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Merge pull request #5580 from akva2/add_terminal_tu
added: translation unit for terminal manipulation stuff
This commit is contained in:
commit
ec3081baab
@ -65,6 +65,7 @@ list (APPEND MAIN_SOURCE_FILES
|
||||
opm/models/parallel/tasklets.cpp
|
||||
opm/models/parallel/threadmanager.cpp
|
||||
opm/models/utils/parametersystem.cpp
|
||||
opm/models/utils/terminal.cpp
|
||||
opm/models/utils/timer.cpp
|
||||
opm/simulators/flow/ActionHandler.cpp
|
||||
opm/simulators/flow/Banners.cpp
|
||||
@ -722,6 +723,7 @@ list (APPEND PUBLIC_HEADER_FILES
|
||||
opm/models/utils/signum.hh
|
||||
opm/models/utils/simulator.hh
|
||||
opm/models/utils/start.hh
|
||||
opm/models/utils/terminal.hpp
|
||||
opm/models/utils/timer.hpp
|
||||
opm/models/utils/timerguard.hh
|
||||
opm/simulators/flow/ActionHandler.hpp
|
||||
|
@ -35,6 +35,8 @@
|
||||
|
||||
#include <dune/common/parametertree.hh>
|
||||
|
||||
#include <opm/models/utils/terminal.hpp>
|
||||
|
||||
#if HAVE_QUAD
|
||||
#include <opm/material/common/quad.hpp>
|
||||
#endif
|
||||
@ -212,7 +214,7 @@ void printParamUsage(std::ostream& os,
|
||||
{
|
||||
std::string paramMessage, paramType, paramDescription;
|
||||
|
||||
int ttyWidth = Opm::Parameters::getTtyWidth();
|
||||
int ttyWidth = Opm::getTtyWidth();
|
||||
|
||||
// convert the CamelCase name to a command line --parameter-name.
|
||||
std::string cmdLineName = "-";
|
||||
@ -295,7 +297,7 @@ void printParamUsage(std::ostream& os,
|
||||
}
|
||||
}
|
||||
|
||||
paramMessage = Opm::Parameters::breakLines(paramMessage, /*indent=*/52, ttyWidth);
|
||||
paramMessage = Opm::breakLines(paramMessage, /*indent=*/52, ttyWidth);
|
||||
paramMessage += "\n";
|
||||
|
||||
// print everything
|
||||
@ -479,57 +481,6 @@ void SetDefault_(const std::string& paramName,
|
||||
|
||||
} // namespace detail
|
||||
|
||||
std::string breakLines(const std::string& msg,
|
||||
int indentWidth,
|
||||
int maxWidth)
|
||||
{
|
||||
std::string result;
|
||||
int startInPos = 0;
|
||||
int inPos = 0;
|
||||
int lastBreakPos = 0;
|
||||
int ttyPos = 0;
|
||||
for (; inPos < int(msg.size()); ++ inPos, ++ ttyPos) {
|
||||
if (msg[inPos] == '\n') {
|
||||
result += msg.substr(startInPos, inPos - startInPos + 1);
|
||||
startInPos = inPos + 1;
|
||||
lastBreakPos = startInPos + 1;
|
||||
|
||||
// we need to use -1 here because ttyPos is incremented after the loop body
|
||||
ttyPos = -1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (std::isspace(msg[inPos])) {
|
||||
lastBreakPos = inPos;
|
||||
}
|
||||
|
||||
if (ttyPos >= maxWidth) {
|
||||
if (lastBreakPos > startInPos) {
|
||||
result += msg.substr(startInPos, lastBreakPos - startInPos);
|
||||
startInPos = lastBreakPos + 1;
|
||||
lastBreakPos = startInPos;
|
||||
inPos = startInPos;
|
||||
}
|
||||
else {
|
||||
result += msg.substr(startInPos, inPos - startInPos);
|
||||
startInPos = inPos;
|
||||
lastBreakPos = startInPos;
|
||||
inPos = startInPos;
|
||||
}
|
||||
|
||||
result += "\n";
|
||||
for (int i = 0; i < indentWidth; ++i) {
|
||||
result += " ";
|
||||
}
|
||||
ttyPos = indentWidth;
|
||||
}
|
||||
}
|
||||
|
||||
result += msg.substr(startInPos);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
MetaData::clear();
|
||||
@ -855,24 +806,6 @@ bool printUnused(std::ostream& os)
|
||||
return false;
|
||||
}
|
||||
|
||||
int getTtyWidth()
|
||||
{
|
||||
int ttyWidth = 10*1000; // effectively do not break lines at all.
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
#if defined TIOCGWINSZ
|
||||
// This is a bit too linux specific, IMO. let's do it anyway
|
||||
struct winsize ttySize;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ttySize);
|
||||
ttyWidth = std::max<int>(80, ttySize.ws_col);
|
||||
#else
|
||||
// default for systems that do not implement the TIOCGWINSZ ioctl
|
||||
ttyWidth = 100;
|
||||
#endif
|
||||
}
|
||||
|
||||
return ttyWidth;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
template bool Get_(const std::string&, bool, bool);
|
||||
|
@ -93,12 +93,6 @@ void SetDefault_(const std::string& paramName,
|
||||
|
||||
}
|
||||
|
||||
std::string breakLines(const std::string& msg,
|
||||
int indentWidth,
|
||||
int maxWidth);
|
||||
|
||||
int getTtyWidth();
|
||||
|
||||
//! \endcond
|
||||
|
||||
/*!
|
||||
|
@ -33,17 +33,11 @@
|
||||
#include <opm/material/densead/Evaluation.hpp>
|
||||
|
||||
#include <opm/models/utils/parametersystem.hpp>
|
||||
|
||||
#include <opm/models/utils/terminal.hpp>
|
||||
#include <opm/models/utils/simulator.hh>
|
||||
#include <opm/models/utils/timer.hpp>
|
||||
|
||||
#include <opm/material/common/Valgrind.hpp>
|
||||
|
||||
#include <opm/material/common/ResetLocale.hpp>
|
||||
|
||||
#include <dune/grid/io/file/dgfparser/dgfparser.hh>
|
||||
#include <dune/common/version.hh>
|
||||
#include <dune/common/parametertreeparser.hh>
|
||||
#include <dune/common/parallel/mpihelper.hh>
|
||||
|
||||
#if HAVE_DUNE_FEM
|
||||
@ -54,13 +48,10 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <locale>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
#if HAVE_MPI
|
||||
#include <mpi.h>
|
||||
@ -202,65 +193,6 @@ static inline int setupParameters_(int argc,
|
||||
return /*status=*/0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the current TTY to a usable state if the program was aborted.
|
||||
*
|
||||
* This is intended to be called as part of a generic exception handler
|
||||
*/
|
||||
static inline void resetTerminal_()
|
||||
{
|
||||
// make sure stderr and stderr do not contain any unwritten data and make sure that
|
||||
// the TTY does not see any unfinished ANSI escape sequence.
|
||||
std::cerr << " \r\n";
|
||||
std::cerr.flush();
|
||||
std::cout << " \r\n";
|
||||
std::cout.flush();
|
||||
|
||||
// it seems like some terminals sometimes takes their time to react, so let's
|
||||
// accommodate them.
|
||||
usleep(/*usec=*/500*1000);
|
||||
|
||||
// this requires the 'stty' command to be available in the command search path. on
|
||||
// most linux systems, is the case. (but even if the system() function fails, the
|
||||
// worst thing which can happen is that the TTY stays potentially choked up...)
|
||||
if (system("stty sane") != 0)
|
||||
std::cout << "Executing the 'stty' command failed."
|
||||
<< " Terminal might be left in an undefined state!\n";
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the current TTY to a usable state if the program was interrupted by
|
||||
* SIGABRT or SIGINT.
|
||||
*/
|
||||
static inline void resetTerminal_(int signum)
|
||||
{
|
||||
// first thing to do when a nuke hits: restore the default signal handler
|
||||
signal(signum, SIG_DFL);
|
||||
|
||||
#if HAVE_MPI
|
||||
int rank = 0;
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
if (rank != 0) {
|
||||
// re-raise the signal
|
||||
raise(signum);
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (isatty(fileno(stdout)) && isatty(fileno(stdin))) {
|
||||
std::cout << "\n\nReceived signal " << signum
|
||||
<< " (\"" << strsignal(signum) << "\")."
|
||||
<< " Trying to reset the terminal.\n";
|
||||
|
||||
resetTerminal_();
|
||||
}
|
||||
|
||||
// after we did our best to clean the pedestrian way, re-raise the signal
|
||||
raise(signum);
|
||||
}
|
||||
//! \endcond
|
||||
|
||||
/*!
|
||||
* \ingroup Common
|
||||
*
|
||||
@ -283,13 +215,13 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
// set the signal handlers to reset the TTY to a well defined state on unexpected
|
||||
// program aborts
|
||||
if (isatty(STDIN_FILENO)) {
|
||||
signal(SIGINT, resetTerminal_);
|
||||
signal(SIGHUP, resetTerminal_);
|
||||
signal(SIGABRT, resetTerminal_);
|
||||
signal(SIGFPE, resetTerminal_);
|
||||
signal(SIGSEGV, resetTerminal_);
|
||||
signal(SIGPIPE, resetTerminal_);
|
||||
signal(SIGTERM, resetTerminal_);
|
||||
signal(SIGINT, resetTerminal);
|
||||
signal(SIGHUP, resetTerminal);
|
||||
signal(SIGABRT, resetTerminal);
|
||||
signal(SIGFPE, resetTerminal);
|
||||
signal(SIGSEGV, resetTerminal);
|
||||
signal(SIGPIPE, resetTerminal);
|
||||
signal(SIGTERM, resetTerminal);
|
||||
}
|
||||
|
||||
resetLocale();
|
||||
@ -341,9 +273,9 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
#endif
|
||||
const std::string briefDescription = Problem::briefDescription();
|
||||
if (!briefDescription.empty()) {
|
||||
std::string tmp = Parameters::breakLines(briefDescription,
|
||||
/*indentWidth=*/0,
|
||||
Parameters::getTtyWidth());
|
||||
std::string tmp = breakLines(briefDescription,
|
||||
/*indentWidth=*/0,
|
||||
getTtyWidth());
|
||||
std::cout << tmp << std::endl << std::endl;
|
||||
}
|
||||
else
|
||||
@ -392,7 +324,7 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
std::cout << e.what() << ". Abort!\n" << std::flush;
|
||||
|
||||
std::cout << "Trying to reset TTY.\n";
|
||||
resetTerminal_();
|
||||
resetTerminal();
|
||||
}
|
||||
|
||||
return 1;
|
||||
@ -403,7 +335,7 @@ static inline int start(int argc, char **argv, bool registerParams=true)
|
||||
std::cout << "Unknown exception thrown!\n" << std::flush;
|
||||
|
||||
std::cout << "Trying to reset TTY.\n";
|
||||
resetTerminal_();
|
||||
resetTerminal();
|
||||
}
|
||||
|
||||
return 3;
|
||||
|
157
opm/models/utils/terminal.cpp
Normal file
157
opm/models/utils/terminal.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
// vi: set et ts=4 sw=4 sts=4:
|
||||
/*
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Consult the COPYING file in the top-level source directory of this
|
||||
module for the precise wording of the license and the list of
|
||||
copyright holders.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
#include <opm/models/utils/terminal.hpp>
|
||||
|
||||
#if HAVE_MPI
|
||||
#include <mpi.h>
|
||||
#endif
|
||||
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
std::string breakLines(const std::string& msg,
|
||||
int indentWidth,
|
||||
int maxWidth)
|
||||
{
|
||||
std::string result;
|
||||
int startInPos = 0;
|
||||
int inPos = 0;
|
||||
int lastBreakPos = 0;
|
||||
int ttyPos = 0;
|
||||
for (; inPos < int(msg.size()); ++ inPos, ++ ttyPos) {
|
||||
if (msg[inPos] == '\n') {
|
||||
result += msg.substr(startInPos, inPos - startInPos + 1);
|
||||
startInPos = inPos + 1;
|
||||
lastBreakPos = startInPos + 1;
|
||||
|
||||
// we need to use -1 here because ttyPos is incremented after the loop body
|
||||
ttyPos = -1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (std::isspace(msg[inPos])) {
|
||||
lastBreakPos = inPos;
|
||||
}
|
||||
|
||||
if (ttyPos >= maxWidth) {
|
||||
if (lastBreakPos > startInPos) {
|
||||
result += msg.substr(startInPos, lastBreakPos - startInPos);
|
||||
startInPos = lastBreakPos + 1;
|
||||
lastBreakPos = startInPos;
|
||||
inPos = startInPos;
|
||||
}
|
||||
else {
|
||||
result += msg.substr(startInPos, inPos - startInPos);
|
||||
startInPos = inPos;
|
||||
lastBreakPos = startInPos;
|
||||
inPos = startInPos;
|
||||
}
|
||||
|
||||
result += "\n";
|
||||
for (int i = 0; i < indentWidth; ++i) {
|
||||
result += " ";
|
||||
}
|
||||
ttyPos = indentWidth;
|
||||
}
|
||||
}
|
||||
|
||||
result += msg.substr(startInPos);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int getTtyWidth()
|
||||
{
|
||||
int ttyWidth = 10*1000; // effectively do not break lines at all.
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
#if defined TIOCGWINSZ
|
||||
// This is a bit too linux specific, IMO. let's do it anyway
|
||||
struct winsize ttySize;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ttySize);
|
||||
ttyWidth = std::max<int>(80, ttySize.ws_col);
|
||||
#else
|
||||
// default for systems that do not implement the TIOCGWINSZ ioctl
|
||||
ttyWidth = 100;
|
||||
#endif
|
||||
}
|
||||
|
||||
return ttyWidth;
|
||||
}
|
||||
|
||||
void resetTerminal()
|
||||
{
|
||||
// make sure stderr and stderr do not contain any unwritten data and make sure that
|
||||
// the TTY does not see any unfinished ANSI escape sequence.
|
||||
std::cerr << " \r\n";
|
||||
std::cerr.flush();
|
||||
std::cout << " \r\n";
|
||||
std::cout.flush();
|
||||
|
||||
// it seems like some terminals sometimes takes their time to react, so let's
|
||||
// accommodate them.
|
||||
usleep(/*usec=*/500*1000);
|
||||
|
||||
// this requires the 'stty' command to be available in the command search path. on
|
||||
// most linux systems, is the case. (but even if the system() function fails, the
|
||||
// worst thing which can happen is that the TTY stays potentially choked up...)
|
||||
if (system("stty sane") != 0) {
|
||||
std::cout << "Executing the 'stty' command failed."
|
||||
<< " Terminal might be left in an undefined state!\n";
|
||||
}
|
||||
}
|
||||
|
||||
void resetTerminal(int signum)
|
||||
{
|
||||
// first thing to do when a nuke hits: restore the default signal handler
|
||||
signal(signum, SIG_DFL);
|
||||
|
||||
#if HAVE_MPI
|
||||
int rank = 0;
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
if (rank != 0) {
|
||||
// re-raise the signal
|
||||
raise(signum);
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (isatty(fileno(stdout)) && isatty(fileno(stdin))) {
|
||||
std::cout << "\n\nReceived signal " << signum
|
||||
<< " (\"" << strsignal(signum) << "\")."
|
||||
<< " Trying to reset the terminal.\n";
|
||||
|
||||
resetTerminal();
|
||||
}
|
||||
|
||||
// after we did our best to clean the pedestrian way, re-raise the signal
|
||||
raise(signum);
|
||||
}
|
||||
|
||||
} // namespace Opm
|
62
opm/models/utils/terminal.hpp
Normal file
62
opm/models/utils/terminal.hpp
Normal file
@ -0,0 +1,62 @@
|
||||
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
// vi: set et ts=4 sw=4 sts=4:
|
||||
/*
|
||||
This file is part of the Open Porous Media project (OPM).
|
||||
|
||||
OPM is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OPM is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Consult the COPYING file in the top-level source directory of this
|
||||
module for the precise wording of the license and the list of
|
||||
copyright holders.
|
||||
*/
|
||||
#ifndef OPM_TERMINAL_HPP
|
||||
#define OPM_TERMINAL_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
/*!
|
||||
* \brief Break up a string in lines suitable for terminal output.
|
||||
* \param msg String to print
|
||||
* \param indentWidth Size of indent
|
||||
* \param maxWidth Maximum with of terminal
|
||||
* \return
|
||||
*/
|
||||
std::string breakLines(const std::string& msg,
|
||||
int indentWidth,
|
||||
int maxWidth);
|
||||
|
||||
/*!
|
||||
* \brief Get the width of the tty we are attached to.
|
||||
* \return Width of tty
|
||||
*/
|
||||
int getTtyWidth();
|
||||
|
||||
/*!
|
||||
* \brief Resets the current TTY to a usable state if the program was aborted.
|
||||
*
|
||||
* This is intended to be called as part of a generic exception handler
|
||||
*/
|
||||
void resetTerminal();
|
||||
|
||||
/*!
|
||||
* \brief Resets the current TTY to a usable state if the program was interrupted by
|
||||
* SIGABRT or SIGINT.
|
||||
*/
|
||||
void resetTerminal(int signum);
|
||||
|
||||
} // namespace Opm
|
||||
|
||||
#endif // OPM_TERMINAL_HPP
|
Loading…
Reference in New Issue
Block a user