Trace Parser: add python output support 33/158533/16
authorAlexander Aksenov <a.aksenov@samsung.com>
Wed, 1 Nov 2017 11:45:13 +0000 (14:45 +0300)
committerAlexander Aksenov <a.aksenov@samsung.com>
Wed, 15 Nov 2017 13:21:33 +0000 (16:21 +0300)
Change-Id: I0f56fe49cb3daa8fe7aa22d067850aef93f717e4
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
src/cli/trace_parser/exe/CMakeLists.txt
src/cli/trace_parser/exe/writer_py.cpp [new file with mode: 0644]
src/cli/trace_parser/exe/writer_py.h [new file with mode: 0644]
src/cli/trace_parser/exe/writers.cpp

index 75d212975af007edb36bbcad0030dcd62deb3ed6..f17d42c49a75bcb083f71d72f86d4b3f7b3f056d 100644 (file)
@@ -9,6 +9,7 @@ set(CMAKE_CXX_FLAGS "-Wall -Werror -std=c++1z")
 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}/main.cpp
 )
 
diff --git a/src/cli/trace_parser/exe/writer_py.cpp b/src/cli/trace_parser/exe/writer_py.cpp
new file mode 100644 (file)
index 0000000..33f3d05
--- /dev/null
@@ -0,0 +1,254 @@
+/*
+ *  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_py.h"
+
+
+template<typename T>
+static void data_out(const std::string &name, const T val, std::ostream &ios_)
+{
+    ios_ << "'" << name << "': " << val << "," << std::endl;
+}
+
+template<typename T>
+static void data_hex_out(const std::string &name, const T val,
+                         std::ostream &ios_)
+{
+    ios_ << "'" << name << "': 0x" << std::hex << val << std::dec << "," << std::endl;
+}
+
+template<typename T>
+static void args_out(const std::string &name, const T val, std::ostream &ios_)
+{
+    ios_ << "'" << name << "': " << val << "," << std::endl;
+}
+
+template<typename T>
+static void args_vector_out(const std::string &name, const std::vector<T> &val,
+                            std::ostream &ios_)
+{
+    ios_ << "'" << name << "': [";
+
+    for (auto const &item : val)
+        ios_ << item << ", ";
+
+    ios_ << "]," << std::endl;
+}
+
+
+void DataWriterPython::output_val(const std::string &name, const uint8_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name, const uint32_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name, const uint64_t val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name, const float val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name, const double val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name,
+                                  const std::string &val)
+{
+    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_ << "'," << std::endl;
+}
+
+void DataWriterPython::output_val(const std::string &name, const char val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_val(const std::string &name, const int val)
+{
+    data_out(name, val, ios_);
+}
+
+void DataWriterPython::output_hex(const std::string &name, const uint32_t val)
+{
+    data_hex_out(name, val, ios_);
+}
+
+void DataWriterPython::output_hex(const std::string &name, const uint64_t val)
+{
+    data_hex_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const bool val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const uint8_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const uint32_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const uint64_t val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const float val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name, const double val)
+{
+    args_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name,
+                                   const std::string &val)
+{
+    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_ << "'," << std::endl;
+}
+
+void DataWriterPython::output_args(const std::string &name,
+                                   const std::vector<float> &val, uint64_t p)
+{
+    args_vector_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name,
+                                   const std::vector<uint32_t> &val, uint64_t p)
+{
+    args_vector_out(name, val, ios_);
+}
+
+void DataWriterPython::output_args(const std::string &name)
+{
+    ios_ << "'" << name << "': None," << std::endl;
+}
+
+void DataWriterPython::output_args_hex(const std::string &name,
+                                       const uint64_t val)
+{
+    ios_ << "'" << name << "': 0x" << std::hex << val << std::dec << ","
+         << std::endl;
+}
+
+void DataWriterPython::output_time(const std::string &name, uint64_t val)
+{
+    ios_ << name << ": " << val << "," << std::endl;
+}
+
+void DataWriterPython::output_buf(const std::vector<char> &buf)
+{
+    // TODO No place with implemnetation
+}
+
+void DataWriterPython::start_array(const std::string &name)
+{
+    ios_ << "'" << name << "': {" ;
+}
+
+void DataWriterPython::finish_array()
+{
+    ios_ << "}," << std::endl;
+}
+
+void DataWriterPython::start_object(const std::string &name)
+{
+    ios_ << "'" << name << "':{";
+}
+
+void DataWriterPython::finish_object()
+{
+    ios_ << "}," << std::endl;
+}
+
+void DataWriterPython::start_msg()
+{
+    ios_ << "{";
+}
+
+void DataWriterPython::finish_msg()
+{
+    ios_ << "}," << std::endl;
+}
+
+void DataWriterPython::start_file(const uint32_t version)
+{
+    ios_ << "trace_info = {" << std::endl;
+    ios_ << "'protocol_version': " << version << std::endl;
+    ios_ << "}" << std::endl;
+
+    ios_ << "trace_obj = [" << std::endl;
+}
+
+void DataWriterPython::finish_file()
+{
+    ios_ << "]" << std::endl;
+}
diff --git a/src/cli/trace_parser/exe/writer_py.h b/src/cli/trace_parser/exe/writer_py.h
new file mode 100644 (file)
index 0000000..69b7c02
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ *  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_PY_H__
+#define __WRITER_PY_H__
+
+
+#include <data_writer.h>
+
+
+class DataWriterPython: 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<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_PY_H__ */
index de4d92e7562f6dfef0ad082de033848d5417d574..d706babffebe3fdfef9f6e7b556a331252afc747 100644 (file)
@@ -32,6 +32,7 @@
 #include <memory>
 #include <data_writer.h>
 #include "writer_text.h"
+#include "writer_py.h"
 #include "writers.h"
 
 
@@ -48,7 +49,7 @@ std::shared_ptr<DataWriter> get_data_writer(const std::string &type,
     if (!type.compare(text_long) || !type.compare(text_short))
         return std::shared_ptr<DataWriter>(new DataWriterText(ios));
     else if (!type.compare(py_long) || !type.compare(py_short))
-        throw std::logic_error("Python output type unsupported");
+        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");