main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /*!
  8. * \file main.cpp
  9. * \author Andrey Semashev
  10. * \date 26.04.2008
  11. *
  12. * \brief An example of logging into a rotating text file.
  13. * See the library tutorial for expanded comments on this code.
  14. * It may also be worthwhile reading the Wiki requirements page:
  15. * http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Logging
  16. */
  17. // #define BOOST_LOG_DYN_LINK 1
  18. #include <stdexcept>
  19. #include <string>
  20. #include <iostream>
  21. #include <boost/smart_ptr/shared_ptr.hpp>
  22. #include <boost/date_time/posix_time/posix_time.hpp>
  23. #include <boost/log/common.hpp>
  24. #include <boost/log/expressions.hpp>
  25. #include <boost/log/attributes.hpp>
  26. #include <boost/log/sources/logger.hpp>
  27. #include <boost/log/sinks/sync_frontend.hpp>
  28. #include <boost/log/sinks/text_file_backend.hpp>
  29. namespace logging = boost::log;
  30. namespace attrs = boost::log::attributes;
  31. namespace src = boost::log::sources;
  32. namespace sinks = boost::log::sinks;
  33. namespace expr = boost::log::expressions;
  34. namespace keywords = boost::log::keywords;
  35. using boost::shared_ptr;
  36. enum { LOG_RECORDS_TO_WRITE = 10000 };
  37. int main(int argc, char* argv[])
  38. {
  39. try
  40. {
  41. // Create a text file sink
  42. typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
  43. shared_ptr< file_sink > sink(new file_sink(
  44. keywords::file_name = "file.log", // file name pattern
  45. keywords::target_file_name = "%Y%m%d_%H%M%S_%5N.log", // file name pattern
  46. keywords::rotation_size = 16384 // rotation size, in characters
  47. ));
  48. // Set up where the rotated files will be stored
  49. sink->locked_backend()->set_file_collector(sinks::file::make_collector(
  50. keywords::target = "logs", // where to store rotated files
  51. keywords::max_size = 16 * 1024 * 1024, // maximum total size of the stored files, in bytes
  52. keywords::min_free_space = 100 * 1024 * 1024, // minimum free space on the drive, in bytes
  53. keywords::max_files = 512 // maximum number of stored files
  54. ));
  55. // Upon restart, scan the target directory for files matching the file_name pattern
  56. sink->locked_backend()->scan_for_files();
  57. sink->set_formatter
  58. (
  59. expr::format("%1%: [%2%] - %3%")
  60. % expr::attr< unsigned int >("RecordID")
  61. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  62. % expr::smessage
  63. );
  64. // Add it to the core
  65. logging::core::get()->add_sink(sink);
  66. // Add some attributes too
  67. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  68. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  69. // Do some logging
  70. src::logger lg;
  71. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  72. {
  73. BOOST_LOG(lg) << "Some log record";
  74. }
  75. return 0;
  76. }
  77. catch (std::exception& e)
  78. {
  79. std::cout << "FAILURE: " << e.what() << std::endl;
  80. return 1;
  81. }
  82. }