Code forensics  0.1
Generate historical information about code changes
logging.hpp
Go to the documentation of this file.
1 #ifndef LOGGING_HPP
2 #define LOGGING_HPP
3 
4 #include <unordered_set>
5 #include <fstream>
6 #include <chrono>
7 
8 #include <boost/log/trivial.hpp>
9 #include <boost/log/common.hpp>
10 #include <boost/log/expressions.hpp>
11 #include <boost/log/attributes.hpp>
12 #include <boost/log/sinks.hpp>
13 #include <boost/log/sources/logger.hpp>
14 #include <boost/log/utility/record_ordering.hpp>
15 #include <boost/core/null_deleter.hpp>
16 
17 #include "common.hpp"
18 #include "program_state.hpp"
19 
20 #include <shared_mutex>
21 
22 #include <indicators/progress_bar.hpp>
23 #include <indicators/block_progress_bar.hpp>
24 #include <indicators/cursor_control.hpp>
25 
26 indicators::BlockProgressBar init_progress(int max, int width = 60);
27 
28 using BarText = indicators::option::PostfixText;
29 
30 
31 void tick_next(
32  indicators::BlockProgressBar& bar,
33  int& count,
34  int max,
35  CR<Str> name,
36  CR<Str> extra = "");
37 
38 
39 namespace logging = boost::log;
40 
41 namespace boost::log {
42 namespace expr = logging::expressions;
43 namespace attrs = logging::attributes;
44 }; // namespace boost::log
45 
46 
47 namespace sc = std::chrono;
48 
49 struct ScopedBar {
50  int count = 0;
51  int max;
52  indicators::BlockProgressBar bar;
55  bool timed;
56  sc::high_resolution_clock::time_point start;
57 
58 
59  inline ScopedBar(
60  walker_state* _state,
61  int _max,
62  CR<Str> _annotation,
63  bool _timed = true,
64  int _width = 40)
65  : max(_max)
66  , bar(init_progress(_max, _width))
67  , state(_state)
68  , annotation(_annotation)
69  , timed(_timed) {
70  if (state->config->log_progress_bars) {
71  // Avoid verlap of the progress bar and the stdout logging.
72  logging::core::get()->flush();
73  }
74  if (timed) { start = sc::high_resolution_clock::now(); }
75  }
76 
77  inline ~ScopedBar() {
78  if (state->config->log_progress_bars) { bar.mark_as_completed(); }
79  }
80 
81  inline void tick() {
82  if (state->config->log_progress_bars) {
83  Str extra;
84  if (timed) {
85  sc::duration<double>
86  diff = sc::high_resolution_clock::now() - start;
87  extra = fmt::format(
88  " {:1.4f}/{:4.2f}/{:4.2f}",
89  (diff / count).count(),
90  (max - count) * (diff / count).count(),
91  diff.count());
92  }
93  tick_next(bar, count, max, annotation, extra);
94  }
95  }
96 };
97 
98 
99 using Logger = logging::sources::severity_logger<
100  logging::trivial::severity_level>;
101 
105 namespace boost::log {
106 using severity = logging::trivial::severity_level;
107 }
108 
111 #define CUSTOM_LOG(logger, sev) \
112  set_get_attrib("File", Str{__FILE__}); \
113  set_get_attrib("Line", __LINE__); \
114  set_get_attrib("Func", Str{__PRETTY_FUNCTION__}); \
115  BOOST_LOG_SEV(logger, sev)
116 
117 
121 template <typename T>
122 using MutLog = logging::attrs::mutable_constant<T, std::shared_mutex>;
123 
130 template <typename ValueType>
131 auto set_get_attrib(const char* name, ValueType value) -> ValueType {
132  auto attr = logging::attribute_cast<MutLog<ValueType>>(
133  logging::core::get()->get_global_attributes()[name]);
134  attr.set(value);
135  return attr.get();
136 }
137 
138 inline auto get_logger(walker_state* state) -> Logger& {
139  return *(state->logger);
140 }
141 
142 inline auto get_logger(UPtr<walker_state>& state) -> Logger& {
143  return *(state->logger);
144 }
145 
146 inline auto get_logger(SPtr<Logger>& in) -> Logger& { return *in; }
147 
148 #define LOG_T(state) \
149  CUSTOM_LOG((get_logger(state)), logging::severity::trace)
150 #define LOG_D(state) \
151  CUSTOM_LOG((get_logger(state)), logging::severity::debug)
152 #define LOG_I(state) \
153  CUSTOM_LOG((get_logger(state)), logging::severity::info)
154 #define LOG_W(state) \
155  CUSTOM_LOG((get_logger(state)), logging::severity::warning)
156 #define LOG_E(state) \
157  CUSTOM_LOG((get_logger(state)), logging::severity::error)
158 #define LOG_F(state) \
159  CUSTOM_LOG((get_logger(state)), logging::severity::fatal)
160 
162 using backend_t = logging::sinks::text_ostream_backend;
163 using sink_t = logging::sinks::asynchronous_sink<
164  backend_t,
165  logging::sinks::unbounded_ordering_queue<
166  logging::attribute_value_ordering<
167  unsigned int,
168  std::less<unsigned int>>>>;
169 
170 BOOST_LOG_ATTRIBUTE_KEYWORD(
171  severity,
172  "Severity",
173  logging::trivial::severity_level)
174 
175 void log_formatter(
176  logging::record_view const& rec,
177  logging::formatting_ostream& strm);
178 
179 Pair<char, fmt::text_style> format_style(logging::severity level);
180 
181 void out_formatter(
182  logging::record_view const& rec,
183  logging::formatting_ostream& strm);
184 
185 boost::shared_ptr<sink_t> create_file_sink(CR<Str> outfile);
186 
187 boost::shared_ptr<sink_t> create_std_sink();
188 
190 
191 #endif // LOGGING_HPP
ScopedBar::tick
void tick()
Definition: logging.hpp:81
out_formatter
void out_formatter(logging::record_view const &rec, logging::formatting_ostream &strm)
log_formatter
void log_formatter(const boost::log::record_view &rec, boost::log::formatting_ostream &strm)
Definition: logging.cpp:23
BarText
indicators::option::PostfixText BarText
Definition: logging.hpp:28
boost::log::severity
logging::trivial::severity_level severity
Definition: logging.hpp:106
backend_t
logging::sinks::text_ostream_backend backend_t
Definition: logging.hpp:162
get_logger
auto get_logger(walker_state *state) -> Logger &
Definition: logging.hpp:138
ScopedBar::ScopedBar
ScopedBar(walker_state *_state, int _max, CR< Str > _annotation, bool _timed=true, int _width=40)
Definition: logging.hpp:59
ScopedBar
Definition: logging.hpp:49
ScopedBar::start
sc::high_resolution_clock::time_point start
Definition: logging.hpp:56
boost::log
Definition: logging.hpp:41
ScopedBar::timed
bool timed
Definition: logging.hpp:55
ScopedBar::max
int max
Definition: logging.hpp:51
MutLog
logging::attrs::mutable_constant< T, std::shared_mutex > MutLog
Definition: logging.hpp:122
ScopedBar::annotation
Str annotation
Definition: logging.hpp:53
init_progress
indicators::BlockProgressBar init_progress(int max, int width=60)
Definition: logging.cpp:5
walker_state::config
CP< walker_config > config
Definition: program_state.hpp:200
ScopedBar::count
int count
Definition: logging.hpp:50
format_style
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", logging::trivial::severity_level) void log_formatter(logging Pair< char, fmt::text_style > format_style(logging::severity level)
Definition: logging.cpp:41
init_logger_properties
void init_logger_properties()
Definition: logging.cpp:109
create_file_sink
boost::shared_ptr< sink_t > create_file_sink(CR< Str > outfile)
Definition: logging.cpp:72
walker_state
Mutable state passed around walker configurations.
Definition: program_state.hpp:199
set_get_attrib
auto set_get_attrib(const char *name, ValueType value) -> ValueType
Definition: logging.hpp:131
ScopedBar::bar
indicators::BlockProgressBar bar
Definition: logging.hpp:52
ScopedBar::state
walker_state * state
Definition: logging.hpp:54
Logger
logging::sources::severity_logger< logging::trivial::severity_level > Logger
Definition: logging.hpp:100
sink_t
logging::sinks::asynchronous_sink< backend_t, logging::sinks::unbounded_ordering_queue< logging::attribute_value_ordering< unsigned int, std::less< unsigned int > >> > sink_t
Definition: logging.hpp:168
ScopedBar::~ScopedBar
~ScopedBar()
Definition: logging.hpp:77
create_std_sink
boost::shared_ptr< sink_t > create_std_sink()
Definition: logging.cpp:92
program_state.hpp
Main code analysis state and configuration classes.
tick_next
void tick_next(indicators::BlockProgressBar &bar, int &count, int max, CR< Str > name, CR< Str > extra="")