From 2492c0867d9f12781e5fe06a6e78d829d4ed90ed Mon Sep 17 00:00:00 2001 From: Arne Morten Kvarving Date: Thu, 5 Sep 2024 11:07:18 +0200 Subject: [PATCH] move resetTerminal functions from start.hh to terminal.cpp --- opm/models/utils/start.hh | 80 ++++------------------------------- opm/models/utils/terminal.cpp | 56 ++++++++++++++++++++++++ opm/models/utils/terminal.hpp | 13 ++++++ 3 files changed, 78 insertions(+), 71 deletions(-) diff --git a/opm/models/utils/start.hh b/opm/models/utils/start.hh index b1a7ca8c1..13dc059a9 100644 --- a/opm/models/utils/start.hh +++ b/opm/models/utils/start.hh @@ -54,13 +54,10 @@ #include #include #include -#include #include #include -#include #include -#include #if HAVE_MPI #include @@ -202,65 +199,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 +221,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(); @@ -392,7 +330,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 +341,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; diff --git a/opm/models/utils/terminal.cpp b/opm/models/utils/terminal.cpp index aa0b1d9bf..15d94a061 100644 --- a/opm/models/utils/terminal.cpp +++ b/opm/models/utils/terminal.cpp @@ -24,6 +24,12 @@ #include #include +#if HAVE_MPI +#include +#endif + +#include +#include #include #include @@ -98,4 +104,54 @@ int getTtyWidth() 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 diff --git a/opm/models/utils/terminal.hpp b/opm/models/utils/terminal.hpp index 5ec5e2270..512f3f4a1 100644 --- a/opm/models/utils/terminal.hpp +++ b/opm/models/utils/terminal.hpp @@ -44,6 +44,19 @@ std::string breakLines(const std::string& msg, */ 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