Trace Parser: add CSV output support
[platform/core/system/swap-manager.git] / src / cli / trace_parser / exe / writer_csv.cpp
1 /*
2  *  SWAP Trace Parser
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Alexander Aksenov <a.aksenov@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Contributors:
23  * - Samsung RnD Institute Russia
24  *
25  */
26
27
28
29 #include <boost/algorithm/string.hpp>
30 #include <data_writer.h>
31 #include "writer_csv.h"
32
33
34 static bool first_element = true;
35
36 static void prepare_output(std::ostream &ios)
37 {
38     if (!first_element)
39         ios << ",";
40     else
41         first_element = false;
42 }
43
44 static inline void start_output()
45 {
46     first_element = true;
47 }
48
49 static inline void finish_output()
50 {
51     first_element = false;
52 }
53
54
55
56 template<typename T>
57 static void data_out(const std::string &name, const T val, std::ostream &ios)
58 {
59     prepare_output(ios);
60
61     ios <<  val;
62 }
63
64 template<typename T>
65 static void data_hex_out(const std::string &name, const T val,
66                          std::ostream &ios)
67 {
68     prepare_output(ios);
69
70     ios << "0x" << std::hex << val << std::dec;
71 }
72
73 template<typename T>
74 static void args_out(const std::string &mod, const T val, std::ostream &ios)
75 {
76     prepare_output(ios);
77
78     ios << "\"" << mod << "\"," << val;
79 }
80
81 template<typename T>
82 static void args_vector_out(const std::string &mod, const std::vector<T> &val,
83                             const uint64_t p, std::ostream &ios)
84 {
85     prepare_output(ios);
86
87     ios << "\"" << mod << "\"," << val.size() << ",0x" << std::hex << p
88         << std::dec;
89
90     for (auto const &item : val)
91         ios << "," <<  item;
92 }
93
94
95
96
97
98 void DataWriterCSV::start_msg()
99 {
100     start_output();
101 }
102
103 void DataWriterCSV::finish_msg()
104 {
105     ios_ << std::endl;
106 }
107
108 void DataWriterCSV::output_val(const std::string &name, const uint8_t val)
109 {
110     data_out(name, val, ios_);
111 }
112
113 void DataWriterCSV::output_val(const std::string &name, const uint32_t val)
114 {
115     data_out(name, val, ios_);
116 }
117
118 void DataWriterCSV::output_val(const std::string &name, const uint64_t val)
119 {
120     data_out(name, val, ios_);
121 }
122
123 void DataWriterCSV::output_val(const std::string &name, const float val)
124 {
125     data_out(name, val, ios_);
126 }
127
128 void DataWriterCSV::output_val(const std::string &name, const double val)
129 {
130     data_out(name, val, ios_);
131 }
132
133 void DataWriterCSV::output_val(const std::string &name, const std::string &val)
134 {
135     prepare_output(ios_);
136
137     ios_ << "\"";
138
139     if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) {
140         ios_ << val;
141     } else {
142         std::string new_val(val);
143
144         boost::replace_all(new_val, "\n", "\\n");
145         boost::replace_all(new_val, "\r", "\\r");
146         ios_ << new_val;
147     }
148
149     ios_ << "\"";
150 }
151
152 void DataWriterCSV::output_val(const std::string &name, const char val)
153 {
154     data_out(name, val, ios_);
155 }
156
157 void DataWriterCSV::output_val(const std::string &name, const int val)
158 {
159     data_out(name, val, ios_);
160 }
161
162 void DataWriterCSV::output_hex(const std::string &name, const uint32_t val)
163 {
164     data_hex_out(name, val, ios_);
165 }
166
167 void DataWriterCSV::output_hex(const std::string &name, const uint64_t val)
168 {
169     data_hex_out(name, val, ios_);
170 }
171
172 void DataWriterCSV::output_args(const std::string &name, const bool val)
173 {
174     args_out("b", val, ios_);
175 }
176
177 void DataWriterCSV::output_args(const std::string &name, const uint8_t val)
178 {
179     args_out("c", val, ios_);
180 }
181
182 void DataWriterCSV::output_args(const std::string &name, const uint32_t val)
183 {
184     args_out("d", val, ios_);
185 }
186
187 void DataWriterCSV::output_args(const std::string &name, const uint64_t val)
188 {
189     args_out("x", val, ios_);
190 }
191
192 void DataWriterCSV::output_args(const std::string &name, const float val)
193 {
194     args_out("f", val, ios_);
195 }
196
197 void DataWriterCSV::output_args(const std::string &name, const double val)
198 {
199     args_out("w", val, ios_);
200 }
201
202 void DataWriterCSV::output_args(const std::string &name, const std::string &val)
203 {
204     prepare_output(ios_);
205
206     ios_ << "\"s\", \"";
207
208     if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) {
209         ios_ << val;
210     } else {
211         std::string new_val(val);
212
213         boost::replace_all(new_val, "\n", "\\n");
214         boost::replace_all(new_val, "\r", "\\r");
215         ios_ << new_val;
216     }
217
218     ios_ << "\"";
219 }
220
221 void DataWriterCSV::output_args(const std::string &name,
222                                 const std::vector<float> &val, uint64_t p)
223 {
224     args_vector_out("F", val, p, ios_);
225 }
226
227 void DataWriterCSV::output_args(const std::string &name,
228                                 const std::vector<uint32_t> &val, uint64_t p)
229 {
230     args_vector_out("D", val, p, ios_);
231 }
232
233 void DataWriterCSV::output_time(const std::string &name, const uint64_t val)
234 {
235     prepare_output(ios_);
236
237     ios_ << val;
238 }
239
240 void DataWriterCSV::output_args_hex(const std::string &name, const uint64_t val)
241 {
242     prepare_output(ios_);
243
244     ios_ << "0x" << std::hex << val << std::dec;
245 }
246
247 void DataWriterCSV::output_buf(const std::vector<char> &buf)
248 {
249     // TODO No place with implemnetation
250 }