pw_trace: tokenizer filter example

Add a example for how to filter out traces. in this example, all traces
from the processing task which aren't trace_id 3 are skipped.

Change-Id: I8c1ad1931cf2319df85c13c01b6d41e359e6cbb6
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/13922
Commit-Queue: Rob Oliver <rgoliver@google.com>
Reviewed-by: (☞゚∀゚)☞ Tennessee Carmel-Veilleux  <tennessee@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 1398351..6e6d6bc 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -112,6 +112,7 @@
       deps += [
         "$dir_pw_trace:trace_example_basic",
         "$dir_pw_trace_tokenized:trace_tokenized_example_basic",
+        "$dir_pw_trace_tokenized:trace_tokenized_example_filter",
         "$dir_pw_trace_tokenized:trace_tokenized_example_trigger",
       ]
     }
diff --git a/pw_trace_tokenized/BUILD b/pw_trace_tokenized/BUILD
index a4c02f1..6b853ea 100644
--- a/pw_trace_tokenized/BUILD
+++ b/pw_trace_tokenized/BUILD
@@ -163,3 +163,14 @@
     ],
     srcs = [ "example/trigger.cc" ]
 )
+
+pw_cc_binary(
+    name = "trace_tokenized_example_filter",
+    deps = [
+        ":pw_trace_example_to_file",
+        "//pw_log",
+        "//dir_pw_trace",
+        "//dir_pw_trace:pw_trace_sample_app",
+    ],
+    srcs = [ "example/filter.cc" ]
+)
diff --git a/pw_trace_tokenized/BUILD.gn b/pw_trace_tokenized/BUILD.gn
index a8baa57..74843b2 100644
--- a/pw_trace_tokenized/BUILD.gn
+++ b/pw_trace_tokenized/BUILD.gn
@@ -150,3 +150,13 @@
   ]
   sources = [ "example/trigger.cc" ]
 }
+
+pw_executable("trace_tokenized_example_filter") {
+  deps = [
+    ":trace_example_to_file",
+    "$dir_pw_log",
+    "$dir_pw_trace",
+    "$dir_pw_trace:trace_sample_app",
+  ]
+  sources = [ "example/filter.cc" ]
+}
diff --git a/pw_trace_tokenized/docs.rst b/pw_trace_tokenized/docs.rst
index aeee29e..c588106 100644
--- a/pw_trace_tokenized/docs.rst
+++ b/pw_trace_tokenized/docs.rst
@@ -202,3 +202,12 @@
 `PW_TRACE_REF_DATA` to specify a start and stop event for the capture. This can
 be useful if the trace buffer is small and you wish to capture a specific
 series of events.
+
+Filter
+------
+The filter example demonstrates how a callback can be used to filter which trace
+events get processed and saved. In this example all events from the processing
+task which don't have traceId equal to 3 are removed. Both the other task traces
+are not removed. This can be a useful feature while debugging as it limits the
+amount of events which get stored to the buffer, and only saves the events of
+interest.
diff --git a/pw_trace_tokenized/example/filter.cc b/pw_trace_tokenized/example/filter.cc
new file mode 100644
index 0000000..8cbe3f4
--- /dev/null
+++ b/pw_trace_tokenized/example/filter.cc
@@ -0,0 +1,71 @@
+// Copyright 2020 The Pigweed Authors
+//
+// 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
+//
+//     https://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.
+//==============================================================================
+// BUID
+// ninja -C out
+// host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_filter
+//
+// RUN
+// .out/host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_filter
+// trace.bin
+//
+// DECODE
+// python pw_trace_tokenized/py/trace_tokenized.py -i trace.bin -o trace.json
+// ./out/host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_basic
+//
+// VIEW
+// In chrome navigate to chrome://tracing, and load the trace.json file.
+
+#include "pw_log/log.h"
+#include "pw_trace/example/sample_app.h"
+#include "pw_trace/trace.h"
+#include "pw_trace_tokenized/example/trace_to_file.h"
+#include "pw_trace_tokenized/trace_callback.h"
+#include "pw_trace_tokenized/trace_tokenized.h"
+
+pw_trace_TraceEventReturnFlags TraceEventCallback(void* user_data,
+                                                  uint32_t trace_ref,
+                                                  pw_trace_EventType event_type,
+                                                  const char* module,
+                                                  uint32_t trace_id,
+                                                  uint8_t flags) {
+  // Filter out all traces from processing task, which aren't traceId 3
+  PW_UNUSED(user_data);
+  PW_UNUSED(event_type);
+  PW_UNUSED(trace_ref);
+  PW_UNUSED(flags);
+  static constexpr uint32_t kFilterId = 3;
+  return (strcmp("Processing", module) == 0 && trace_id != kFilterId)
+             ? PW_TRACE_EVENT_RETURN_FLAGS_SKIP_EVENT
+             : 0;
+}
+
+int main(int argc, char** argv) {  // Take filename as arg
+  if (argc != 2) {
+    PW_LOG_ERROR("Expected output file name as argument.\n");
+    return -1;
+  }
+
+  // Register filter callback
+  pw::trace::Callbacks::Instance().RegisterEventCallback(TraceEventCallback);
+
+  PW_TRACE_SET_ENABLED(true);  // Start with tracing enabled
+
+  // Dump trace data to the file passed in.
+  pw::trace::TraceToFile trace_to_file(argv[1]);
+
+  PW_LOG_INFO("Running filter example...");
+  RunTraceSampleApp();
+  return 0;
+}
\ No newline at end of file