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 form_attr.cpp
9 * \author Andrey Semashev
10 * \date 01.02.2009
11 *
12 * \brief This header contains tests for the \c attr formatter.
13 */
14
15 #define BOOST_TEST_MODULE form_attr
16
17 #include <memory>
18 #include <string>
19 #include <iomanip>
20 #include <ostream>
21 #include <algorithm>
22 #include <boost/test/unit_test.hpp>
23 #include <boost/log/attributes/constant.hpp>
24 #include <boost/log/attributes/attribute_set.hpp>
25 #include <boost/log/utility/type_dispatch/standard_types.hpp>
26 #include <boost/log/utility/formatting_ostream.hpp>
27 #include <boost/log/expressions.hpp>
28 #include <boost/log/core/record.hpp>
29 #include "char_definitions.hpp"
30 #include "make_record.hpp"
31
32 namespace logging = boost::log;
33 namespace attrs = logging::attributes;
34 namespace expr = logging::expressions;
35
36 namespace {
37
38 class my_class
39 {
40 int m_Data;
41
42 public:
my_class(int data)43 explicit my_class(int data) : m_Data(data) {}
44
45 template< typename CharT, typename TraitsT >
operator <<(std::basic_ostream<CharT,TraitsT> & strm,my_class const & obj)46 friend std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_class const& obj)
47 {
48 strm << "[data: " << obj.m_Data << "]";
49 return strm;
50 }
51 };
52
53 } // namespace
54
55 // The test checks that default formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(default_formatting,CharT,char_types)56 BOOST_AUTO_TEST_CASE_TEMPLATE(default_formatting, CharT, char_types)
57 {
58 typedef logging::record_view record_view;
59 typedef logging::attribute_set attr_set;
60 typedef std::basic_string< CharT > string;
61 typedef logging::basic_formatting_ostream< CharT > osstream;
62 typedef logging::basic_formatter< CharT > formatter;
63 typedef test_data< CharT > data;
64
65 attrs::constant< int > attr1(10);
66 attrs::constant< double > attr2(5.5);
67 attrs::constant< my_class > attr3(my_class(77));
68
69 attr_set set1;
70 set1[data::attr1()] = attr1;
71 set1[data::attr2()] = attr2;
72 set1[data::attr3()] = attr3;
73
74 record_view rec = make_record_view(set1);
75
76 // Check for various modes of attribute value type specification
77 {
78 string str1, str2;
79 osstream strm1(str1), strm2(str2);
80 formatter f = expr::stream << expr::attr< int >(data::attr1()) << expr::attr< double >(data::attr2());
81 f(rec, strm1);
82 strm2 << 10 << 5.5;
83 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
84 }
85 {
86 string str1, str2;
87 osstream strm1(str1), strm2(str2);
88 formatter f = expr::stream << expr::attr< logging::numeric_types >(data::attr1()) << expr::attr< logging::numeric_types >(data::attr2());
89 f(rec, strm1);
90 strm2 << 10 << 5.5;
91 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
92 }
93 // Check that custom types as attribute values are also supported
94 {
95 string str1, str2;
96 osstream strm1(str1), strm2(str2);
97 formatter f = expr::stream << expr::attr< my_class >(data::attr3());
98 f(rec, strm1);
99 strm2 << my_class(77);
100 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
101 }
102 // Check how missing attribute values are handled
103 {
104 string str1;
105 osstream strm1(str1);
106 formatter f = expr::stream
107 << expr::attr< int >(data::attr1())
108 << expr::attr< int >(data::attr4()).or_throw()
109 << expr::attr< double >(data::attr2());
110 BOOST_CHECK_THROW(f(rec, strm1), logging::runtime_error);
111 }
112 {
113 string str1, str2;
114 osstream strm1(str1), strm2(str2);
115 formatter f = expr::stream
116 << expr::attr< int >(data::attr1())
117 << expr::attr< int >(data::attr4())
118 << expr::attr< double >(data::attr2());
119 f(rec, strm1);
120 strm2 << 10 << 5.5;
121 BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
122 }
123 }
124