Prashanth Swaminathan | 0a9129c | 2020-09-14 16:27:24 -0700 | [diff] [blame] | 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), |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 57 | &bytes_read) != pw::Status::OutOfRange()) { |
Adrien Larbanet | d1ca56c | 2021-06-10 14:20:45 +0000 | [diff] [blame] | 58 | trace_buffer->PopFront() |
| 59 | .IgnoreError(); // TODO(pwbug/387): Handle Status properly |
Prashanth Swaminathan | 0a9129c | 2020-09-14 16:27:24 -0700 | [diff] [blame] | 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 | } |
Ted Pudlik | 10ec2bd | 2021-10-27 21:17:09 +0000 | [diff] [blame] | 78 | if (!line_builder.empty()) { |
Prashanth Swaminathan | 0a9129c | 2020-09-14 16:27:24 -0700 | [diff] [blame] | 79 | PW_LOG_INFO("[TRACE] data: %s", line_builder.c_str()); |
| 80 | } |
| 81 | PW_LOG_INFO("[TRACE] end"); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 82 | return pw::OkStatus(); |
Prashanth Swaminathan | 0a9129c | 2020-09-14 16:27:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace trace |
| 86 | } // namespace pw |