util_setup_settings.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright Andrey Semashev 2019.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * https://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <cstddef>
  8. #include <string>
  9. #include <iostream>
  10. #include <boost/log/sources/severity_logger.hpp>
  11. #include <boost/log/sources/record_ostream.hpp>
  12. #include <boost/log/utility/setup/common_attributes.hpp>
  13. #include <boost/log/utility/setup/settings.hpp>
  14. #include <boost/log/utility/setup/from_settings.hpp>
  15. #include <boost/log/utility/setup/filter_parser.hpp>
  16. #include <boost/log/utility/setup/formatter_parser.hpp>
  17. namespace logging = boost::log;
  18. namespace src = boost::log::sources;
  19. // Let's define our own severity levels
  20. enum severity_level
  21. {
  22. normal,
  23. notification,
  24. warning,
  25. error,
  26. critical
  27. };
  28. const char* const severity_strings[] =
  29. {
  30. "normal",
  31. "notification",
  32. "warning",
  33. "error",
  34. "critical"
  35. };
  36. // The operator puts a human-friendly representation of the severity level to the stream
  37. std::ostream& operator<< (std::ostream& strm, severity_level level)
  38. {
  39. if (static_cast< std::size_t >(level) < sizeof(severity_strings) / sizeof(*severity_strings))
  40. strm << severity_strings[level];
  41. else
  42. strm << static_cast< int >(level);
  43. return strm;
  44. }
  45. // The operator parses the severity level from the stream
  46. std::istream& operator>> (std::istream& strm, severity_level& level)
  47. {
  48. if (strm.good())
  49. {
  50. std::string str;
  51. strm >> str;
  52. for (unsigned int i = 0; i < sizeof(severity_strings) / sizeof(*severity_strings); ++i)
  53. {
  54. if (str == severity_strings[i])
  55. {
  56. level = static_cast< severity_level >(i);
  57. return strm;
  58. }
  59. }
  60. strm.setstate(std::ios_base::failbit);
  61. }
  62. return strm;
  63. }
  64. void init_logging()
  65. {
  66. // Before initializing the library from settings, we need to register any custom filter and formatter factories
  67. logging::register_simple_filter_factory< severity_level >("Severity");
  68. logging::register_simple_formatter_factory< severity_level, char >("Severity");
  69. //[ example_util_setup_settings
  70. logging::settings setts;
  71. setts["Core"]["Filter"] = "%Severity% >= warning";
  72. setts["Core"]["DisableLogging"] = false;
  73. // Subsections can be referred to with a single path
  74. setts["Sinks.Console"]["Destination"] = "Console";
  75. setts["Sinks.Console"]["Filter"] = "%Severity% >= critical";
  76. setts["Sinks.Console"]["Format"] = "%TimeStamp% [%Severity%] %Message%";
  77. setts["Sinks.Console"]["AutoFlush"] = true;
  78. // ...as well as the individual parameters
  79. setts["Sinks.File.Destination"] = "TextFile";
  80. setts["Sinks.File.FileName"] = "MyApp_%3N.log";
  81. setts["Sinks.File.AutoFlush"] = true;
  82. setts["Sinks.File.RotationSize"] = 10 * 1024 * 1024; // 10 MiB
  83. setts["Sinks.File.Format"] = "%TimeStamp% [%Severity%] %Message%";
  84. logging::init_from_settings(setts);
  85. //]
  86. // Add attributes
  87. logging::add_common_attributes();
  88. }
  89. int main(int, char*[])
  90. {
  91. init_logging();
  92. src::severity_logger< severity_level > lg;
  93. BOOST_LOG_SEV(lg, normal) << "A regular message";
  94. BOOST_LOG_SEV(lg, warning) << "Something bad is going on but I can handle it";
  95. BOOST_LOG_SEV(lg, critical) << "Everything crumbles, shoot me now!";
  96. return 0;
  97. }