aboutsummaryrefslogtreecommitdiff
path: root/pw_trace_tokenized/example/filter.cc
blob: a16efb478a0558861b5f08a749e486bbe4e079fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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.
//==============================================================================
// BUILD
// ninja -C out
// pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_filter
//
// RUN
// ./out/pw_strict_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/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_basic#trace
//
// 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 */, pw_trace_tokenized_TraceEvent* event) {
  // Filter out all traces from processing task, which aren't traceId 3
  static constexpr uint32_t kFilterId = 3;
  return (strcmp("Processing", event->module) == 0 &&
          event->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& callbacks = pw::trace::GetCallbacks();
  callbacks.RegisterEventCallback(TraceEventCallback)
      .IgnoreError();  // TODO: b/242598609 - Handle Status properly

  PW_TRACE_SET_ENABLED(true);  // Start with tracing enabled

  // Dump trace data to the file passed in.
  pw::trace::TraceToFile trace_to_file(callbacks, argv[1]);

  PW_LOG_INFO("Running filter example...");
  RunTraceSampleApp();
  return 0;
}