Add implementation of write_message and fatal_error in error.cpp

This commit is contained in:
Paul Romano 2019-02-05 22:53:53 -06:00
parent b178a66e7f
commit 102aa934ae
3 changed files with 64 additions and 65 deletions

View file

@ -9,11 +9,6 @@
namespace openmc {
extern "C" void fatal_error_from_c(const char* message, int message_len);
extern "C" void warning_from_c(const char* message, int message_len);
extern "C" void write_message_from_c(const char* message, int message_len,
int level);
inline void
set_errmsg(const char* message)
{
@ -32,17 +27,7 @@ set_errmsg(const std::stringstream& message)
std::strcpy(openmc_err_msg, message.str().c_str());
}
inline
void fatal_error(const char* message)
{
fatal_error_from_c(message, std::strlen(message));
}
inline
void fatal_error(const std::string& message)
{
fatal_error_from_c(message.c_str(), message.length());
}
[[noreturn]] void fatal_error(const std::string& message, int err=-1);
inline
void fatal_error(const std::stringstream& message)
@ -50,6 +35,12 @@ void fatal_error(const std::stringstream& message)
fatal_error(message.str());
}
inline
void fatal_error(const char* message)
{
fatal_error({message, std::strlen(message)});
}
void warning(const std::string& message);
inline
@ -58,17 +49,7 @@ void warning(const std::stringstream& message)
warning(message.str());
}
inline
void write_message(const char* message, int level)
{
write_message_from_c(message, std::strlen(message), level);
}
inline
void write_message(const std::string& message, int level)
{
write_message_from_c(message.c_str(), message.length(), level);
}
void write_message(const std::string& message, int level);
inline
void write_message(const std::stringstream& message, int level)

View file

@ -111,14 +111,6 @@ contains
end subroutine warning
subroutine warning_from_c(message, message_len) bind(C)
integer(C_INT), intent(in), value :: message_len
character(kind=C_CHAR), intent(in) :: message(message_len)
character(message_len+1) :: message_out
write(message_out, *) message
call warning(message_out)
end subroutine
!===============================================================================
! FATAL_ERROR alerts the user that an error has been encountered and displays a
! message about the particular problem. Errors are considered 'fatal' and hence
@ -208,14 +200,6 @@ contains
end subroutine fatal_error
subroutine fatal_error_from_c(message, message_len) bind(C)
integer(C_INT), intent(in), value :: message_len
character(kind=C_CHAR), intent(in) :: message(message_len)
character(message_len+1) :: message_out
write(message_out, *) message
call fatal_error(message_out)
end subroutine
!===============================================================================
! WRITE_MESSAGE displays an informational message to the log file and the
! standard output stream.
@ -271,14 +255,4 @@ contains
end subroutine write_message
subroutine write_message_from_c(message, message_len, level) bind(C)
integer(C_INT), intent(in), value :: message_len
character(kind=C_CHAR), intent(in) :: message(message_len)
integer(C_INT), intent(in), value :: level
character(message_len+1) :: message_out
! Using * in the internal write adds an extra space at the beginning
write(message_out, *) message
call write_message(message_out(2:), level)
end subroutine write_message_from_c
end module error

View file

@ -1,11 +1,13 @@
#include "openmc/error.h"
#include "openmc/message_passing.h"
#include "openmc/settings.h"
#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h> // for isatty
#endif
#include <cstdlib> // for exit
#include <iomanip> // for setw
#include <iostream>
@ -18,21 +20,10 @@ void abort_mpi(int code)
}
#endif
void warning(const std::string& message)
void output(const std::string& message, std::ostream& out, int indent)
{
#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();
@ -57,6 +48,20 @@ void warning(const std::string& message)
i_start += (pos == std::string::npos) ? line_len : pos + 1;
}
}
}
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
std::cerr << " WARNING: ";
output(message, std::cerr, 10);
#ifdef _POSIX_VERSION
// Reset color for terminal
@ -66,4 +71,43 @@ void warning(const std::string& message)
#endif
}
void write_message(const std::string& message, int level)
{
// Only allow master to print to screen
if (!mpi::master) return;
if (level <= settings::verbosity) {
std::cout << " ";
output(message, std::cout, 1);
}
}
void fatal_error(const std::string& message, int err)
{
#ifdef _POSIX_VERSION
// Make output red if user is in a terminal
if (isatty(STDERR_FILENO)) {
std::cerr << "\033[0;31m";
}
#endif
// Write error message
std::cerr << " ERROR: ";
output(message, std::cerr, 8);
#ifdef _POSIX_VERSION
// Reset color for terminal
if (isatty(STDERR_FILENO)) {
std::cerr << "\033[0m";
}
#endif
#ifdef OPENMC_MPI
MPI_Abort(mpi::intracomm, err);
#endif
// Abort the program
std::exit(err);
}
} // namespace openmc