Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//logging.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//logging.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with logging.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
27
28
29#ifndef BERTINI_LOGGING_HPP
30#define BERTINI_LOGGING_HPP
31
32
33//#define BOOST_LOG_DYN_LINK
34
35#include <boost/log/core.hpp>
36#include <boost/log/trivial.hpp>
37#include <boost/log/expressions.hpp>
38#include <boost/log/sinks/text_file_backend.hpp>
39#include <boost/log/utility/setup/file.hpp>
40#include <boost/log/utility/setup/common_attributes.hpp>
41#include <boost/log/sources/severity_logger.hpp>
42#include <boost/log/sources/record_ostream.hpp>
43
44namespace bertini{
45namespace logging{
46
47
48
49 namespace blog = boost::log;
50
51 using severity_level = blog::trivial::severity_level;
52 namespace src = blog::sources;
53 namespace sinks = blog::sinks;
54 namespace keywords = blog::keywords;
55
56
57 // the following is adapted from https://stackoverflow.com/questions/11421432/
58 // question answered by user James Adkison, asked by Adi, edited by James McNellis.
59 // the adaptation is the replacement of std::ostream with the blros type. why the unmodified code still fails
60 // for blros types is a mystery.
61 using blros = blog::record_ostream;
63 template<typename T>
64 blros& operator<<(typename std::enable_if<std::is_enum<T>::value, blros>::type& stream, const T& e)
65 {
66 return stream << static_cast<typename std::underlying_type<T>::type>(e);
67 }
68
69
87 struct Logging
88 {
89
91 static
92 void Init(std::string const& name_pattern = "bertini_%N.log",
93 std::string const& format = "%Message%",
94 unsigned rotation_size = 10*1024*1024,
95 severity_level const& new_level = severity_level::error)
96 {
97 AddFile(name_pattern, format, rotation_size);
98 SetLevel(new_level);
99
100 BOOST_LOG_TRIVIAL(info) << "initialized logging";
101 }
102
103
105 static
106 void AddFile(std::string const& name_pattern, std::string const& format, unsigned rotation_size, bool auto_flush = true)
107 {
108 blog::add_file_log
109 (
110 keywords::file_name = name_pattern,
111 keywords::rotation_size = rotation_size,
112 keywords::format = format,
113 keywords::auto_flush = auto_flush
114 );
115 }
116
117
123 static
124 void SetLevel(severity_level const& new_level)
125 {
126 blog::core::get()->set_filter
127 (
128 blog::trivial::severity >= new_level
129 );
130 }
131
132
133 };
134
135
136
137} // namespace logging
138} // re: namespace bertini
139
140
141
142#endif
Definition logging.hpp:45
blros & operator<<(typename std::enable_if< std::is_enum< T >::value, blros >::type &stream, const T &e)
Stream-insertion of an enum into a log record, via its underlying integer type.
Definition logging.hpp:64
blog::trivial::severity_level severity_level
The logging severity level type (from Boost.Log).
Definition logging.hpp:51
blog::record_ostream blros
Shorthand for the Boost.Log record output stream type.
Definition logging.hpp:61
Definition algorithm_builder.hpp:36
Definition logging.hpp:88
static void Init(std::string const &name_pattern="bertini_%N.log", std::string const &format="%Message%", unsigned rotation_size=10 *1024 *1024, severity_level const &new_level=severity_level::error)
Initialize Bertini2 logging (call once); adds a rotating log file and sets the level.
Definition logging.hpp:92
static void SetLevel(severity_level const &new_level)
trivial logger-provided severity levels are
Definition logging.hpp:124
static void AddFile(std::string const &name_pattern, std::string const &format, unsigned rotation_size, bool auto_flush=true)
Add a rotating log-file sink with the given name pattern, format, and rotation size.
Definition logging.hpp:106