main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 12.05.2010
  11. *
  12. * \brief An example of initializing the library from a settings file,
  13. * with custom filter and formatter factories for attributes.
  14. */
  15. // #define BOOST_ALL_DYN_LINK 1
  16. #include <string>
  17. #include <iostream>
  18. #include <fstream>
  19. #include <stdexcept>
  20. #include <boost/ref.hpp>
  21. #include <boost/bind.hpp>
  22. #include <boost/smart_ptr/make_shared_object.hpp>
  23. #include <boost/log/core.hpp>
  24. #include <boost/log/common.hpp>
  25. #include <boost/log/attributes.hpp>
  26. #include <boost/log/core/record.hpp>
  27. #include <boost/log/attributes/value_visitation.hpp>
  28. #include <boost/log/utility/setup/from_stream.hpp>
  29. #include <boost/log/utility/setup/common_attributes.hpp>
  30. #include <boost/log/utility/setup/filter_parser.hpp>
  31. #include <boost/log/utility/setup/formatter_parser.hpp>
  32. namespace logging = boost::log;
  33. namespace attrs = boost::log::attributes;
  34. namespace src = boost::log::sources;
  35. //! Enum for our custom severity levels
  36. enum severity_level
  37. {
  38. normal,
  39. notification,
  40. warning,
  41. error,
  42. critical
  43. };
  44. //! Formatting operator for severity levels
  45. inline std::ostream& operator<< (std::ostream& strm, severity_level level)
  46. {
  47. switch (level)
  48. {
  49. case normal:
  50. strm << "normal";
  51. break;
  52. case notification:
  53. strm << "notification";
  54. break;
  55. case warning:
  56. strm << "warning";
  57. break;
  58. case error:
  59. strm << "error";
  60. break;
  61. case critical:
  62. strm << "critical";
  63. break;
  64. default:
  65. strm << static_cast< int >(level);
  66. break;
  67. }
  68. return strm;
  69. }
  70. //! Parsing operator for severity levels
  71. inline std::istream& operator>> (std::istream& strm, severity_level& level)
  72. {
  73. if (strm.good())
  74. {
  75. std::string str;
  76. strm >> str;
  77. if (str == "normal")
  78. level = normal;
  79. else if (str == "notification")
  80. level = notification;
  81. else if (str == "warning")
  82. level = warning;
  83. else if (str == "error")
  84. level = error;
  85. else if (str == "critical")
  86. level = critical;
  87. else
  88. strm.setstate(std::ios_base::failbit);
  89. }
  90. return strm;
  91. }
  92. //! Our custom formatter for the scope list
  93. struct scope_list_formatter
  94. {
  95. typedef void result_type;
  96. typedef attrs::named_scope::value_type scope_stack;
  97. explicit scope_list_formatter(logging::attribute_name const& name) :
  98. name_(name)
  99. {
  100. }
  101. void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const
  102. {
  103. // We need to acquire the attribute value from the log record
  104. logging::visit< scope_stack >
  105. (
  106. name_,
  107. rec.attribute_values(),
  108. boost::bind(&scope_list_formatter::format, _1, boost::ref(strm))
  109. );
  110. }
  111. private:
  112. //! This is where our custom formatting takes place
  113. static void format(scope_stack const& scopes, logging::formatting_ostream& strm)
  114. {
  115. scope_stack::const_iterator it = scopes.begin(), end = scopes.end();
  116. for (; it != end; ++it)
  117. {
  118. strm << "\t" << it->scope_name << " [" << it->file_name << ":" << it->line << "]\n";
  119. }
  120. }
  121. private:
  122. logging::attribute_name name_;
  123. };
  124. class my_scopes_formatter_factory :
  125. public logging::formatter_factory< char >
  126. {
  127. public:
  128. /*!
  129. * This function creates a formatter for the MyScopes attribute.
  130. * It effectively associates the attribute with the scope_list_formatter class
  131. */
  132. formatter_type create_formatter(
  133. logging::attribute_name const& attr_name, args_map const& args)
  134. {
  135. return formatter_type(scope_list_formatter(attr_name));
  136. }
  137. };
  138. //! The function initializes the logging library
  139. void init_logging()
  140. {
  141. // First thing - register the custom formatter for MyScopes
  142. logging::register_formatter_factory("MyScopes", boost::make_shared< my_scopes_formatter_factory >());
  143. // Also register filter and formatter factories for our custom severity level enum. Since our operator<< and operator>> implement
  144. // all required behavior, simple factories provided by Boost.Log will do.
  145. logging::register_simple_filter_factory< severity_level >("Severity");
  146. logging::register_simple_formatter_factory< severity_level, char >("Severity");
  147. // Then load the settings from the file
  148. std::ifstream settings("settings.txt");
  149. if (!settings.is_open())
  150. throw std::runtime_error("Could not open settings.txt file");
  151. logging::init_from_stream(settings);
  152. // Add some attributes. Note that severity level will be provided by the logger, so we don't need to add it here.
  153. logging::add_common_attributes();
  154. logging::core::get()->add_global_attribute("MyScopes", attrs::named_scope());
  155. }
  156. //! Global logger, which we will use to write log messages
  157. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, src::severity_logger< severity_level >)
  158. //! The function tests logging
  159. void try_logging()
  160. {
  161. BOOST_LOG_FUNCTION();
  162. src::severity_logger< severity_level >& lg = test_lg::get();
  163. BOOST_LOG_SEV(lg, critical) << "This is a critical severity record";
  164. BOOST_LOG_NAMED_SCOPE("random name");
  165. BOOST_LOG_SEV(lg, error) << "This is a error severity record";
  166. }
  167. int main(int argc, char* argv[])
  168. {
  169. try
  170. {
  171. init_logging();
  172. try_logging();
  173. }
  174. catch (std::exception& e)
  175. {
  176. std::cout << "FAILURE: " << e.what() << std::endl;
  177. return -1;
  178. }
  179. return 0;
  180. }