Release 18.08
[platform/upstream/armnn.git] / src / armnn / JsonPrinter.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #include "JsonPrinter.hpp"
7
8 #include <iomanip>
9 #include <iostream>
10
11 namespace armnn
12 {
13
14 void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object)
15 {
16     PrintLabel(object.m_Label);
17     PrintMeasurementsList(object.m_Measurements);
18     PrintSeparator();
19     PrintNewLine();
20     PrintUnit(object.m_Unit);
21
22     if (!object.m_Children.empty())
23     {
24         PrintSeparator();
25         PrintNewLine();
26         for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
27         {
28             PrintJsonChildObject(object.m_Children[childIndex]);
29             // Only print separator and new line if current child is not the last element.
30             if (&object.m_Children[childIndex] != &object.m_Children.back())
31             {
32                 PrintSeparator();
33                 PrintNewLine();
34             }
35         }
36     }
37     PrintNewLine();
38     PrintFooter();
39 }
40
41 void JsonPrinter::PrintHeader()
42 {
43     m_OutputStream << "{" << std::endl;
44     IncrementNumberOfTabs();
45 }
46
47 void JsonPrinter::PrintArmNNHeader()
48 {
49     PrintTabs();
50     m_OutputStream << R"("ArmNN": {)" << std::endl;
51     IncrementNumberOfTabs();
52 }
53
54 void JsonPrinter::PrintLabel(const std::string& label)
55 {
56     PrintTabs();
57     m_OutputStream << R"(")" << label << R"(": {)" << std::endl;
58     IncrementNumberOfTabs();
59 }
60
61 void JsonPrinter::PrintUnit(armnn::Measurement::Unit unit)
62 {
63     PrintTabs();
64     m_OutputStream << R"("unit": ")";
65     m_OutputStream << armnn::Measurement::ToString(unit);
66     m_OutputStream << R"(")";
67 }
68
69 void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector)
70 {
71     if (measurementsVector.empty())
72     {
73         return;
74     }
75
76     PrintTabs();
77     m_OutputStream << R"("raw": [)" << std::endl;
78     IncrementNumberOfTabs();
79     PrintTabs();
80     auto iter = measurementsVector.begin();
81     m_OutputStream << *iter;
82     for (iter = std::next(iter); iter != measurementsVector.end(); ++iter)
83     {
84         m_OutputStream << "," << std::endl;
85         PrintTabs();
86         m_OutputStream << *iter;
87     }
88     m_OutputStream << std::endl;
89     DecrementNumberOfTabs();
90     PrintTabs();
91     m_OutputStream << "]";
92 }
93
94 void JsonPrinter::PrintTabs()
95 {
96     unsigned int numTabs = m_NumTabs;
97     while (numTabs-- > 0)
98     {
99         m_OutputStream << "\t";
100     }
101 }
102
103 void JsonPrinter::PrintSeparator()
104 {
105     m_OutputStream << ",";
106 }
107
108 void JsonPrinter::PrintNewLine()
109 {
110     m_OutputStream << std::endl;
111 }
112
113 void JsonPrinter::PrintFooter()
114 {
115     DecrementNumberOfTabs();
116     PrintTabs();
117     m_OutputStream << "}";
118 }
119
120 void JsonPrinter::DecrementNumberOfTabs()
121 {
122     if (m_NumTabs == 0)
123     {
124         return;
125     }
126     --m_NumTabs;
127 }
128
129 void JsonPrinter::IncrementNumberOfTabs()
130 {
131     ++m_NumTabs;
132 }
133
134 } // namespace armnn