• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 #include "pw_trace_tokenized/trace_buffer_log.h"
17 
18 #include <span>
19 
20 #include "pw_base64/base64.h"
21 #include "pw_log/log.h"
22 #include "pw_string/string_builder.h"
23 #include "pw_trace_tokenized/trace_buffer.h"
24 
25 namespace pw {
26 namespace trace {
27 namespace {
28 
29 constexpr int kMaxEntrySize = PW_TRACE_BUFFER_MAX_BLOCK_SIZE_BYTES;
30 constexpr int kMaxEntrySizeBase64 = pw::base64::EncodedSize(kMaxEntrySize);
31 constexpr int kLineLength = 80;
32 
33 class ScopedTracePause {
34  public:
ScopedTracePause()35   ScopedTracePause() : was_enabled_(pw_trace_IsEnabled()) {
36     PW_TRACE_SET_ENABLED(false);
37   }
~ScopedTracePause()38   ~ScopedTracePause() { PW_TRACE_SET_ENABLED(was_enabled_); }
39 
40  private:
41   bool was_enabled_;
42 };
43 
44 }  // namespace
45 
DumpTraceBufferToLog()46 pw::Status DumpTraceBufferToLog() {
47   std::byte line_buffer[kLineLength] = {};
48   std::byte entry_buffer[kMaxEntrySize + 1] = {};
49   char entry_base64_buffer[kMaxEntrySizeBase64] = {};
50   pw::StringBuilder line_builder(line_buffer);
51   ScopedTracePause pause_trace;
52   pw::ring_buffer::PrefixedEntryRingBuffer* trace_buffer =
53       pw::trace::GetBuffer();
54   size_t bytes_read = 0;
55   PW_LOG_INFO("[TRACE] begin");
56   while (trace_buffer->PeekFront(std::span(entry_buffer).subspan(1),
57                                  &bytes_read) != pw::Status::OutOfRange()) {
58     trace_buffer->PopFront()
59         .IgnoreError();  // TODO(pwbug/387): Handle Status properly
60     entry_buffer[0] = static_cast<std::byte>(bytes_read);
61     // The entry buffer is formatted as (size, entry) with an extra byte as
62     // a header to the entry. The calcuation of bytes_read + 1 represents
63     // the extra size header.
64     size_t to_write =
65         pw::base64::Encode(std::span(entry_buffer, bytes_read + 1),
66                            std::span(entry_base64_buffer));
67     size_t space_left = line_builder.max_size() - line_builder.size();
68     size_t written = 0;
69     while (to_write - written >= space_left) {
70       line_builder.append(entry_base64_buffer + written, space_left);
71       PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str());
72       line_builder.clear();
73       written += space_left;
74       space_left = line_builder.max_size();
75     }
76     line_builder.append(entry_base64_buffer + written, to_write - written);
77   }
78   if (!line_builder.empty()) {
79     PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str());
80   }
81   PW_LOG_INFO("[TRACE] end");
82   return pw::OkStatus();
83 }
84 
85 }  // namespace trace
86 }  // namespace pw
87