Trace Parser: add JSON output support 22/158722/14
authorAlexander Aksenov <a.aksenov@samsung.com>
Thu, 2 Nov 2017 12:25:34 +0000 (15:25 +0300)
committerAlexander Aksenov <a.aksenov@samsung.com>
Wed, 15 Nov 2017 13:21:33 +0000 (16:21 +0300)
Change-Id: Ia9246acea475f56ea73659357c220b07dbbde84e
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
src/cli/trace_parser/exe/CMakeLists.txt
src/cli/trace_parser/exe/writer_json.cpp [new file with mode: 0644]
src/cli/trace_parser/exe/writer_json.h [new file with mode: 0644]
src/cli/trace_parser/exe/writers.cpp

index f17d42c49a75bcb083f71d72f86d4b3f7b3f056d..bae6d516e2ad2837330f23971cb8cbeea09bc7c3 100644 (file)
@@ -10,6 +10,7 @@ set(SRC
   ${CMAKE_CURRENT_SOURCE_DIR}/writers.cpp
   ${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}/main.cpp
 )
 
diff --git a/src/cli/trace_parser/exe/writer_json.cpp b/src/cli/trace_parser/exe/writer_json.cpp
new file mode 100644 (file)
index 0000000..9c6bd91
--- /dev/null
@@ -0,0 +1,294 @@
+/*
+ *  SWAP Trace Parser
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Alexander Aksenov <a.aksenov@samsung.com>
+ *
+ * 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 <boost/algorithm/string.hpp>
+#include <data_writer.h>
+#include "writer_json.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<typename T>
+static void data_out(const std::string &name, const T val, std::ostream &ios)
+{
+    prepare_output(ios);
+
+    ios << "\"" << name << "\" : " << val;
+}
+
+template<typename T>
+static void args_out(const std::string &name, const T val, std::ostream &ios)
+{
+    prepare_output(ios);
+
+    ios << "\"" << name << "\" : " << val;
+}
+
+template<typename T>
+static void args_vector_out(const std::string &name, const std::vector<T> &val,
+                            std::ostream &ios)
+{
+    prepare_output(ios);
+
+    ios << "\"" << name << "\" : [";
+
+    for (auto const &item : val)
+        ios << item << ", ";
+}
+
+
+
+void DataWriterJSON::output_val(const std::string &name, const uint8_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const uint32_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const uint64_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const float val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const double val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const std::string &val)
+{
+    prepare_output(ios_);
+
+    ios_ << "\"" << name << "\" : \"";
+
+    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 DataWriterJSON::output_val(const std::string &name, const char val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_val(const std::string &name, const int val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_hex(const std::string &name, const uint32_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_hex(const std::string &name, const uint64_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const bool val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const uint8_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const uint32_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const uint64_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const float val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name, const double val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name,
+                                 const std::string &val)
+{
+    prepare_output(ios_);
+
+    ios_ << "\"" << name << "\" : \"";
+
+    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 DataWriterJSON::output_args(const std::string &name)
+{
+    prepare_output(ios_);
+
+    ios_ << "\"" << name << "\" : null";
+}
+
+void DataWriterJSON::output_args(const std::string &name,
+                                const std::vector<float> &val, uint64_t p)
+{
+    args_vector_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args(const std::string &name,
+                                const std::vector<uint32_t> &val, uint64_t p)
+{
+    args_vector_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_args_hex(const std::string &name,
+                                    const uint64_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterJSON::output_time(const std::string &name, const uint64_t val)
+{
+    prepare_output(ios_);
+
+    ios_ << "\"" << name << "\" : " << val;
+}
+
+void DataWriterJSON::output_buf(const std::vector<char> &buf)
+{
+    // TODO No place with implemnetation
+}
+
+void DataWriterJSON::start_array(const std::string &name)
+{
+    prepare_output(ios_);
+    start_output();
+
+    ios_ << "\"" << name << "\" : {";
+}
+
+void DataWriterJSON::finish_array()
+{
+    finish_output();
+    ios_ << "}";
+}
+
+void DataWriterJSON::start_object(const std::string &name)
+{
+    prepare_output(ios_);
+    start_output();
+
+    ios_ << "\"" << name << "\" : {";
+}
+
+void DataWriterJSON::finish_object()
+{
+    finish_output();
+    ios_ << "}";
+}
+
+void DataWriterJSON::start_msg()
+{
+    prepare_output(ios_);
+    start_output();
+
+    ios_ << std::endl << "{";
+}
+
+void DataWriterJSON::finish_msg()
+{
+    finish_output();
+
+    ios_ << "}";
+}
+
+void DataWriterJSON::start_file(const uint32_t version)
+{
+    ios_ << "{" << std::endl;
+    ios_ << "\"trace_info\" : {" << std::endl;
+    ios_ << "\"protocol_version\" : " << version << std::endl;
+    ios_ << "}," << std::endl;
+
+    ios_ << "\"trace_obj\" : [" << std::endl;
+}
+
+void DataWriterJSON::finish_file()
+{
+    ios_ << "]" << std::endl;
+    ios_ << "}" << std::endl;
+}
diff --git a/src/cli/trace_parser/exe/writer_json.h b/src/cli/trace_parser/exe/writer_json.h
new file mode 100644 (file)
index 0000000..cc14768
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  SWAP Trace Parser
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Alexander Aksenov <a.aksenov@samsung.com>
+ *
+ * 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_JSON_H__
+#define __WRITER_JSON_H__
+
+
+#include <data_writer.h>
+
+
+class DataWriterJSON: public DataWriter
+{
+public:
+    using DataWriter::DataWriter;
+
+    void start_file(const uint32_t version);
+    void finish_file(void);
+
+    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<float> &val, uint64_t p);
+    void output_args(const std::string &name, const std::vector<uint32_t> &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<char> &buf);
+    void start_array(const std::string &name);
+    void finish_array();
+    void start_object(const std::string &name);
+    void finish_object();
+};
+
+
+#endif /* __WRITER_JSON_H__ */
index d706babffebe3fdfef9f6e7b556a331252afc747..2e38e8001ad4c67983b968b6be2007fcd13a6d3f 100644 (file)
@@ -33,6 +33,7 @@
 #include <data_writer.h>
 #include "writer_text.h"
 #include "writer_py.h"
+#include "writer_json.h"
 #include "writers.h"
 
 
@@ -51,7 +52,7 @@ std::shared_ptr<DataWriter> get_data_writer(const std::string &type,
     else if (!type.compare(py_long) || !type.compare(py_short))
         return std::shared_ptr<DataWriter>(new DataWriterPython(ios));
     else if (!type.compare(json_long) || !type.compare(json_short))
-        throw std::logic_error("Json output type unsupported");
+        return std::shared_ptr<DataWriter>(new DataWriterJSON(ios));
 
     throw std::logic_error("Unsupported Type");
 }