sinks_xml_file.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <stdexcept>
  8. #include <string>
  9. #include <iostream>
  10. #include <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/lambda/lambda.hpp>
  12. #include <boost/date_time/posix_time/posix_time.hpp>
  13. #include <boost/log/common.hpp>
  14. #include <boost/log/expressions.hpp>
  15. #include <boost/log/attributes.hpp>
  16. #include <boost/log/sinks.hpp>
  17. #include <boost/log/sources/logger.hpp>
  18. namespace logging = boost::log;
  19. namespace attrs = boost::log::attributes;
  20. namespace src = boost::log::sources;
  21. namespace sinks = boost::log::sinks;
  22. namespace expr = boost::log::expressions;
  23. namespace keywords = boost::log::keywords;
  24. typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
  25. //[ example_sinks_xml_file_collecting
  26. void init_file_collecting(boost::shared_ptr< file_sink > sink)
  27. {
  28. sink->locked_backend()->set_file_collector(sinks::file::make_collector(
  29. keywords::target = "logs", /*< the target directory >*/
  30. keywords::max_size = 16 * 1024 * 1024, /*< maximum total size of the stored files, in bytes >*/
  31. keywords::min_free_space = 100 * 1024 * 1024, /*< minimum free space on the drive, in bytes >*/
  32. keywords::max_files = 512 /*< maximum number of stored files >*/
  33. ));
  34. }
  35. //]
  36. #if 0
  37. //[ example_sinks_xml_file
  38. // Complete file sink type
  39. typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
  40. void write_header(sinks::text_file_backend::stream_type& file)
  41. {
  42. file << "<?xml version=\"1.0\"?>\n<log>\n";
  43. }
  44. void write_footer(sinks::text_file_backend::stream_type& file)
  45. {
  46. file << "</log>\n";
  47. }
  48. void init_logging()
  49. {
  50. // Create a text file sink
  51. boost::shared_ptr< file_sink > sink(new file_sink(
  52. keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml", /*< the resulting file name pattern >*/
  53. keywords::rotation_size = 16384 /*< rotation size, in characters >*/
  54. ));
  55. sink->set_formatter
  56. (
  57. expr::format("\t<record id=\"%1%\" timestamp=\"%2%\">%3%</record>")
  58. % expr::attr< unsigned int >("RecordID")
  59. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  60. % expr::xml_decor[ expr::stream << expr::smessage ] /*< the log message has to be decorated, if it contains special characters >*/
  61. );
  62. // Set header and footer writing functors
  63. sink->locked_backend()->set_open_handler(&write_header);
  64. sink->locked_backend()->set_close_handler(&write_footer);
  65. // Add the sink to the core
  66. logging::core::get()->add_sink(sink);
  67. }
  68. //]
  69. #endif
  70. //[ example_sinks_xml_file_final
  71. void init_logging()
  72. {
  73. // Create a text file sink
  74. boost::shared_ptr< file_sink > sink(new file_sink(
  75. keywords::file_name = "%Y%m%d_%H%M%S_%5N.xml",
  76. keywords::rotation_size = 16384
  77. ));
  78. // Set up where the rotated files will be stored
  79. init_file_collecting(sink);
  80. // Upon restart, scan the directory for files matching the file_name pattern
  81. sink->locked_backend()->scan_for_files();
  82. sink->set_formatter
  83. (
  84. expr::format("\t<record id=\"%1%\" timestamp=\"%2%\">%3%</record>")
  85. % expr::attr< unsigned int >("RecordID")
  86. % expr::attr< boost::posix_time::ptime >("TimeStamp")
  87. % expr::xml_decor[ expr::stream << expr::smessage ]
  88. );
  89. // Set header and footer writing functors
  90. namespace bll = boost::lambda;
  91. sink->locked_backend()->set_open_handler
  92. (
  93. bll::_1 << "<?xml version=\"1.0\"?>\n<log>\n"
  94. );
  95. sink->locked_backend()->set_close_handler
  96. (
  97. bll::_1 << "</log>\n"
  98. );
  99. // Add the sink to the core
  100. logging::core::get()->add_sink(sink);
  101. }
  102. //]
  103. enum { LOG_RECORDS_TO_WRITE = 2000 };
  104. int main(int argc, char* argv[])
  105. {
  106. try
  107. {
  108. // Initialize logging library
  109. init_logging();
  110. // And also add some attributes
  111. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  112. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  113. // Do some logging
  114. src::logger lg;
  115. for (unsigned int i = 0; i < LOG_RECORDS_TO_WRITE; ++i)
  116. {
  117. BOOST_LOG(lg) << "XML log record " << i;
  118. }
  119. // Test that XML character decoration works
  120. BOOST_LOG(lg) << "Special XML characters: &, <, >, \", '";
  121. return 0;
  122. }
  123. catch (std::exception& e)
  124. {
  125. std::cout << "FAILURE: " << e.what() << std::endl;
  126. return 1;
  127. }
  128. }