aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/printable.cpp
blob: e2a56c7d70f91adf93c3cbba7b01451a10828235 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "printable.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <sstream>

namespace vespalib {

std::string Printable::toString(bool verbose, const std::string& indent) const
{
    std::ostringstream o;
    print(o, verbose, indent);
    return o.str();
}

vespalib::string
AsciiPrintable::PrintProperties::indent(uint32_t extraLevels) const
{
    vespalib::asciistream as;
    as << _indent;
    for (uint32_t i=0; i<extraLevels; ++i) {
        as << "  ";
    }
    return as.str();
}

void
AsciiPrintable::print(std::ostream& out, bool verbose,
                      const std::string& indent) const
{
    vespalib::asciistream as;
    print(as, PrintProperties(verbose ? VERBOSE : NORMAL, indent));
    out << as.str();
}

vespalib::string
AsciiPrintable::toString(const PrintProperties& p) const
{
    vespalib::asciistream as;
    print(as, p);
    return as.str();
}

std::ostream& operator<<(std::ostream& out, const Printable& p) {
    p.print(out);
    return out;
}

vespalib::asciistream& operator<<(vespalib::asciistream& out, const AsciiPrintable& p)
{
    p.print(out);
    return out;
}

} // vespalib