gpt4 book ai didi

c++ - 不确定我在 boost 日志中的线程 ID 是否正确?

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:41 27 4
gpt4 key购买 nike

下面是我的完整测试代码,我打印了当前的线程id,但是看起来很长,对吗?而且因为我正在使用 BOOST_LOG_SCOPED_THREAD_TAG,所以我可以在其他线程中记录线程 ID 吗?

#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->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("csv.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::log::attributes::current_thread_id::value_type >("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;
}

测试应用程序生成两个日志文件:sign.log 和 csv.log,它们具有相同的内容,如下所示:

[2013-12-23 23:38:47]<trace>(0x00007f1f8a173740): A trace severity message
[2013-12-23 23:38:47]<debug>(0x00007f1f8a173740): A debug severity message
[2013-12-23 23:38:47]<info>(0x00007f1f8a173740): An informational severity message
[2013-12-23 23:38:47]<warning>(0x00007f1f8a173740): A warning severity message
[2013-12-23 23:38:47]<error>(0x00007f1f8a173740): An error severity message
[2013-12-23 23:38:47]<fatal>(0x00007f1f8a173740): A fatal severity message

最佳答案

//! This function is executed in multiple threads
void thread_fun(boost::barrier& bar)
{

// Here we go. First, identify the thread.
BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());

// Now, do some logging
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
{
BOOST_LOG(test_lg::get()) << "Log record " << i;
}
}

参见:http://www.boost.org/doc/libs/1_55_0/libs/log/example/bounded_async_log/main.cpp

关于c++ - 不确定我在 boost 日志中的线程 ID 是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20746709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com