util_stp_formatter_parser.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 setup_formatter_parser.cpp
  9. * \author Andrey Semashev
  10. * \date 25.08.2013
  11. *
  12. * \brief This header contains tests for the formatter parser.
  13. */
  14. #define BOOST_TEST_MODULE setup_formatter_parser
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/utility/setup/formatter_parser.hpp>
  18. #if !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS) && !defined(BOOST_LOG_WITHOUT_DEFAULT_FACTORIES)
  19. #include <boost/smart_ptr/shared_ptr.hpp>
  20. #include <boost/log/exceptions.hpp>
  21. #include <boost/log/core/record.hpp>
  22. #include <boost/log/core/record_view.hpp>
  23. #include <boost/log/attributes/constant.hpp>
  24. #include <boost/log/attributes/attribute_set.hpp>
  25. #include <boost/log/attributes/attribute_value_set.hpp>
  26. #include <boost/log/utility/formatting_ostream.hpp>
  27. #include <boost/log/expressions/formatter.hpp>
  28. #include "make_record.hpp"
  29. namespace logging = boost::log;
  30. namespace attrs = logging::attributes;
  31. typedef logging::attribute_set attr_set;
  32. typedef logging::attribute_value_set attr_values;
  33. typedef logging::record_view record_view;
  34. typedef logging::basic_formatting_ostream< char > osstream;
  35. typedef logging::basic_formatter< char > formatter;
  36. // Tests for simple attribute placeholders in formatters
  37. BOOST_AUTO_TEST_CASE(attr_placeholders)
  38. {
  39. attrs::constant< int > attr1(10);
  40. attrs::constant< std::string > attr2("hello");
  41. attr_set set1;
  42. set1["MyAttr"] = attr1;
  43. set1["MyStr"] = attr2;
  44. record_view rec = make_record_view(set1);
  45. {
  46. formatter f = logging::parse_formatter("String literal");
  47. std::string str1, str2;
  48. osstream strm1(str1), strm2(str2);
  49. f(rec, strm1);
  50. strm2 << "String literal";
  51. BOOST_CHECK_EQUAL(strm1.str(), strm2.str());
  52. }
  53. {
  54. formatter f = logging::parse_formatter("MyAttr: %MyAttr%, MyStr: %MyStr%");
  55. std::string str1, str2;
  56. osstream strm1(str1), strm2(str2);
  57. f(rec, strm1);
  58. strm2 << "MyAttr: " << attr1.get() << ", MyStr: " << attr2.get();
  59. BOOST_CHECK_EQUAL(strm1.str(), strm2.str());
  60. }
  61. {
  62. formatter f = logging::parse_formatter("MyAttr: % MyAttr %, MyStr: % MyStr %");
  63. std::string str1, str2;
  64. osstream strm1(str1), strm2(str2);
  65. f(rec, strm1);
  66. strm2 << "MyAttr: " << attr1.get() << ", MyStr: " << attr2.get();
  67. BOOST_CHECK_EQUAL(strm1.str(), strm2.str());
  68. }
  69. {
  70. formatter f = logging::parse_formatter("%MyAttr%%MyStr%");
  71. std::string str1, str2;
  72. osstream strm1(str1), strm2(str2);
  73. f(rec, strm1);
  74. strm2 << attr1.get() << attr2.get();
  75. BOOST_CHECK_EQUAL(strm1.str(), strm2.str());
  76. }
  77. {
  78. formatter f = logging::parse_formatter("MyAttr: %MyAttr%, MissingAttr: %MissingAttr%");
  79. std::string str1, str2;
  80. osstream strm1(str1), strm2(str2);
  81. f(rec, strm1);
  82. strm2 << "MyAttr: " << attr1.get() << ", MissingAttr: ";
  83. BOOST_CHECK_EQUAL(strm1.str(), strm2.str());
  84. }
  85. }
  86. namespace {
  87. class test_formatter_factory :
  88. public logging::formatter_factory< char >
  89. {
  90. private:
  91. typedef logging::formatter_factory< char > base_type;
  92. public:
  93. typedef base_type::string_type string_type;
  94. typedef base_type::args_map args_map;
  95. typedef base_type::formatter_type formatter_type;
  96. public:
  97. explicit test_formatter_factory(logging::attribute_name const& name) : m_name(name), m_called(false)
  98. {
  99. }
  100. void expect_args(args_map const& args)
  101. {
  102. m_args = args;
  103. }
  104. void check_called()
  105. {
  106. BOOST_CHECK(m_called);
  107. m_called = false;
  108. }
  109. formatter_type create_formatter(logging::attribute_name const& name, args_map const& args)
  110. {
  111. BOOST_CHECK_EQUAL(m_name, name);
  112. BOOST_CHECK_EQUAL(m_args.size(), args.size());
  113. for (args_map::const_iterator it = m_args.begin(), end = m_args.end(); it != end; ++it)
  114. {
  115. args_map::const_iterator parsed_it = args.find(it->first);
  116. if (parsed_it != args.end())
  117. {
  118. BOOST_TEST_CHECKPOINT(("Arg: " + it->first));
  119. BOOST_CHECK_EQUAL(it->second, parsed_it->second);
  120. }
  121. else
  122. {
  123. BOOST_ERROR(("Arg not found: " + it->first));
  124. }
  125. }
  126. m_called = true;
  127. return formatter_type();
  128. }
  129. private:
  130. logging::attribute_name m_name;
  131. args_map m_args;
  132. bool m_called;
  133. };
  134. } // namespace
  135. // Tests for formatter factory
  136. BOOST_AUTO_TEST_CASE(formatter_factory)
  137. {
  138. logging::attribute_name attr_name("MyCustomAttr");
  139. boost::shared_ptr< test_formatter_factory > factory(new test_formatter_factory(attr_name));
  140. logging::register_formatter_factory(attr_name, factory);
  141. {
  142. BOOST_TEST_CHECKPOINT("formatter 1");
  143. test_formatter_factory::args_map args;
  144. factory->expect_args(args);
  145. logging::parse_formatter("Hello: %MyCustomAttr%");
  146. factory->check_called();
  147. }
  148. {
  149. BOOST_TEST_CHECKPOINT("formatter 2");
  150. test_formatter_factory::args_map args;
  151. factory->expect_args(args);
  152. logging::parse_formatter("Hello: %MyCustomAttr()%");
  153. factory->check_called();
  154. }
  155. {
  156. BOOST_TEST_CHECKPOINT("formatter 3");
  157. test_formatter_factory::args_map args;
  158. factory->expect_args(args);
  159. logging::parse_formatter("Hello: %MyCustomAttr ( ) %");
  160. factory->check_called();
  161. }
  162. {
  163. BOOST_TEST_CHECKPOINT("formatter 4");
  164. test_formatter_factory::args_map args;
  165. args["param1"] = "10";
  166. factory->expect_args(args);
  167. logging::parse_formatter("Hello: %MyCustomAttr(param1=10)%");
  168. factory->check_called();
  169. }
  170. {
  171. BOOST_TEST_CHECKPOINT("formatter 5");
  172. test_formatter_factory::args_map args;
  173. args["param1"] = "10";
  174. factory->expect_args(args);
  175. logging::parse_formatter("Hello: % MyCustomAttr ( param1 = 10 ) % ");
  176. factory->check_called();
  177. }
  178. {
  179. BOOST_TEST_CHECKPOINT("formatter 6");
  180. test_formatter_factory::args_map args;
  181. args["param1"] = " 10 ";
  182. factory->expect_args(args);
  183. logging::parse_formatter("Hello: %MyCustomAttr(param1=\" 10 \")%");
  184. factory->check_called();
  185. }
  186. {
  187. BOOST_TEST_CHECKPOINT("formatter 7");
  188. test_formatter_factory::args_map args;
  189. args["param1"] = "10";
  190. args["param2"] = "abcd";
  191. factory->expect_args(args);
  192. logging::parse_formatter("Hello: %MyCustomAttr(param1 = 10, param2 = abcd)%");
  193. factory->check_called();
  194. }
  195. {
  196. BOOST_TEST_CHECKPOINT("formatter 8");
  197. test_formatter_factory::args_map args;
  198. args["param1"] = "10";
  199. args["param2"] = "abcd";
  200. factory->expect_args(args);
  201. logging::parse_formatter("Hello: %MyCustomAttr(param1=10,param2=abcd)%");
  202. factory->check_called();
  203. }
  204. {
  205. BOOST_TEST_CHECKPOINT("formatter 9");
  206. test_formatter_factory::args_map args;
  207. args["param1"] = "10";
  208. args["param2"] = "abcd";
  209. args["param_last"] = "-2.2";
  210. factory->expect_args(args);
  211. logging::parse_formatter("Hello: %MyCustomAttr(param1 = 10, param2 = \"abcd\", param_last = -2.2)%");
  212. factory->check_called();
  213. }
  214. }
  215. // Tests for invalid formatters
  216. BOOST_AUTO_TEST_CASE(invalid)
  217. {
  218. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr"), logging::parse_error);
  219. BOOST_CHECK_THROW(logging::parse_formatter("MyStr%"), logging::parse_error);
  220. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(%"), logging::parse_error);
  221. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr)%"), logging::parse_error);
  222. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param%"), logging::parse_error);
  223. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param=%"), logging::parse_error);
  224. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param)%"), logging::parse_error);
  225. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param=)%"), logging::parse_error);
  226. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(=value)%"), logging::parse_error);
  227. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param,param2)%"), logging::parse_error);
  228. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param=value,)%"), logging::parse_error);
  229. BOOST_CHECK_THROW(logging::parse_formatter("%MyStr(param=value,param2)%"), logging::parse_error);
  230. }
  231. #endif // !defined(BOOST_LOG_WITHOUT_SETTINGS_PARSERS) && !defined(BOOST_LOG_WITHOUT_DEFAULT_FACTORIES)