| /* |
| * Copyright Andrey Semashev 2007 - 2015. |
| * Distributed under the Boost Software License, Version 1.0. |
| * (See accompanying file LICENSE_1_0.txt or copy at |
| * http://www.boost.org/LICENSE_1_0.txt) |
| */ |
| |
| #include <cstddef> |
| #include <string> |
| #include <ostream> |
| #include <fstream> |
| #include <iomanip> |
| #include <boost/smart_ptr/shared_ptr.hpp> |
| #include <boost/smart_ptr/make_shared_object.hpp> |
| #include <boost/phoenix/bind.hpp> |
| #include <boost/date_time/posix_time/posix_time.hpp> |
| #include <boost/log/core.hpp> |
| #include <boost/log/expressions.hpp> |
| #include <boost/log/attributes.hpp> |
| #include <boost/log/sources/basic_logger.hpp> |
| #include <boost/log/sources/severity_logger.hpp> |
| #include <boost/log/sources/severity_channel_logger.hpp> |
| #include <boost/log/sources/record_ostream.hpp> |
| #include <boost/log/sinks/sync_frontend.hpp> |
| #include <boost/log/sinks/text_ostream_backend.hpp> |
| #include <boost/log/attributes/scoped_attribute.hpp> |
| #include <boost/log/utility/value_ref.hpp> |
| #include <boost/log/utility/setup/common_attributes.hpp> |
| |
| namespace logging = boost::log; |
| namespace src = boost::log::sources; |
| namespace expr = boost::log::expressions; |
| namespace sinks = boost::log::sinks; |
| namespace attrs = boost::log::attributes; |
| namespace keywords = boost::log::keywords; |
| |
| // We define our own severity levels |
| enum severity_level |
| { |
| normal, |
| notification, |
| warning, |
| error, |
| critical |
| }; |
| |
| // The operator puts a human-friendly representation of the severity level to the stream |
| std::ostream& operator<< (std::ostream& strm, severity_level level) |
| { |
| static const char* strings[] = |
| { |
| "normal", |
| "notification", |
| "warning", |
| "error", |
| "critical" |
| }; |
| |
| if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings)) |
| strm << strings[level]; |
| else |
| strm << static_cast< int >(level); |
| |
| return strm; |
| } |
| |
| //[ example_tutorial_filtering |
| BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int) |
| BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level) |
| BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string) |
| |
| void init() |
| { |
| // Setup the common formatter for all sinks |
| logging::formatter fmt = expr::stream |
| << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ') |
| << ": <" << severity << ">\t" |
| << expr::if_(expr::has_attr(tag_attr)) |
| [ |
| expr::stream << "[" << tag_attr << "] " |
| ] |
| << expr::smessage; |
| |
| // Initialize sinks |
| typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; |
| boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); |
| |
| sink->locked_backend()->add_stream( |
| boost::make_shared< std::ofstream >("full.log")); |
| |
| sink->set_formatter(fmt); |
| |
| logging::core::get()->add_sink(sink); |
| |
| sink = boost::make_shared< text_sink >(); |
| |
| sink->locked_backend()->add_stream( |
| boost::make_shared< std::ofstream >("important.log")); |
| |
| sink->set_formatter(fmt); |
| |
| sink->set_filter(severity >= warning || (expr::has_attr(tag_attr) && tag_attr == "IMPORTANT_MESSAGE")); |
| |
| logging::core::get()->add_sink(sink); |
| |
| // Add attributes |
| logging::add_common_attributes(); |
| } |
| //] |
| |
| #if 0 |
| |
| //[ example_tutorial_filtering_bind |
| bool my_filter(logging::value_ref< severity_level, tag::severity > const& level, |
| logging::value_ref< std::string, tag::tag_attr > const& tag) |
| { |
| return level >= warning || tag == "IMPORTANT_MESSAGE"; |
| } |
| |
| void init() |
| { |
| //<- |
| |
| // Setup the common formatter for all sinks |
| logging::formatter fmt = expr::stream |
| << std::setw(6) << std::setfill('0') << line_id << std::setfill(' ') |
| << ": <" << severity << ">\t" |
| << expr::if_(expr::has_attr(tag_attr)) |
| [ |
| expr::stream << "[" << tag_attr << "] " |
| ] |
| << expr::smessage; |
| |
| // Initialize sinks |
| typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; |
| boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); |
| |
| sink->locked_backend()->add_stream( |
| boost::make_shared< std::ofstream >("full.log")); |
| |
| sink->set_formatter(fmt); |
| |
| logging::core::get()->add_sink(sink); |
| |
| sink = boost::make_shared< text_sink >(); |
| |
| sink->locked_backend()->add_stream( |
| boost::make_shared< std::ofstream >("important.log")); |
| |
| sink->set_formatter(fmt); |
| |
| //-> |
| // ... |
| |
| namespace phoenix = boost::phoenix; |
| sink->set_filter(phoenix::bind(&my_filter, severity.or_none(), tag_attr.or_none())); |
| |
| // ... |
| //<- |
| |
| logging::core::get()->add_sink(sink); |
| |
| // Add attributes |
| logging::add_common_attributes(); |
| |
| //-> |
| } |
| //] |
| |
| #endif |
| |
| void logging_function() |
| { |
| src::severity_logger< severity_level > slg; |
| |
| BOOST_LOG_SEV(slg, normal) << "A regular message"; |
| BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it"; |
| BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!"; |
| |
| { |
| BOOST_LOG_SCOPED_THREAD_TAG("Tag", "IMPORTANT_MESSAGE"); |
| BOOST_LOG_SEV(slg, normal) << "An important message"; |
| } |
| } |
| |
| int main(int, char*[]) |
| { |
| init(); |
| |
| logging_function(); |
| |
| return 0; |
| } |