6 #include <boost/date_time/posix_time/posix_time.hpp> 7 #include <boost/log/attributes.hpp> 8 #include <boost/log/expressions.hpp> 9 #include <boost/log/sinks/text_ostream_backend.hpp> 10 #include <boost/log/utility/formatting_ostream.hpp> 11 #include <boost/log/utility/setup/console.hpp> 12 #include <boost/log/utility/setup/file.hpp> 13 #include <boost/shared_ptr.hpp> 21 #ifndef JB_LOG_DEFAULTS_minimum_severity 22 #define JB_LOG_DEFAULTS_minimum_severity jb::severity_level::info 23 #endif // JB_LOG_DEFAULTS_minimum_severity 25 #ifndef JB_LOG_DEFAULTS_minimum_console_severity 26 #define JB_LOG_DEFAULTS_minimum_console_severity jb::severity_level::trace 27 #endif // JB_LOG_DEFAULTS_minimum_console_severity 29 #ifndef JB_LOG_DEFAULTS_enable_file_logging 30 #define JB_LOG_DEFAULTS_enable_file_logging false 31 #endif // JB_LOG_DEFAULTS_enable_file_logging 33 #ifndef JB_LOG_DEFAULTS_enable_console_logging 34 #define JB_LOG_DEFAULTS_enable_console_logging true 35 #endif // JB_LOG_DEFAULTS_enable_console_logging 37 #ifndef JB_LOG_DEFAULTS_logfile_suffix 38 #define JB_LOG_DEFAULTS_logfile_suffix "_%Y%m%d.%N.log" 39 #endif // JB_LOG_DEFAULTS_logfile_suffix 41 #ifndef JB_LOG_DEFAULTS_logfile_archive_directory 42 #define JB_LOG_DEFAULTS_logfile_archive_directory "" 43 #endif // JB_LOG_DEFAULTS_logfile_archive_directory 45 #ifndef JB_LOG_DEFAULTS_maximum_size_archived 46 #define JB_LOG_DEFAULTS_maximum_size_archived 128L * 1024 * 1024 * 1024 47 #endif // JB_LOG_DEFAULTS_maximum_size_archived 49 #ifndef JB_LOG_DEFAULTS_minimum_free_space 50 #define JB_LOG_DEFAULTS_minimum_free_space 8L * 1024 * 1024 * 1024 51 #endif // JB_LOG_DEFAULTS_minimum_free_space 67 desc(
"minimum-severity")
68 .help(
"Log messages below this severity are filtered out"),
71 desc(
"minimum-console-severity")
72 .help(
"Log messages below this severity are " 73 "filtered out in the console"),
76 desc(
"enable-console-logging")
77 .help(
"If set, log messages are sent to the " 78 "console. Enabled by default"),
81 desc(
"enable-file-logging")
82 .help(
"If set, log messages are set to a " 83 "log file. Disabled by default"),
86 desc(
"logfile-basename")
87 .help(
"Define the name of the logfile," 88 " used only if enable-file-logging is true"),
91 desc(
"logfile-suffix")
92 .help(
"Define suffix for the filename," 93 " typically _%Y%m%d.%N.log." 94 " The format characters are defined" 98 desc(
"logfile-archive-directory")
99 .help(
"Define where are old, " 100 "and full logfiles archived."),
103 desc(
"maximum-size-archived")
104 .help(
"Define how much space, at most, " 105 "is used for saved logfiles."),
108 desc(
"minimum-free-space")
109 .help(
"Define the minimum amount of free" 110 " disk space kept after cleaning up" 118 "enable-file-logging is set," 119 " you must also set logfile-basename",
130 current_thread_id,
"ThreadID",
131 boost::log::attributes::current_thread_id::value_type)
133 local_time,
"TimeStamp", boost::log::attributes::local_clock::value_type)
139 JB_ASSERT_THROW(*attr[jb::log::severity] >= *attr[jb::log::min_severity]);
142 return (*attr[jb::log::transaction] % 10000) == 0;
148 boost::log::record_view
const& rec, boost::log::formatting_ostream& strm) {
149 strm <<
" [" << rec[jb::log::current_thread_id] <<
"]" 151 << rec[jb::log::severity] <<
"]";
154 strm <<
" tid=<" << rec[jb::log::transaction] <<
">";
156 strm <<
" " << rec[boost::log::expressions::smessage] <<
" (" 157 << rec[jb::log::filename] <<
":" << rec[jb::log::lineno] <<
")";
161 boost::log::record_view
const& rec, boost::log::formatting_ostream& strm) {
162 auto usecs = rec[jb::log::local_time]->time_of_day().total_microseconds();
169 boost::log::record_view
const& rec, boost::log::formatting_ostream& strm) {
170 auto date = rec[jb::log::local_time]->date();
171 auto usecs = rec[jb::log::local_time]->time_of_day().total_microseconds();
173 strm << std::setfill(
'0') << std::setw(4) << date.year() <<
'-' 174 << std::setfill(
'0') << std::setw(2) << date.month().as_number() <<
'-' 175 << std::setfill(
'0') << std::setw(2) << date.day() <<
' ' 191 namespace expr = boost::log::expressions;
192 namespace keywords = boost::log::keywords;
193 auto core = boost::log::core::get();
195 core->add_global_attribute(
"Scopes", boost::log::attributes::named_scope());
196 core->add_global_attribute(
197 "ThreadID", boost::log::attributes::current_thread_id());
198 core->add_global_attribute(
199 "TimeStamp", boost::log::attributes::local_clock());
200 core->add_global_attribute(
201 "Transaction", boost::log::attributes::make_function(&
jb::log::tid));
202 core->add_global_attribute(
203 "MinSeverity", boost::log::attributes::constant<jb::severity_level>(
209 auto sink = boost::log::add_console_log(std::clog);
211 sink->set_filter([cfg](::boost::log::attribute_value_set
const& attr) {
218 auto sink = boost::log::add_file_log(
219 keywords::file_name = filename_format,
220 keywords::time_based_rotation =
221 boost::log::sinks::file::rotation_at_time_point(0, 0, 0));
223 sink->locked_backend()->set_file_collector(
224 boost::log::sinks::file::make_collector(
228 sink->locked_backend()->scan_for_files();
void format_logfile(boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
Define defaults for program parameters.
jb::config_attribute< config, bool > enable_file_logging
#define JB_LOG_DEFAULTS_logfile_archive_directory
severity_level
Severity levels for JayBeams, based on syslog.
jb::severity_level minimum_console_severity
jb::config_attribute< config, std::string > logfile_basename
void validate() const override
Validate the settings.
Configuration object for the logging functions.
jb::config_attribute< config, std::string > logfile_archive_directory
#define JB_LOG_DEFAULTS_minimum_free_space
#define JB_LOG_DEFAULTS_minimum_console_severity
Helper class to print time durations in a HHMMSS.UUUUUU format.
std::string logfile_archive_directory
jb::config_attribute< config, long > maximum_size_archived
bool enable_console_logging
#define JB_LOG_DEFAULTS_logfile_suffix
void init(config const &cfg)
Initialize the logging functions using the configuration provided.
#define JB_LOG_DEFAULTS_enable_console_logging
jb::config_attribute< config, std::string > logfile_suffix
long maximum_size_archived
jb::config_attribute< config, jb::severity_level > minimum_console_severity
#define JB_ASSERT_THROW(PRED)
#define JB_LOG_DEFAULTS_maximum_size_archived
A simple class to communicate the result of parsing the options.
jb::config_attribute< config, jb::severity_level > minimum_severity
std::string logfile_suffix
boost::log::attributes::local_clock::value_type bool filter_predicate(::boost::log::attribute_value_set const &attr)
void format_common(boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
void format_console(boost::log::record_view const &rec, boost::log::formatting_ostream &strm)
int severity_level_width()
Return the recommended with for printing security levels.
jb::severity_level minimum_severity
jb::config_attribute< config, bool > enable_console_logging
#define JB_LOG_DEFAULTS_minimum_severity
BOOST_LOG_ATTRIBUTE_KEYWORD(current_thread_id, "ThreadID", boost::log::attributes::current_thread_id::value_type) BOOST_LOG_ATTRIBUTE_KEYWORD(local_time
#define JB_LOG_DEFAULTS_enable_file_logging
jb::config_attribute< config, long > minimum_free_space
The top-level namespace for the JayBeams library.