opm-common
MessageFormatter.hpp
1 /*
2  Copyright 2016 SINTEF ICT, Applied Mathematics.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_MESSAGEFORMATTER_HEADER_INCLUDED
21 #define OPM_MESSAGEFORMATTER_HEADER_INCLUDED
22 
23 #include <opm/common/OpmLog/LogUtil.hpp>
24 
25 #include <cstdint>
26 #include <string>
27 
28 namespace Opm
29 {
30 
33  {
34  public:
36  virtual ~MessageFormatterInterface() = default;
40  virtual std::string format(const std::int64_t message_flag, const std::string& message) = 0;
41  };
42 
45  {
46  public:
48  SimpleMessageFormatter(const bool use_prefix, const bool use_color_coding)
49  : use_color_coding_(use_color_coding)
50  {
51  if (use_prefix) {
52  prefix_flag_ = Log::DefaultMessageTypes;
53  }
54  }
55 
56  SimpleMessageFormatter(const std::int64_t prefix_flag, const bool use_color_coding)
57  : use_color_coding_(use_color_coding),
58  prefix_flag_(prefix_flag)
59  {
60  }
61 
62  explicit SimpleMessageFormatter(const bool use_color_coding)
63  : use_color_coding_(use_color_coding)
64  {
65  prefix_flag_ = Log::MessageType::Warning + Log::MessageType::Error
66  + Log::MessageType::Problem + Log::MessageType::Bug;
67  }
71  virtual std::string format(const std::int64_t message_flag, const std::string& message) override
72  {
73  std::string msg = message;
74  if (message_flag & prefix_flag_) {
75  msg = Log::prefixMessage(message_flag, msg);
76  }
77  if (use_color_coding_) {
78  msg = Log::colorCodeMessage(message_flag, msg);
79  }
80  return msg;
81  }
82  private:
83  bool use_color_coding_ = false;
84  std::int64_t prefix_flag_ = 0;
85  };
86 
87 } // namespace Opm
88 
89 #endif // OPM_MESSAGEFORMATTER_HEADER_INCLUDED
virtual std::string format(const std::int64_t message_flag, const std::string &message) override
Returns a copy of the input string with a flag-dependant prefix (if use_prefix) and the entire messag...
Definition: MessageFormatter.hpp:71
virtual std::string format(const std::int64_t message_flag, const std::string &message)=0
Should return a possibly modified/decorated version of the input string, the formatting applied depen...
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
A simple formatter capable of adding message prefixes and colors.
Definition: MessageFormatter.hpp:44
virtual ~MessageFormatterInterface()=default
Virtual destructor to enable inheritance.
SimpleMessageFormatter(const bool use_prefix, const bool use_color_coding)
Constructor controlling formatting to be applied.
Definition: MessageFormatter.hpp:48
Abstract interface for message formatting classes.
Definition: MessageFormatter.hpp:32