logger.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 // Heavily inspired by spdlog, Copyright(c) 2015 Gabi Melman
20 
21 #include <momemta/impl/logger/logger.h>
22 
23 #include <momemta/impl/logger/common.h>
24 
25 logger::logger::logger(::logger::sink_ptr single_sink) {
26  _level = logging::level::info;
27  _flush_level = logging::level::off;
28  _sinks.push_back(single_sink);
29  _formatter = std::make_shared<::logger::full_formatter>();
30  _index.store(1, std::memory_order_relaxed);
31 }
32 
33 logger::logger::~logger() = default;
34 
35 void logger::logger::set_level(logging::level::level_enum log_level) {
36  _level.store(log_level);
37 }
38 void logger::logger::flush_on(logging::level::level_enum log_level) {
39  _flush_level.store(log_level);
40 }
41 
42 void logger::logger::set_formatter(formatter_ptr formatter) {
43  _formatter = formatter;
44 }
45 
46 logging::level::level_enum logger::logger::level() const {
47  return static_cast<logging::level::level_enum>(_level.load(std::memory_order_relaxed));
48 }
49 
50 bool logger::logger::should_log(logging::level::level_enum msg_level) const {
51  return msg_level >= _level.load(std::memory_order_relaxed);
52 }
53 
54 // protected virtual called at end of each user log call (if enabled) by the line_logger
55 void logger::logger::_sink_it(::logger::details::log_msg& msg) {
56  _formatter->format(msg);
57 
58  for (auto &sink : _sinks) {
59  if (sink->should_log( msg.level)) {
60  sink->log(msg);
61  }
62  }
63 
64  if (_should_flush_on(msg))
65  flush();
66 }
67 
68 void logger::logger::flush() {
69  for (auto& sink : _sinks)
70  sink->flush();
71 }
72 
73 
74 bool logger::logger::_should_flush_on(const ::logger::details::log_msg &msg) {
75  const auto flush_level = _flush_level.load(std::memory_order_relaxed);
76  return (msg.level >= flush_level) && (msg.level != logging::level::off);
77 }
78 
79 const std::vector<logger::sink_ptr>& logger::logger::sinks() const {
80  return _sinks;
81 }
82 
83 ::logger::ostream_wrapper::ostream_wrapper(::logger::logger& l, logging::level::level_enum lvl):
84  _valid(true),
85  _logger(l),
86  _lvl(lvl) {
87  // Empty
88 }
89 
90 ::logger::ostream_wrapper::~ostream_wrapper() {
91  _logger.log(_lvl, _stream.str());
92  _valid = false;
93 }
94 
95 bool ::logger::ostream_wrapper::valid() {
96  return _valid;
97 }
98 
99 void ::logger::ostream_wrapper::destroy() {
100  _valid = false;
101 }
bool _should_flush_on(const details::log_msg &)
return true if the given message level should trigger a flush
Definition: logger.cc:74