preCICE v3.1.2
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1#include <boost/log/attributes/constant.hpp>
2#include <boost/log/attributes/function.hpp>
3#include <boost/log/attributes/named_scope.hpp>
4#include <boost/log/attributes/timer.hpp>
5#include <boost/log/sources/severity_feature.hpp>
6#include <boost/log/trivial.hpp>
7#include <boost/log/utility/setup/common_attributes.hpp>
8#include <utility>
9#include <utils/assertion.hpp>
10
12#include "logging/Logger.hpp"
13
14namespace precice::logging {
15
17template <class BaseT>
18class precice_feature : public BaseT {
19public:
20 using char_type = typename BaseT::char_type;
21 using threading_model = typename BaseT::threading_model;
22
23 using open_record_lock = typename boost::log::strictest_lock<
24 boost::lock_guard<threading_model>,
25 typename BaseT::open_record_lock,
26 typename BaseT::add_attribute_lock,
27 typename BaseT::remove_attribute_lock>::type;
28
29protected:
30 template <typename ArgsT>
31 boost::log::record open_record_unlocked(ArgsT const &args);
32};
33
36namespace keywords {
40} // namespace keywords
41
43template <typename BaseT>
44template <typename ArgsT>
45boost::log::record precice_feature<BaseT>::open_record_unlocked(ArgsT const &args)
46{
47 // Extract the named argument from the parameters pack
48 int line_value = args[keywords::line | -1];
49 std::string file_value = args[keywords::file | std::string()];
50 std::string func_value = args[keywords::func | std::string()];
51
52 PRECICE_ASSERT(line_value >= 0);
53 PRECICE_ASSERT(!file_value.empty());
54 PRECICE_ASSERT(!func_value.empty());
55
56 // Process extracted tags
57 using boost::log::attributes::constant;
58 boost::log::attribute_set &attrs = BaseT::attributes();
59 {
60 [[maybe_unused]] auto res = BaseT::add_attribute_unlocked("Line", constant<int>(line_value));
61 PRECICE_ASSERT(res.second);
62 }
63 {
64 [[maybe_unused]] auto res = BaseT::add_attribute_unlocked("File", constant<std::string>(std::move(file_value)));
65 PRECICE_ASSERT(res.second);
66 }
67 {
68 [[maybe_unused]] auto res = BaseT::add_attribute_unlocked("Function", constant<std::string>(std::move(func_value)));
69 PRECICE_ASSERT(res.second);
70 }
71
72 // Forward the call to the base feature
73 auto &&record = BaseT::open_record_unlocked(args);
74
75 // cleanup
76 attrs.erase("Line");
77 attrs.erase("File");
78 attrs.erase("Function");
79
80 return std::move(record);
81}
82
84struct precice_log : public boost::mpl::quote1<precice_feature> {
85};
86
88template <class BaseLogger>
89using BoostLogger = boost::log::sources::basic_composite_logger<
90 char,
91 BaseLogger,
92 boost::log::sources::single_thread_model,
93 boost::log::sources::features<
94 boost::log::sources::severity<boost::log::trivial::severity_level>,
96
101class Logger::LoggerImpl : public BoostLogger<Logger::LoggerImpl> {
102public:
106 explicit LoggerImpl(std::string_view module);
107};
108
111{
112 namespace attrs = boost::log::attributes;
113
114 // The preCICE attribute marks attribute values that originate from preCICE
115 add_attribute("preCICE", attrs::constant<bool>(true));
116 add_attribute("Participant", attrs::make_function<std::string>([] { return getGlobalLoggingConfig().participant; }));
117 add_attribute("Rank", attrs::make_function<int>([] { return getGlobalLoggingConfig().rank; }));
118
119 // Constant attributes
120 add_attribute("Module", attrs::constant<std::string>(std::string{module}));
121
122 // Ensure, boost-provided attributes are present
123 add_attribute("TimeStamp", attrs::local_clock());
124 add_attribute("Runtime", attrs::timer());
125 add_attribute("Scope", attrs::named_scope());
126}
127
129 : _impl(new LoggerImpl{module}) {}
130
132Logger::~Logger() = default;
133
136 : _impl(std::make_unique<LoggerImpl>(*other._impl))
137{
138}
139
140Logger::Logger(Logger &&other) noexcept = default;
141
143{
144 swap(other);
145 return *this;
146}
147
148void Logger::swap(Logger &other) noexcept
149{
150 _impl.swap(other._impl);
151}
152
154#define PRECICE_LOG_IMPL(lg, sev, loc) \
155 BOOST_LOG_STREAM_WITH_PARAMS((lg), (boost::log::keywords::severity = (sev))(keywords::line = (loc.line))(keywords::file = (loc.file))(keywords::func = (loc.func)))
156
158{
159 try {
160 PRECICE_LOG_IMPL(*_impl, boost::log::trivial::severity_level::error, loc) << mess;
161 } catch (...) {
162 }
163}
164
166{
167 try {
168 PRECICE_LOG_IMPL(*_impl, boost::log::trivial::severity_level::warning, loc) << mess;
169 } catch (...) {
170 }
171}
172
174{
175 try {
176 PRECICE_LOG_IMPL(*_impl, boost::log::trivial::severity_level::info, loc) << mess;
177 } catch (...) {
178 }
179}
180
182{
183 try {
184 PRECICE_LOG_IMPL(*_impl, boost::log::trivial::severity_level::debug, loc) << mess;
185 } catch (...) {
186 }
187}
188
190{
191 try {
192 PRECICE_LOG_IMPL(*_impl, boost::log::trivial::severity_level::trace, loc) << mess;
193 } catch (...) {
194 }
195}
196
197#undef PRECICE_LOG_IMPL
198
199} // namespace precice::logging
#define PRECICE_LOG_IMPL(lg, sev, loc)
Convenience macro that automatically passes LogLocation info to the logger.
Definition Logger.cpp:154
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
LoggerImpl(std::string_view module)
Registers attributes that don't depend on the LogLocation.
Definition Logger.cpp:110
This class provides a lightweight logger.
Definition Logger.hpp:16
void warning(LogLocation loc, std::string_view mess) noexcept
Definition Logger.cpp:165
void error(LogLocation loc, std::string_view mess) noexcept
Definition Logger.cpp:157
Logger & operator=(Logger other)
Definition Logger.cpp:142
void debug(LogLocation loc, std::string_view mess) noexcept
Definition Logger.cpp:181
void info(LogLocation loc, std::string_view mess) noexcept
Definition Logger.cpp:173
void swap(Logger &other) noexcept
Definition Logger.cpp:148
Logger(std::string_view module)
Definition Logger.cpp:128
~Logger()
This is required for the std::unique_ptr.
std::unique_ptr< LoggerImpl > _impl
Pimpl to the logger implementation.
Definition Logger.hpp:44
void trace(LogLocation loc, std::string_view mess) noexcept
Definition Logger.cpp:189
A feature that adds LogLocation info to log attributes.
Definition Logger.cpp:18
typename BaseT::threading_model threading_model
Definition Logger.cpp:21
typename boost::log::strictest_lock< boost::lock_guard< threading_model >, typename BaseT::open_record_lock, typename BaseT::add_attribute_lock, typename BaseT::remove_attribute_lock >::type open_record_lock
Definition Logger.cpp:23
boost::log::record open_record_unlocked(ArgsT const &args)
Adds log attributes to the current logger based on the LogLocation info.
Definition Logger.cpp:45
typename BaseT::char_type char_type
Definition Logger.cpp:20
T empty(T... args)
BOOST_PARAMETER_KEYWORD(line_ns, line)
contains the logging framework.
boost::log::sources::basic_composite_logger< char, BaseLogger, boost::log::sources::single_thread_model, boost::log::sources::features< boost::log::sources::severity< boost::log::trivial::severity_level >, precice_log > > BoostLogger
The boost logger that combines required featrues.
Definition Logger.cpp:89
GlobalLoggingConfig & getGlobalLoggingConfig()
Returns the global logging configuration.
STL namespace.
Struct used to capture the original location of a log request.
Definition Logger.hpp:9
Required by BoostLogger to use precice_feature.
Definition Logger.cpp:84