sinks_file.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <string>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/log/core.hpp>
  12. #include <boost/log/sinks/sync_frontend.hpp>
  13. #include <boost/log/sinks/text_file_backend.hpp>
  14. #include <boost/log/sources/severity_channel_logger.hpp>
  15. #include <boost/log/sources/record_ostream.hpp>
  16. namespace logging = boost::log;
  17. namespace src = boost::log::sources;
  18. namespace sinks = boost::log::sinks;
  19. namespace keywords = boost::log::keywords;
  20. //[ example_sinks_file
  21. void init_logging()
  22. {
  23. boost::shared_ptr< logging::core > core = logging::core::get();
  24. boost::shared_ptr< sinks::text_file_backend > backend =
  25. boost::make_shared< sinks::text_file_backend >(
  26. keywords::file_name = "file.log", /*< active file name pattern >*/
  27. keywords::target_file_name = "file_%5N.log", /*< target file name pattern >*/
  28. keywords::rotation_size = 5 * 1024 * 1024, /*< rotate the file upon reaching 5 MiB size... >*/
  29. keywords::time_based_rotation = sinks::file::rotation_at_time_point(12, 0, 0) /*< ...or every day, at noon, whichever comes first >*/
  30. );
  31. // Wrap it into the frontend and register in the core.
  32. // The backend requires synchronization in the frontend.
  33. typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
  34. boost::shared_ptr< sink_t > sink(new sink_t(backend));
  35. core->add_sink(sink);
  36. }
  37. //]
  38. int main(int, char*[])
  39. {
  40. init_logging();
  41. src::severity_channel_logger< > lg(keywords::channel = "net");
  42. BOOST_LOG_SEV(lg, 3) << "Hello world!";
  43. return 0;
  44. }