File: src_record_ostream.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (479 lines) | stat: -rw-r--r-- 17,054 bytes parent folder | download | duplicates (10)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 *          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   src_record_ostream.cpp
 * \author Andrey Semashev
 * \date   23.08.2015
 *
 * \brief  This header contains tests for the log record formatting output stream.
 */

#define BOOST_TEST_MODULE src_record_ostream

#include <locale>
#include <string>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
#include <string_view>
#endif
#include <boost/test/unit_test.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/log/core/record.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/expressions/message.hpp>
#include <boost/log/attributes/value_extraction.hpp>
#include "char_definitions.hpp"
#include "make_record.hpp"

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

namespace {

struct unreferencable_data
{
    unsigned int m : 2;
    unsigned int n : 6;

    enum my_enum
    {
        one = 1,
        two = 2
    };

    // The following static constants don't have definitions, so they can only be used in constant expressions.
    // Trying to bind a reference to these members will result in linking errors.
    static const int x = 7;
    static const my_enum y = one;

    unreferencable_data()
    {
        m = 1;
        n = 5;
    }
};

template< typename CharT >
struct test_impl
{
    typedef CharT char_type;
    typedef test_data< char_type > strings;
    typedef std::basic_string< char_type > string_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_record_ostream< char_type > record_ostream_type;

    template< typename StringT >
    static void width_formatting()
    {
        // Check that widening works
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type strm_fmt(rec);
            strm_fmt << strings::abc() << std::setw(8) << (StringT)strings::abcd() << strings::ABC();
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << strings::abc() << std::setw(8) << (StringT)strings::abcd() << strings::ABC();

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }

        // Check that the string is not truncated
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type strm_fmt(rec);
            strm_fmt << strings::abc() << std::setw(1) << (StringT)strings::abcd() << strings::ABC();
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << strings::abc() << std::setw(1) << (StringT)strings::abcd() << strings::ABC();

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }
    }

    template< typename StringT >
    static void fill_formatting()
    {
        logging::record rec = make_record();
        BOOST_REQUIRE(!!rec);
        record_ostream_type strm_fmt(rec);
        strm_fmt << strings::abc() << std::setfill(static_cast< char_type >('x')) << std::setw(8) << (StringT)strings::abcd() << strings::ABC();
        strm_fmt.flush();
        string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

        ostream_type strm_correct;
        strm_correct << strings::abc() << std::setfill(static_cast< char_type >('x')) << std::setw(8) << (StringT)strings::abcd() << strings::ABC();

        BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
    }

    template< typename StringT >
    static void alignment()
    {
        // Left alignment
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type strm_fmt(rec);
            strm_fmt << strings::abc() << std::setw(8) << std::left << (StringT)strings::abcd() << strings::ABC();
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << strings::abc() << std::setw(8) << std::left << (StringT)strings::abcd() << strings::ABC();

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }

        // Right alignment
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type strm_fmt(rec);
            strm_fmt << strings::abc() << std::setw(8) << std::right << (StringT)strings::abcd() << strings::ABC();
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << strings::abc() << std::setw(8) << std::right << (StringT)strings::abcd() << strings::ABC();

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }
    }

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
    template< typename StringT >
    static void rvalue_stream()
    {
        logging::record rec = make_record();
        BOOST_REQUIRE(!!rec);
        record_ostream_type(rec) << strings::abc() << std::setw(8) << (StringT)strings::abcd() << strings::ABC() << std::flush;
        string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

        ostream_type strm_correct;
        strm_correct << strings::abc() << std::setw(8) << (StringT)strings::abcd() << strings::ABC();

        BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
    }
#endif

    static void output_unreferencable_data()
    {
        unreferencable_data data;
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type strm_fmt(rec);
            strm_fmt << data.m << static_cast< char_type >(' ') << data.n << static_cast< char_type >(' ') << unreferencable_data::x << static_cast< char_type >(' ') << unreferencable_data::y;
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << static_cast< unsigned int >(data.m) << static_cast< char_type >(' ') << static_cast< unsigned int >(data.n) << static_cast< char_type >(' ') << static_cast< int >(unreferencable_data::x) << static_cast< char_type >(' ') << static_cast< int >(unreferencable_data::y);

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            record_ostream_type(rec) << data.m << static_cast< char_type >(' ') << data.n << static_cast< char_type >(' ') << unreferencable_data::x << static_cast< char_type >(' ') << unreferencable_data::y << std::flush;
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

            ostream_type strm_correct;
            strm_correct << static_cast< unsigned int >(data.m) << static_cast< char_type >(' ') << static_cast< unsigned int >(data.n) << static_cast< char_type >(' ') << static_cast< int >(unreferencable_data::x) << static_cast< char_type >(' ') << static_cast< int >(unreferencable_data::y);

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }
#endif
    }

    static void formatting_params_restoring()
    {
        record_ostream_type strm_fmt;
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            strm_fmt.attach_record(rec);
            strm_fmt << std::setw(8) << std::setfill(static_cast< char_type >('x')) << std::hex << 15;
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);
            strm_fmt.detach_from_record();

            ostream_type strm_correct;
            strm_correct << std::setw(8) << std::setfill(static_cast< char_type >('x')) << std::hex << 15;

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }

        // Check that the formatting flags are reset for the next record
        {
            logging::record rec = make_record();
            BOOST_REQUIRE(!!rec);
            strm_fmt.attach_record(rec);
            strm_fmt << 15;
            strm_fmt.flush();
            string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);
            strm_fmt.detach_from_record();

            ostream_type strm_correct;
            strm_correct << 15;

            BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
        }
    }
};

} // namespace

// Test support for width formatting
BOOST_AUTO_TEST_CASE_TEMPLATE(width_formatting, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::BOOST_NESTED_TEMPLATE width_formatting< const CharT* >();
    test::BOOST_NESTED_TEMPLATE width_formatting< typename test::string_type >();
    test::BOOST_NESTED_TEMPLATE width_formatting< boost::basic_string_view< CharT > >();
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
    test::BOOST_NESTED_TEMPLATE width_formatting< std::basic_string_view< CharT > >();
#endif
}

// Test support for filler character setup
BOOST_AUTO_TEST_CASE_TEMPLATE(fill_formatting, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::BOOST_NESTED_TEMPLATE fill_formatting< const CharT* >();
    test::BOOST_NESTED_TEMPLATE fill_formatting< typename test::string_type >();
    test::BOOST_NESTED_TEMPLATE fill_formatting< boost::basic_string_view< CharT > >();
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
    test::BOOST_NESTED_TEMPLATE fill_formatting< std::basic_string_view< CharT > >();
#endif
}

// Test support for text alignment
BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::BOOST_NESTED_TEMPLATE alignment< const CharT* >();
    test::BOOST_NESTED_TEMPLATE alignment< typename test::string_type >();
    test::BOOST_NESTED_TEMPLATE alignment< boost::basic_string_view< CharT > >();
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
    test::BOOST_NESTED_TEMPLATE alignment< std::basic_string_view< CharT > >();
#endif
}

#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
// Test support for rvalue stream objects
BOOST_AUTO_TEST_CASE_TEMPLATE(rvalue_stream, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::BOOST_NESTED_TEMPLATE rvalue_stream< const CharT* >();
    test::BOOST_NESTED_TEMPLATE rvalue_stream< typename test::string_type >();
    test::BOOST_NESTED_TEMPLATE rvalue_stream< boost::basic_string_view< CharT > >();
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
    test::BOOST_NESTED_TEMPLATE rvalue_stream< std::basic_string_view< CharT > >();
#endif
}
#endif

// Test output of data to which a reference cannot be bound
BOOST_AUTO_TEST_CASE_TEMPLATE(output_unreferencable_data, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::output_unreferencable_data();
}

// Test that formatting settings are reset for new log records
BOOST_AUTO_TEST_CASE_TEMPLATE(formatting_params_restoring, CharT, char_types)
{
    typedef test_impl< CharT > test;
    test::formatting_params_restoring();
}

namespace my_namespace {

class A {};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, A const&)
{
    strm << "A";
    return strm;
}

class B {};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, B&)
{
    strm << "B";
    return strm;
}

template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, B*)
{
    strm << "B*";
    return strm;
}

template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, const B*)
{
    strm << "const B*";
    return strm;
}

class C {};
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, C const&)
{
    strm << "C";
    return strm;
}

enum E { eee };
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, E)
{
    strm << "E";
    return strm;
}

} // namespace my_namespace

// Test operator forwarding
BOOST_AUTO_TEST_CASE_TEMPLATE(operator_forwarding, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_string< char_type > string_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_record_ostream< char_type > record_ostream_type;

    logging::record rec = make_record();
    BOOST_REQUIRE(!!rec);
    record_ostream_type strm_fmt(rec);

    const my_namespace::A a = my_namespace::A(); // const lvalue
    my_namespace::B b; // lvalue
    strm_fmt << a << b << my_namespace::C(); // rvalue
    strm_fmt << my_namespace::eee;
    strm_fmt << &b << (my_namespace::B const*)&b;
    strm_fmt.flush();
    string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

    ostream_type strm_correct;
    strm_correct << a << b << my_namespace::C() << my_namespace::eee << &b << (my_namespace::B const*)&b;

    BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
}

namespace my_namespace2 {

class A {};
template< typename CharT >
inline logging::basic_record_ostream< CharT >& operator<< (logging::basic_record_ostream< CharT >& strm, A const&)
{
    strm << "A";
    return strm;
}

class B {};
template< typename CharT >
inline logging::basic_record_ostream< CharT >& operator<< (logging::basic_record_ostream< CharT >& strm, B&)
{
    strm << "B";
    return strm;
}

class C {};
template< typename CharT >
inline logging::basic_record_ostream< CharT >& operator<< (logging::basic_record_ostream< CharT >& strm, C const&)
{
    strm << "C";
    return strm;
}

class D {};
template< typename CharT >
inline logging::basic_record_ostream< CharT >& operator<< (logging::basic_record_ostream< CharT >& strm,
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
    D&&
#else
    D const&
#endif
    )
{
    strm << "D";
    return strm;
}

enum E { eee };
template< typename CharT >
inline logging::basic_record_ostream< CharT >& operator<< (logging::basic_record_ostream< CharT >& strm, E)
{
    strm << "E";
    return strm;
}

} // namespace my_namespace2

// Test operator overriding
BOOST_AUTO_TEST_CASE_TEMPLATE(operator_overriding, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_string< char_type > string_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_record_ostream< char_type > record_ostream_type;

    logging::record rec = make_record();
    BOOST_REQUIRE(!!rec);
    record_ostream_type strm_fmt(rec);

    const my_namespace2::A a = my_namespace2::A(); // const lvalue
    my_namespace2::B b; // lvalue
    strm_fmt << a << b << my_namespace2::C() << my_namespace2::D(); // rvalue
    strm_fmt << my_namespace2::eee;
    strm_fmt.flush();
    string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

    ostream_type strm_correct;
    strm_correct << "ABCDE";

    BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
}

// Test that operator<< returns a record_ostream
BOOST_AUTO_TEST_CASE_TEMPLATE(operator_return_type, CharT, char_types)
{
    typedef CharT char_type;
    typedef std::basic_string< char_type > string_type;
    typedef std::basic_ostringstream< char_type > ostream_type;
    typedef logging::basic_record_ostream< char_type > record_ostream_type;

    logging::record rec = make_record();
    BOOST_REQUIRE(!!rec);
    record_ostream_type strm_fmt(rec);

    // The test here verifies that the result of << "Hello" is a record_ostream, not std::ostream or logging::formatting_ostream.
    // The subsequent << A() will only compile if the stream is record_ostream.
    strm_fmt << "Hello " << my_namespace2::A();
    strm_fmt.flush();
    string_type rec_message = logging::extract_or_throw< string_type >(expr::message.get_name(), rec);

    ostream_type strm_correct;
    strm_correct << "Hello A";

    BOOST_CHECK(equal_strings(rec_message, strm_correct.str()));
}