74 lines
2.4 KiB
C++
74 lines
2.4 KiB
C++
/*
|
|
* 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 <ostream>
|
|
#include <fstream>
|
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
|
#include <boost/smart_ptr/make_shared_object.hpp>
|
|
#include <boost/optional.hpp>
|
|
#include <boost/log/core.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <boost/log/expressions.hpp>
|
|
#include <boost/log/sinks/sync_frontend.hpp>
|
|
#include <boost/log/sinks/text_ostream_backend.hpp>
|
|
#include <boost/log/sources/severity_logger.hpp>
|
|
#include <boost/log/sources/record_ostream.hpp>
|
|
#include <boost/log/utility/formatting_ostream.hpp>
|
|
#include <boost/log/utility/setup/common_attributes.hpp>
|
|
#include <boost/log/attributes/value_extraction.hpp>
|
|
|
|
namespace logging = boost::log;
|
|
namespace src = boost::log::sources;
|
|
namespace expr = boost::log::expressions;
|
|
namespace sinks = boost::log::sinks;
|
|
|
|
//[ example_tutorial_formatters_custom
|
|
void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
|
|
{
|
|
// Get the LineID attribute value and put it into the stream
|
|
strm << logging::extract< unsigned int >("LineID", rec) << ": ";
|
|
|
|
// The same for the severity level.
|
|
// The simplified syntax is possible if attribute keywords are used.
|
|
strm << "<" << rec[logging::trivial::severity] << "> ";
|
|
|
|
// Finally, put the record message to the stream
|
|
strm << rec[expr::smessage];
|
|
}
|
|
|
|
void init()
|
|
{
|
|
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 >("sample.log"));
|
|
|
|
sink->set_formatter(&my_formatter);
|
|
|
|
logging::core::get()->add_sink(sink);
|
|
}
|
|
//]
|
|
|
|
int main(int, char*[])
|
|
{
|
|
init();
|
|
logging::add_common_attributes();
|
|
|
|
using namespace logging::trivial;
|
|
src::severity_logger< severity_level > lg;
|
|
|
|
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
|
|
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
|
|
BOOST_LOG_SEV(lg, info) << "An informational severity message";
|
|
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
|
|
BOOST_LOG_SEV(lg, error) << "An error severity message";
|
|
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
|
|
|
|
return 0;
|
|
}
|