[PATCH 07/12] record: Add filter base class

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Mar 16 10:50:27 UTC 2020


Update #3904.
---
 trace/record/client.h              | 21 ++++++++++++++++++++-
 trace/record/record-client-base.cc | 33 +++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/trace/record/client.h b/trace/record/client.h
index 2fe8d51..636fb0b 100644
--- a/trace/record/client.h
+++ b/trace/record/client.h
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: BSD-2-Clause */
 
 /*
- * Copyright (C) 2018, 2019 embedded brains GmbH (http://www.embedded-brains.de)
+ * Copyright (C) 2018, 2020 embedded brains GmbH (http://www.embedded-brains.de)
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -37,6 +37,7 @@
 #include <csignal>
 #include <cstring>
 #include <iostream>
+#include <list>
 #include <map>
 #include <stdexcept>
 #include <string>
@@ -98,6 +99,19 @@ class ConfigFile {
                         const char* value);
 };
 
+class Filter {
+ public:
+  Filter() = default;
+
+  Filter(const Filter&) = default;
+
+  Filter& operator=(const Filter&) = default;
+
+  virtual ~Filter() = default;
+
+  virtual bool Run(void** buf, size_t* n) = 0;
+};
+
 class Client {
  public:
   Client() = default;
@@ -114,6 +128,8 @@ class Client {
 
   void RequestStop() { stop_ = 1; }
 
+  void AddFilter(Filter* filter) { filters_.push_back(filter); }
+
   void Destroy();
 
   void set_limit(uint64_t limit) { limit_ = limit; }
@@ -127,9 +143,12 @@ class Client {
 
  private:
   rtems_record_client_context base_;
+  std::list<Filter*> filters_;
   FileDescriptor input_;
   sig_atomic_t stop_ = 0;
   uint64_t limit_ = 0;
+
+  void Flush();
 };
 
 #endif  // RTEMS_TOOLS_TRACE_RECORD_CLIENT_H_
diff --git a/trace/record/record-client-base.cc b/trace/record/record-client-base.cc
index 8c467d4..f022db4 100644
--- a/trace/record/record-client-base.cc
+++ b/trace/record/record-client-base.cc
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: BSD-2-Clause */
 
 /*
- * Copyright (C) 2018, 2019 embedded brains GmbH (http://www.embedded-brains.de)
+ * Copyright (C) 2018, 2020 embedded brains GmbH (http://www.embedded-brains.de)
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -144,6 +144,24 @@ int ConfigFile::INIHandler(void* user,
   return 0;
 }
 
+void Client::Flush() {
+  while (true) {
+    void* p = nullptr;
+    size_t n = 0;
+    for (auto filter : filters_) {
+      if (!filter->Run(&p, &n)) {
+        break;
+      }
+    }
+
+    if (n > 0) {
+      rtems_record_client_run(&base_, p, n);
+    } else {
+      break;
+    }
+  }
+}
+
 void Client::Run() {
   uint64_t todo = UINT64_MAX;
 
@@ -159,9 +177,20 @@ void Client::Run() {
       break;
     }
 
-    rtems_record_client_run(&base_, buf, static_cast<size_t>(n));
+    void* p = &buf[0];
+    size_t k = static_cast<size_t>(n);
+    for (auto filter : filters_) {
+      if (!filter->Run(&p, &k)) {
+        std::cerr << "error: input filter failure" << std::endl;
+        return;
+      }
+    }
+
+    rtems_record_client_run(&base_, p, k);
     todo -= static_cast<size_t>(n);
   }
+
+  Flush();
 }
 
 void Client::Destroy() {
-- 
2.16.4



More information about the devel mailing list