opm-common/opm/common/OpmLog/LogUtil.cpp

99 lines
2.9 KiB
C++
Raw Normal View History

/*
Copyright 2015 Statoil ASA.
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 3 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/>.
*/
#include <sstream>
#include <stdexcept>
#include <opm/common/OpmLog/LogUtil.hpp>
namespace Opm {
namespace Log {
bool isPower2(int64_t x) {
return ((x != 0) && !(x & (x - 1)));
}
std::string fileMessage(const std::string& filename , int line , const std::string& message) {
std::ostringstream oss;
oss << message << "\n" << "In file " << filename << ", line " << line << "\n";
return oss.str();
}
std::string fileMessage(int64_t messageType , const std::string& filename , int line , const std::string& message) {
return fileMessage( filename , line , prefixMessage( messageType , message ));
}
std::string prefixMessage(int64_t messageType, const std::string& message) {
std::string prefix;
switch (messageType) {
case MessageType::Debug:
prefix = "Debug";
break;
2016-06-08 02:00:24 -05:00
case MessageType::Note:
prefix = "Note";
2016-06-08 03:55:38 -05:00
break;
case MessageType::Info:
prefix = "Info";
break;
case MessageType::Warning:
prefix = "Warning";
break;
case MessageType::Error:
prefix = "Error";
break;
case MessageType::Problem:
prefix = "Problem";
break;
case MessageType::Bug:
prefix = "Bug";
break;
default:
throw std::invalid_argument("Unhandled messagetype");
}
return prefix + ": " + message;
}
2016-05-09 04:29:11 -05:00
std::string colorCodeMessage(int64_t messageType, const std::string& message) {
switch (messageType) {
case MessageType::Debug:
2016-06-08 02:00:24 -05:00
case MessageType::Note:
case MessageType::Info:
return message; // No color coding, not even the code for default color.
2016-05-09 04:29:11 -05:00
case MessageType::Warning:
return AnsiTerminalColors::blue_strong + message + AnsiTerminalColors::none;
2016-05-09 04:29:11 -05:00
case MessageType::Error:
case MessageType::Problem:
case MessageType::Bug:
return AnsiTerminalColors::red_strong + message + AnsiTerminalColors::none;
2016-05-09 04:29:11 -05:00
default:
throw std::invalid_argument("Unhandled messagetype");
}
}
}
}