mirror of
https://github.com/boostorg/log.git
synced 2026-01-19 16:32:09 +00:00
This replaces boost::thread and most mutexes and condition variables with std equivalents. It also adds support for std lock types to the strictest_lock type trait. This significantly, although not completely, reduces the dependency on Boost.Thread. Refs https://github.com/boostorg/log/issues/232.
115 lines
3.4 KiB
C++
115 lines
3.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)
|
|
*/
|
|
/*!
|
|
* \file main.cpp
|
|
* \author Andrey Semashev
|
|
* \date 10.06.2008
|
|
*
|
|
* \brief An example of logging in multiple threads.
|
|
* See the library tutorial for expanded comments on this code.
|
|
* It may also be worthwhile reading the Wiki requirements page:
|
|
* http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
|
|
*/
|
|
|
|
// #define BOOST_LOG_DYN_LINK 1
|
|
|
|
#include <stdexcept>
|
|
#include <thread>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <boost/compat/latch.hpp>
|
|
|
|
#include <boost/log/common.hpp>
|
|
#include <boost/log/expressions.hpp>
|
|
#include <boost/log/attributes.hpp>
|
|
#include <boost/log/sinks.hpp>
|
|
#include <boost/log/sources/logger.hpp>
|
|
|
|
namespace logging = boost::log;
|
|
namespace attrs = boost::log::attributes;
|
|
namespace src = boost::log::sources;
|
|
namespace sinks = boost::log::sinks;
|
|
namespace expr = boost::log::expressions;
|
|
namespace keywords = boost::log::keywords;
|
|
|
|
using boost::shared_ptr;
|
|
|
|
enum
|
|
{
|
|
LOG_RECORDS_TO_WRITE = 10000,
|
|
THREAD_COUNT = 2
|
|
};
|
|
|
|
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::logger_mt)
|
|
|
|
//! This function is executed in multiple threads
|
|
void thread_fun(boost::compat::latch& latch)
|
|
{
|
|
// Wait until all threads are created
|
|
latch.arrive_and_wait();
|
|
|
|
// Now, do some logging
|
|
for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
|
|
{
|
|
BOOST_LOG(test_lg::get()) << "Log record " << i;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
try
|
|
{
|
|
// Open a rotating text file
|
|
shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
|
|
if (!strm->good())
|
|
throw std::runtime_error("Failed to open a text log file");
|
|
|
|
// Create a text file sink
|
|
shared_ptr< sinks::synchronous_sink< sinks::text_ostream_backend > > sink(
|
|
new sinks::synchronous_sink< sinks::text_ostream_backend >);
|
|
|
|
sink->locked_backend()->add_stream(strm);
|
|
|
|
sink->set_formatter
|
|
(
|
|
expr::format("%1%: [%2%] [%3%] - %4%")
|
|
% expr::attr< unsigned int >("RecordID")
|
|
% expr::attr< boost::posix_time::ptime >("TimeStamp")
|
|
% expr::attr< attrs::current_thread_id::value_type >("ThreadID")
|
|
% expr::smessage
|
|
);
|
|
|
|
// Add it to the core
|
|
logging::core::get()->add_sink(sink);
|
|
|
|
// Add some attributes too
|
|
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
|
|
logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
|
|
logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
|
|
|
|
// Create logging threads
|
|
boost::compat::latch latch(THREAD_COUNT);
|
|
std::thread threads[THREAD_COUNT];
|
|
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
|
|
threads[i] = std::thread([&latch]() { thread_fun(latch); });
|
|
|
|
// Wait until all action ends
|
|
for (unsigned int i = 0; i < THREAD_COUNT; ++i)
|
|
threads[i].join();
|
|
|
|
return 0;
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cout << "FAILURE: " << e.what() << std::endl;
|
|
return 1;
|
|
}
|
|
}
|