Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_trace_tokenized / trace_buffer_log.cc
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:
35   ScopedTracePause() : was_enabled_(pw_trace_IsEnabled()) {
36     PW_TRACE_SET_ENABLED(false);
37   }
38   ~ScopedTracePause() { PW_TRACE_SET_ENABLED(was_enabled_); }
39
40  private:
41   bool was_enabled_;
42 };
43
44 }  // namespace
45
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     entry_buffer[0] = static_cast<std::byte>(bytes_read);
60     // The entry buffer is formatted as (size, entry) with an extra byte as
61     // a header to the entry. The calcuation of bytes_read + 1 represents
62     // the extra size header.
63     size_t to_write =
64         pw::base64::Encode(std::span(entry_buffer, bytes_read + 1),
65                            std::span(entry_base64_buffer));
66     size_t space_left = line_builder.max_size() - line_builder.size();
67     size_t written = 0;
68     while (to_write - written >= space_left) {
69       line_builder.append(entry_base64_buffer + written, space_left);
70       PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str());
71       line_builder.clear();
72       written += space_left;
73       space_left = line_builder.max_size();
74     }
75     line_builder.append(entry_base64_buffer + written, to_write - written);
76   }
77   if (line_builder.size() > 0) {
78     PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str());
79   }
80   PW_LOG_INFO("[TRACE] end");
81   return pw::OkStatus();
82 }
83
84 }  // namespace trace
85 }  // namespace pw