MessageFormatter.hpp
Go to the documentation of this file.
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
24#include <string>
25
26namespace Opm
27{
28
29
32 {
33 public:
35 virtual ~MessageFormatterInterface() = default;
39 virtual std::string format(const int64_t message_flag, const std::string& message) = 0;
40 };
41
42
43
44
45
48 {
49 public:
51 SimpleMessageFormatter(const bool use_prefix, const bool use_color_coding)
52 : use_color_coding_(use_color_coding)
53 {
54 if (use_prefix) {
55 prefix_flag_ = Log::DefaultMessageTypes;
56 }
57 }
58
59
60 SimpleMessageFormatter(const int64_t prefix_flag, const bool use_color_coding)
61 : use_color_coding_(use_color_coding),
62 prefix_flag_(prefix_flag)
63 {
64 }
65
66
67 explicit SimpleMessageFormatter(const bool use_color_coding)
68 : use_color_coding_(use_color_coding)
69 {
72 }
76 virtual std::string format(const int64_t message_flag, const std::string& message) override
77 {
78 std::string msg = message;
79 if (message_flag & prefix_flag_) {
80 msg = Log::prefixMessage(message_flag, msg);
81 }
82 if (use_color_coding_) {
83 msg = Log::colorCodeMessage(message_flag, msg);
84 }
85 return msg;
86 }
87 private:
88 bool use_color_coding_ = false;
89 int64_t prefix_flag_ = 0;
90 };
91
92
93} // namespace Opm
94
95#endif // OPM_MESSAGEFORMATTER_HEADER_INCLUDED
const char *const string
Definition: cJSON.h:170
Abstract interface for message formatting classes.
Definition: MessageFormatter.hpp:32
virtual ~MessageFormatterInterface()=default
Virtual destructor to enable inheritance.
virtual std::string format(const int64_t message_flag, const std::string &message)=0
A simple formatter capable of adding message prefixes and colors.
Definition: MessageFormatter.hpp:48
virtual std::string format(const int64_t message_flag, const std::string &message) override
Definition: MessageFormatter.hpp:76
SimpleMessageFormatter(const int64_t prefix_flag, const bool use_color_coding)
Definition: MessageFormatter.hpp:60
SimpleMessageFormatter(const bool use_color_coding)
Definition: MessageFormatter.hpp:67
SimpleMessageFormatter(const bool use_prefix, const bool use_color_coding)
Constructor controlling formatting to be applied.
Definition: MessageFormatter.hpp:51
const int64_t Bug
Definition: LogUtil.hpp:38
const int64_t Problem
Definition: LogUtil.hpp:37
const int64_t Error
Definition: LogUtil.hpp:36
const int64_t Warning
Definition: LogUtil.hpp:35
const int64_t DefaultMessageTypes
Definition: LogUtil.hpp:41
std::string colorCodeMessage(int64_t messageType, const std::string &msg)
std::string prefixMessage(int64_t messageType, const std::string &msg)
Definition: A.hpp:4