1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 //==============================================================================
15 // BUILD
16 // ninja -C out
17 // pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_filter
18 //
19 // RUN
20 // ./out/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_filter
21 // trace.bin
22 //
23 // DECODE
24 // python pw_trace_tokenized/py/trace_tokenized.py -i trace.bin -o trace.json
25 // ./out/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_basic#trace
26 //
27 // VIEW
28 // In chrome navigate to chrome://tracing, and load the trace.json file.
29
30 #include "pw_log/log.h"
31 #include "pw_trace/example/sample_app.h"
32 #include "pw_trace/trace.h"
33 #include "pw_trace_tokenized/example/trace_to_file.h"
34 #include "pw_trace_tokenized/trace_callback.h"
35 #include "pw_trace_tokenized/trace_tokenized.h"
36
TraceEventCallback(void *,pw_trace_tokenized_TraceEvent * event)37 pw_trace_TraceEventReturnFlags TraceEventCallback(
38 void* /* user_data */, pw_trace_tokenized_TraceEvent* event) {
39 // Filter out all traces from processing task, which aren't traceId 3
40 static constexpr uint32_t kFilterId = 3;
41 return (strcmp("Processing", event->module) == 0 &&
42 event->trace_id != kFilterId)
43 ? PW_TRACE_EVENT_RETURN_FLAGS_SKIP_EVENT
44 : 0;
45 }
46
main(int argc,char ** argv)47 int main(int argc, char** argv) { // Take filename as arg
48 if (argc != 2) {
49 PW_LOG_ERROR("Expected output file name as argument.\n");
50 return -1;
51 }
52
53 // Register filter callback
54 pw::trace::Callbacks& callbacks = pw::trace::GetCallbacks();
55 callbacks.RegisterEventCallback(TraceEventCallback)
56 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
57
58 PW_TRACE_SET_ENABLED(true); // Start with tracing enabled
59
60 // Dump trace data to the file passed in.
61 pw::trace::TraceToFile trace_to_file(callbacks, argv[1]);
62
63 PW_LOG_INFO("Running filter example...");
64 RunTraceSampleApp();
65 return 0;
66 }
67