如何使用此代码打印时间,日期,当前功能? [英] How to print time, date, current function using this code?
本文介绍了如何使用此代码打印时间,日期,当前功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有示例Boost Logger代码,当执行此代码时,我得到这样的输出
输出:
1:60:line_number.cpp:< debug>
如何得到这样的输出:
执行输出:
1:< time,date> line_number.cpp:< current_function> < debug>
1:2014-Jul-15 10:47:26.137959 line_number.cpp:Main.cpp< debug>
任何人都可以帮助我,我是新手来提升记录器。
我尝试过:
I had sample Boost Logger code,when Execute this code, I got output like this
output:
1: 60: line_number.cpp: <debug>
How to get output like this:
Execpting output:
1:<time,date> line_number.cpp: <current_function> <debug>
1:2014-Jul-15 10:47:26.137959 line_number.cpp: Main.cpp <debug>
Can anyone help me,I am new to boost logger.
What I have tried:
#include <ostream>
#include <fstream>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/manipulators/add_value.hpp>
#include <boost/filesystem.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
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) << ": ";
strm << logging::extract< unsigned int >("TimeStamp", rec) << ": ";
strm << logging::extract< int >("Line", rec) << ": ";
logging::value_ref< std::string > fullpath = logging::extract< std::string >("File", rec);
strm << boost::filesystem::path(fullpath.get()).filename().string() << ": ";
// 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 >("sample2333.log"));
sink->set_formatter(&my_formatter);
logging::core::get()->add_sink(sink);
}
#define MY_GLOBAL_LOGGER(log_,sv) BOOST_LOG_SEV( log_, sv) \
<< boost::log::add_value("Line", __LINE__) \
<< boost::log::add_value("File", __FILE__) \
<< boost::log::add_value("Function", BOOST_CURRENT_FUNCTION) \
int main(int, char*[])
{
init();
logging::add_common_attributes();
using namespace logging::trivial;
src::severity_logger< severity_level > lg;
MY_GLOBAL_LOGGER(lg,debug) << "Sample";
MY_GLOBAL_LOGGER(lg,info) << "Critical";
MY_GLOBAL_LOGGER(lg,warning) << "Simple";
MY_GLOBAL_LOGGER(lg,error) << "Hello";
system("pause");
return 0;
}
推荐答案
使用时间函数(Windows) [ ^ ],并将结果添加到您的日志记录中。
Use whichever of the Time Functions (Windows)[^] you need, and add the result to your logging.
这篇关于如何使用此代码打印时间,日期,当前功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文