From b178a66e7f378058e3f2098b16f22bc85288c2d5 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 5 Feb 2019 22:14:08 -0600 Subject: [PATCH] Add implementation of warning() in C++ --- include/openmc/error.h | 6 +---- src/error.cpp | 57 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/include/openmc/error.h b/include/openmc/error.h index b02c0a9f85..9bfebb2ce6 100644 --- a/include/openmc/error.h +++ b/include/openmc/error.h @@ -50,11 +50,7 @@ void fatal_error(const std::stringstream& message) fatal_error(message.str()); } -inline -void warning(const std::string& message) -{ - warning_from_c(message.c_str(), message.length()); -} +void warning(const std::string& message); inline void warning(const std::stringstream& message) diff --git a/src/error.cpp b/src/error.cpp index d17219f31d..fdb42d2698 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -2,13 +2,68 @@ #include "openmc/message_passing.h" +#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) +#include // for isatty +#endif + +#include // for setw +#include + namespace openmc { #ifdef OPENMC_MPI void abort_mpi(int code) { - MPI_Abort(mpi::intracomm, code); + MPI_Abort(mpi::intracomm, code); } #endif +void warning(const std::string& message) +{ +#ifdef _POSIX_VERSION + // Make output yellow if user is in a terminal + if (isatty(STDERR_FILENO)) { + std::cerr << "\033[0;33m"; + } +#endif + + // Write warning at beginning + std::cerr << " WARNING: "; + + // Set line wrapping and indentation + int line_wrap = 80; + int indent = 10; + + // Determine length of message + int length = message.size(); + + int i_start = 0; + int line_len = line_wrap - indent + 1; + while (i_start < length) { + if (length - i_start < line_len) { + // Remainder of message will fit on line + std::cerr << message.substr(i_start) << '\n'; + break; + + } else { + // Determine last space in current line + std::string s = message.substr(i_start, line_len); + auto pos = s.find_last_of(' '); + + // Write up to last space, or whole line if no space is present + std::cerr << s.substr(0, pos) << '\n' << std::setw(indent) << " "; + + // Advance starting position + i_start += (pos == std::string::npos) ? line_len : pos + 1; + } + } + +#ifdef _POSIX_VERSION + // Reset color for terminal + if (isatty(STDERR_FILENO)) { + std::cerr << "\033[0m"; + } +#endif +} + } // namespace openmc