156 lines
4.3 KiB
C++
156 lines
4.3 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 <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/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/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/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
|
|
};
|
|
|
|
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)
|
|
BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope", attrs::named_scope::value_type)
|
|
BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type)
|
|
|
|
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!";
|
|
}
|
|
|
|
//[ example_tutorial_attributes_named_scope
|
|
void named_scope_logging()
|
|
{
|
|
BOOST_LOG_NAMED_SCOPE("named_scope_logging");
|
|
|
|
src::severity_logger< severity_level > slg;
|
|
|
|
BOOST_LOG_SEV(slg, normal) << "Hello from the function named_scope_logging!";
|
|
}
|
|
//]
|
|
|
|
//[ example_tutorial_attributes_tagged_logging
|
|
void tagged_logging()
|
|
{
|
|
src::severity_logger< severity_level > slg;
|
|
slg.add_attribute("Tag", attrs::constant< std::string >("My tag value"));
|
|
|
|
BOOST_LOG_SEV(slg, normal) << "Here goes the tagged record";
|
|
}
|
|
//]
|
|
|
|
//[ example_tutorial_attributes_timed_logging
|
|
void timed_logging()
|
|
{
|
|
BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
|
|
|
|
src::severity_logger< severity_level > slg;
|
|
BOOST_LOG_SEV(slg, normal) << "Starting to time nested functions";
|
|
|
|
logging_function();
|
|
|
|
BOOST_LOG_SEV(slg, normal) << "Stopping to time nested functions";
|
|
}
|
|
//]
|
|
|
|
// 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;
|
|
}
|
|
|
|
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
|
|
(
|
|
expr::stream
|
|
<< std::hex << std::setw(8) << std::setfill('0') << line_id << std::dec << std::setfill(' ')
|
|
<< ": <" << severity << ">\t"
|
|
<< "(" << scope << ") "
|
|
<< expr::if_(expr::has_attr(tag_attr))
|
|
[
|
|
expr::stream << "[" << tag_attr << "] "
|
|
]
|
|
<< expr::if_(expr::has_attr(timeline))
|
|
[
|
|
expr::stream << "[" << timeline << "] "
|
|
]
|
|
<< expr::smessage
|
|
);
|
|
|
|
logging::core::get()->add_sink(sink);
|
|
|
|
// Add attributes
|
|
logging::add_common_attributes();
|
|
logging::core::get()->add_global_attribute("Scope", attrs::named_scope());
|
|
}
|
|
|
|
int main(int, char*[])
|
|
{
|
|
init();
|
|
|
|
named_scope_logging();
|
|
tagged_logging();
|
|
timed_logging();
|
|
|
|
return 0;
|
|
}
|