This time, I will use sink directly without the help of add_file_log function.
This example shows how to format log, register sink into core, write log to local file. Full example code is below:#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.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/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/thread/thread.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void InitLog() {
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 >("sign.log"));
sink->set_formatter (
expr::format("[%1%]<%2%>(%3%): %4%")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
% logging::trivial::severity
% expr::attr< boost::thread::id >("ThreadID")
% expr::smessage
);
logging::core::get()->add_sink(sink);
logging::add_common_attributes();
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
}
int main(int, char*[]) {
InitLog();
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;
}
The output is
[2013-12-06 18:02:28]<trace>(): A trace severity message
[2013-12-06 18:02:28]<debug>(): A debug severity message
[2013-12-06 18:02:28]<info>(): An informational severity message
[2013-12-06 18:02:28]<warning>(): A warning severity message
[2013-12-06 18:02:28]<error>(): An error severity message
[2013-12-06 18:02:28]<fatal>(): A fatal severity message
my build.lsp code is here
#!/usr/bin/newlisp
(if (file? "main")
(delete-file "main"))
(exec "c++ -g -std=c++11 -Wall -DBOOST_LOG_DYN_LINK -o ./main.o -c ./main.cc")
(exec "c++ -g -std=c++11 ./main.o -o main -rdynamic -lpthread -lboost_log -lboost_system -lboost_thread -lboost_filesystem")
(exit)
No comments:
Post a Comment