xref: /aosp_15_r20/external/pigweed/pw_trace_tokenized/example/rpc.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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 /*
16 
17 NOTE
18 To use this example you need to enable nanopb. One option is to first install
19 the nanopb package by running the command:
20 
21 pw package install nanopb
22 
23 Next add nanopb to your args.gn by running the command
24 
25 gn args out
26 
27 Then add the following line to that text file:
28 
29 dir_pw_third_party_nanopb = getenv("PW_PACKAGE_ROOT") + "/nanopb"
30 
31 BUILD
32 ninja -C out \
33 pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_rpc
34 
35 RUN
36 ./out/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_rpc
37 
38 DECODE
39 python pw_trace_tokenized/py/pw_trace_tokenized/get_trace.py \
40  -s localhost:33000 \
41  -o trace.json \
42  -t \
43  out/pw_strict_host_clang_debug/obj/pw_trace_tokenized/bin/trace_tokenized_example_rpc\
44  pw_trace_tokenized/pw_trace_protos/trace_rpc.proto
45 
46 VIEW
47 In chrome navigate to chrome://tracing, and load the trace.json file.
48 */
49 #include <thread>
50 
51 #include "pw_assert/check.h"
52 #include "pw_log/log.h"
53 #include "pw_rpc/server.h"
54 #include "pw_rpc_system_server/rpc_server.h"
55 #include "pw_trace/example/sample_app.h"
56 #include "pw_trace_tokenized/trace_rpc_service_nanopb.h"
57 #include "pw_trace_tokenized/trace_tokenized.h"
58 
59 namespace {
60 
61 pw::trace::TraceService trace_service(pw::trace::GetTokenizedTracer());
62 
RpcThread()63 void RpcThread() {
64   pw::rpc::system_server::Init();
65 
66   // Set up the server and start processing data.
67   pw::rpc::system_server::Server().RegisterService(trace_service);
68   PW_CHECK_OK(pw::rpc::system_server::Start());
69 }
70 
71 }  // namespace
72 
main()73 int main() {
74   std::thread rpc_thread(RpcThread);
75 
76   // Enable tracing.
77   PW_TRACE_SET_ENABLED(true);
78 
79   PW_LOG_INFO("Running basic trace example...\n");
80   RunTraceSampleApp();
81   return 0;
82 }
83