File: form_to_log_manip.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (98 lines) | stat: -rw-r--r-- 2,985 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *          Copyright Andrey Semashev 2007 - 2015.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   form_to_log_manip.cpp
 * \author Andrey Semashev
 * \date   05.07.2015
 *
 * \brief  This header contains tests for support for the \c to_log_manip customization point.
 */

#define BOOST_TEST_MODULE form_to_log_manip

#include <string>
#include <ostream>
#include <algorithm>
#include <boost/test/unit_test.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/attributes/attribute_set.hpp>
#include <boost/log/utility/formatting_ostream.hpp>
#include <boost/log/utility/manipulators/to_log.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/core/record.hpp>
#include "char_definitions.hpp"
#include "make_record.hpp"

namespace logging = boost::log;
namespace attrs = logging::attributes;
namespace expr = logging::expressions;

namespace {

struct my_class
{
    int m_Data;

    explicit my_class(int data) : m_Data(data) {}
};

} // namespace

BOOST_LOG_ATTRIBUTE_KEYWORD(a_my_class, "MyClass", my_class)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_string, "String", std::string)

namespace {

template< typename CharT, typename TraitsT, typename AllocatorT >
inline logging::basic_formatting_ostream< CharT, TraitsT, AllocatorT >&
operator<< (logging::basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, logging::to_log_manip< my_class, tag::a_my_class > const& obj)
{
    strm << "a_my_class: [data: " << obj.get().m_Data << "]";
    return strm;
}

} // namespace

namespace std {

template< typename CharT, typename TraitsT, typename AllocatorT >
inline logging::basic_formatting_ostream< CharT, TraitsT, AllocatorT >&
operator<< (logging::basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, logging::to_log_manip< std::string, tag::a_string > const& obj)
{
    strm << "a_string: [" << obj.get() << "]";
    return strm;
}

} // namespace std

BOOST_AUTO_TEST_CASE_TEMPLATE(operator_overrides, CharT, char_types)
{
    typedef logging::record_view record_view;
    typedef logging::attribute_set attr_set;
    typedef std::basic_string< CharT > string;
    typedef logging::basic_formatting_ostream< CharT > osstream;
    typedef logging::basic_formatter< CharT > formatter;

    attrs::constant< my_class > attr1(my_class(77));
    attrs::constant< std::string > attr2("Hello");

    attr_set set1;
    set1[a_my_class.get_name()] = attr1;
    set1[a_string.get_name()] = attr2;

    record_view rec = make_record_view(set1);

    // Check that out custom operators are called
    {
        string str1, str2;
        osstream strm1(str1), strm2(str2);
        formatter f = expr::stream << a_my_class << ", " << a_string;
        f(rec, strm1);
        strm2 << "a_my_class: [data: " << 77 << "], a_string: [" << "Hello" << "]";
        BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
    }
}