blob: 11019bab18dabcb03e4d33598a16ff30b74954dc [file] [log] [blame]
Prashanth Swaminathan0a9129c2020-09-14 16:27:24 -07001// 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
25namespace pw {
26namespace trace {
27namespace {
28
29constexpr int kMaxEntrySize = PW_TRACE_BUFFER_MAX_BLOCK_SIZE_BYTES;
30constexpr int kMaxEntrySizeBase64 = pw::base64::EncodedSize(kMaxEntrySize);
31constexpr int kLineLength = 80;
32
33class 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
46pw::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),
Wyatt Heplerd78f7c62020-09-28 14:27:32 -070057 &bytes_read) != pw::Status::OutOfRange()) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000058 trace_buffer->PopFront()
59 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Prashanth Swaminathan0a9129c2020-09-14 16:27:24 -070060 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 }
Ted Pudlik10ec2bd2021-10-27 21:17:09 +000078 if (!line_builder.empty()) {
Prashanth Swaminathan0a9129c2020-09-14 16:27:24 -070079 PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str());
80 }
81 PW_LOG_INFO("[TRACE] end");
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080082 return pw::OkStatus();
Prashanth Swaminathan0a9129c2020-09-14 16:27:24 -070083}
84
85} // namespace trace
86} // namespace pw