diff --git a/opm/common/OpmLog/OpmLog.cpp b/opm/common/OpmLog/OpmLog.cpp index 94c103427..cc5f85dc0 100644 --- a/opm/common/OpmLog/OpmLog.cpp +++ b/opm/common/OpmLog/OpmLog.cpp @@ -21,9 +21,27 @@ #include #include #include +#include // For errno +#include // For fileno() and stdout +#include // For isatty() namespace Opm { + namespace { + bool stdoutIsTerminal() + { + const int errno_save = errno; // For playing nice with C error handling. + const int file_descriptor = fileno(stdout); + if (file_descriptor == -1) { + // stdout is an invalid stream + errno = errno_save; + return false; + } else { + return isatty(file_descriptor); + } + } + } + std::shared_ptr OpmLog::getLogger() { if (!m_logger) @@ -176,12 +194,14 @@ namespace Opm { - void OpmLog::setupSimpleDefaultLogging(const bool use_prefix) + void OpmLog::setupSimpleDefaultLogging(const bool use_prefix, + const bool use_color_coding, + const int message_limit) { std::shared_ptr streamLog = std::make_shared(std::cout, Log::DefaultMessageTypes); OpmLog::addBackend( "SimpleDefaultLog", streamLog); - streamLog->setMessageLimiter(std::make_shared(10)); - streamLog->setMessageFormatter(std::make_shared(use_prefix, true)); + streamLog->setMessageLimiter(std::make_shared(message_limit)); + streamLog->setMessageFormatter(std::make_shared(use_prefix, use_color_coding && stdoutIsTerminal())); } /******************************************************************/ diff --git a/opm/common/OpmLog/OpmLog.hpp b/opm/common/OpmLog/OpmLog.hpp index 655da9da1..0b42876ad 100644 --- a/opm/common/OpmLog/OpmLog.hpp +++ b/opm/common/OpmLog/OpmLog.hpp @@ -64,7 +64,16 @@ public: static void removeAllBackends(); static bool enabledMessageType( int64_t messageType ); static void addMessageType( int64_t messageType , const std::string& prefix); - static void setupSimpleDefaultLogging(const bool use_prefix); + + /// Create a basic logging setup that will send all log messages to standard output. + /// + /// By default category prefixes will be printed (i.e. Error: or + /// Warning:), color coding will be used, and a maximum of 10 + /// messages with the same tag will be printed. These settings can + /// be controlled by the function parameters. + static void setupSimpleDefaultLogging(const bool use_prefix = true, + const bool use_color_coding = true, + const int message_limit = 10); template static std::shared_ptr getBackend(const std::string& name) {