From a06ec84ded53502beea1c8caeb4acecf9aacb7e9 Mon Sep 17 00:00:00 2001 From: Alexander Aksenov Date: Fri, 3 Nov 2017 13:44:25 +0300 Subject: [PATCH] Trace Parser: add CSV output support Change-Id: Ib8826b11988619690e80bd1e314f532d359676e1 Signed-off-by: Alexander Aksenov --- src/cli/trace_parser/exe/CMakeLists.txt | 1 + src/cli/trace_parser/exe/main.cpp | 2 +- src/cli/trace_parser/exe/writer_csv.cpp | 250 ++++++++++++++++++++++++++++++++ src/cli/trace_parser/exe/writer_csv.h | 79 ++++++++++ src/cli/trace_parser/exe/writers.cpp | 5 + 5 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 src/cli/trace_parser/exe/writer_csv.cpp create mode 100644 src/cli/trace_parser/exe/writer_csv.h diff --git a/src/cli/trace_parser/exe/CMakeLists.txt b/src/cli/trace_parser/exe/CMakeLists.txt index bae6d51..3397b80 100644 --- a/src/cli/trace_parser/exe/CMakeLists.txt +++ b/src/cli/trace_parser/exe/CMakeLists.txt @@ -11,6 +11,7 @@ set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/writer_text.cpp ${CMAKE_CURRENT_SOURCE_DIR}/writer_py.cpp ${CMAKE_CURRENT_SOURCE_DIR}/writer_json.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/writer_csv.cpp ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ) diff --git a/src/cli/trace_parser/exe/main.cpp b/src/cli/trace_parser/exe/main.cpp index 5e60c5c..5683c98 100644 --- a/src/cli/trace_parser/exe/main.cpp +++ b/src/cli/trace_parser/exe/main.cpp @@ -89,7 +89,7 @@ static po::variables_map parse_args(int argc, char **argv) ("version,v", po::value()->required(), "Protocol version") ("type,t", po::value()->required(), - "Output type(t(ext), p(y), j(son))") + "Output type(t(ext), p(y), j(son), c(sv))") ; po::positional_options_description p; diff --git a/src/cli/trace_parser/exe/writer_csv.cpp b/src/cli/trace_parser/exe/writer_csv.cpp new file mode 100644 index 0000000..ae08784 --- /dev/null +++ b/src/cli/trace_parser/exe/writer_csv.cpp @@ -0,0 +1,250 @@ +/* + * SWAP Trace Parser + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * + * Alexander Aksenov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + + + +#include +#include +#include "writer_csv.h" + + +static bool first_element = true; + +static void prepare_output(std::ostream &ios) +{ + if (!first_element) + ios << ","; + else + first_element = false; +} + +static inline void start_output() +{ + first_element = true; +} + +static inline void finish_output() +{ + first_element = false; +} + + + +template +static void data_out(const std::string &name, const T val, std::ostream &ios) +{ + prepare_output(ios); + + ios << val; +} + +template +static void data_hex_out(const std::string &name, const T val, + std::ostream &ios) +{ + prepare_output(ios); + + ios << "0x" << std::hex << val << std::dec; +} + +template +static void args_out(const std::string &mod, const T val, std::ostream &ios) +{ + prepare_output(ios); + + ios << "\"" << mod << "\"," << val; +} + +template +static void args_vector_out(const std::string &mod, const std::vector &val, + const uint64_t p, std::ostream &ios) +{ + prepare_output(ios); + + ios << "\"" << mod << "\"," << val.size() << ",0x" << std::hex << p + << std::dec; + + for (auto const &item : val) + ios << "," << item; +} + + + + + +void DataWriterCSV::start_msg() +{ + start_output(); +} + +void DataWriterCSV::finish_msg() +{ + ios_ << std::endl; +} + +void DataWriterCSV::output_val(const std::string &name, const uint8_t val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const uint32_t val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const uint64_t val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const float val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const double val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const std::string &val) +{ + prepare_output(ios_); + + ios_ << "\""; + + if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) { + ios_ << val; + } else { + std::string new_val(val); + + boost::replace_all(new_val, "\n", "\\n"); + boost::replace_all(new_val, "\r", "\\r"); + ios_ << new_val; + } + + ios_ << "\""; +} + +void DataWriterCSV::output_val(const std::string &name, const char val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_val(const std::string &name, const int val) +{ + data_out(name, val, ios_); +} + +void DataWriterCSV::output_hex(const std::string &name, const uint32_t val) +{ + data_hex_out(name, val, ios_); +} + +void DataWriterCSV::output_hex(const std::string &name, const uint64_t val) +{ + data_hex_out(name, val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const bool val) +{ + args_out("b", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const uint8_t val) +{ + args_out("c", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const uint32_t val) +{ + args_out("d", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const uint64_t val) +{ + args_out("x", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const float val) +{ + args_out("f", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const double val) +{ + args_out("w", val, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, const std::string &val) +{ + prepare_output(ios_); + + ios_ << "\"s\", \""; + + if ((val.find("\n") == std::string::npos) && (val.find("\r") == std::string::npos)) { + ios_ << val; + } else { + std::string new_val(val); + + boost::replace_all(new_val, "\n", "\\n"); + boost::replace_all(new_val, "\r", "\\r"); + ios_ << new_val; + } + + ios_ << "\""; +} + +void DataWriterCSV::output_args(const std::string &name, + const std::vector &val, uint64_t p) +{ + args_vector_out("F", val, p, ios_); +} + +void DataWriterCSV::output_args(const std::string &name, + const std::vector &val, uint64_t p) +{ + args_vector_out("D", val, p, ios_); +} + +void DataWriterCSV::output_time(const std::string &name, const uint64_t val) +{ + prepare_output(ios_); + + ios_ << val; +} + +void DataWriterCSV::output_args_hex(const std::string &name, const uint64_t val) +{ + prepare_output(ios_); + + ios_ << "0x" << std::hex << val << std::dec; +} + +void DataWriterCSV::output_buf(const std::vector &buf) +{ + // TODO No place with implemnetation +} diff --git a/src/cli/trace_parser/exe/writer_csv.h b/src/cli/trace_parser/exe/writer_csv.h new file mode 100644 index 0000000..00611d2 --- /dev/null +++ b/src/cli/trace_parser/exe/writer_csv.h @@ -0,0 +1,79 @@ +/* + * SWAP Trace Parser + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * + * Alexander Aksenov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - Samsung RnD Institute Russia + * + */ + + + +#ifndef __WRITER_CSV_H__ +#define __WRITER_CSV_H__ + + +#include + + +class DataWriterCSV: public DataWriter +{ +public: + using DataWriter::DataWriter; + + void start_file(const uint32_t version) {} + void finish_file() {} + + void start_msg(); + void finish_msg(); + + void output_val(const std::string &name, const uint8_t val); + void output_val(const std::string &name, const uint32_t val); + void output_val(const std::string &name, const uint64_t val); + void output_val(const std::string &name, const float val); + void output_val(const std::string &name, const double val); + void output_val(const std::string &name, const std::string &val); + void output_val(const std::string &name, const char val); + void output_val(const std::string &name, const int val); + + void output_hex(const std::string &name, const uint32_t val); + void output_hex(const std::string &name, const uint64_t val); + + void output_args(const std::string &name, const bool val); + void output_args(const std::string &name, const uint8_t val); + void output_args(const std::string &name, const uint32_t val); + void output_args(const std::string &name, const uint64_t val); + void output_args(const std::string &name, const float val); + void output_args(const std::string &name, const double val); + void output_args(const std::string &name, const std::string &val); + void output_args(const std::string &name) {} + void output_args(const std::string &name, const std::vector &val, uint64_t p); + void output_args(const std::string &name, const std::vector &val, uint64_t p); + void output_args_hex(const std::string &name, const uint64_t val); + + void output_time(const std::string &name, const uint64_t val); + void output_buf(const std::vector &buf); + void start_array(const std::string &name) {} + void finish_array() {} + void start_object(const std::string &name) {} + void finish_object() {} +}; + +#endif /* __WRITER_CSV_H__ */ diff --git a/src/cli/trace_parser/exe/writers.cpp b/src/cli/trace_parser/exe/writers.cpp index 2e38e80..969d4c1 100644 --- a/src/cli/trace_parser/exe/writers.cpp +++ b/src/cli/trace_parser/exe/writers.cpp @@ -34,6 +34,7 @@ #include "writer_text.h" #include "writer_py.h" #include "writer_json.h" +#include "writer_csv.h" #include "writers.h" @@ -46,6 +47,8 @@ std::shared_ptr get_data_writer(const std::string &type, const std::string py_short = "p"; const std::string json_long = "json"; const std::string json_short = "j"; + const std::string csv_long = "csv"; + const std::string csv_short = "c"; if (!type.compare(text_long) || !type.compare(text_short)) return std::shared_ptr(new DataWriterText(ios)); @@ -53,6 +56,8 @@ std::shared_ptr get_data_writer(const std::string &type, return std::shared_ptr(new DataWriterPython(ios)); else if (!type.compare(json_long) || !type.compare(json_short)) return std::shared_ptr(new DataWriterJSON(ios)); + else if (!type.compare(csv_long) || !type.compare(csv_short)) + return std::shared_ptr(new DataWriterCSV(ios)); throw std::logic_error("Unsupported Type"); } -- 2.7.4