GCC Code Coverage Report
Directory: src/yaplog/ Exec Total Coverage
File: src/yaplog/dump.cpp Lines: 50 50 100.0 %
Date: 2021-11-23 14:33:27 Branches: 75 140 53.6 %

Line Branch Exec Source
1
#include "dump.h"
2
3
#include <sstream>
4
#include <iomanip>
5
6
using namespace logger;
7
8
6
dump::dump(const void *data, const size_t size) : m_data(data)
9
6
                                                , m_size(size)
10
6
{ }
11
12
6
dump::~dump()
13
6
{ }
14
15
/**
16
* @brief Retrieve human readable version of buffer.
17
*
18
* @return Human readable string.
19
*/
20
6
std::string dump::get_formatted_string() const
21
{
22
6
    const unsigned char *data_ptr = static_cast<const unsigned char *>(m_data);
23
24
6
    std::stringstream output;
25

6
    output << "Dump " << m_size
26

6
           << " byte(s) starting at " << m_data << ":";
27
28
6
    std::stringstream offset;
29
6
    std::stringstream hexa;
30
6
    std::stringstream ascii;
31
32
6
    size_t i = 0;
33
34

6
    offset.str("");
35
6
    offset << "\n";
36

6
    offset << std::setw(8) << std::setfill('0')
37

6
           << std::hex << (i);
38
6
    offset << " ";
39

6
    hexa.str("");
40

6
    ascii.str("");
41
141
    for (i = 0; i < m_size; i++) {
42
        // Add one extra space after 8 bytes.
43
135
        if (i % 16 == 8) {
44
8
            ascii << " ";
45
8
            hexa << " ";
46
        }
47
48

135
        hexa << std::hex << std::setfill('0') << std::setw(2)
49
135
             << static_cast<unsigned int>(*data_ptr);
50
135
        hexa << " ";
51
52
135
        if (isprint(*data_ptr))
53
127
            ascii << (*data_ptr);
54
        else
55
8
            ascii << ".";
56
57
135
        data_ptr++;
58
59
135
        if (i % 16 == 15) {
60
14
            output << offset.str() << hexa.str()
61



7
                   << "|" << ascii.str() << "|";
62
63

7
            offset.str("");
64
7
            offset << "\n";
65

7
            offset << std::setw(8) << std::setfill('0')
66

7
                   << std::hex << (i + 1);
67
7
            offset << " ";
68

7
            hexa.str("");
69

7
            ascii.str("");
70
        }
71
    }
72
73
6
    if (i % 16 != 0) {
74

3
        output << offset.str();
75


3
        output << std::setw(49) << std::setfill(' ') << std::left << hexa.str();
76
3
        output << "|";
77


3
        output << std::setw(17) << std::setfill(' ') << std::left << ascii.str();
78
3
        output << "|";
79
    }
80
81
12
    return output.str();
82
6
}