1 // Copyright 2023 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 #pragma once 15 16 #include <cstddef> 17 #include <type_traits> 18 19 #include "pw_log_tokenized/config.h" 20 #include "pw_tokenizer/base64.h" 21 22 namespace pw::log_tokenized { 23 24 // Minimum capacity for a string that to hold the Base64-encoded version of a 25 // PW_LOG_TOKENIZED_ENCODING_BUFFER_SIZE_BYTES tokenized message. This is the 26 // capacity needed to encode to a `pw::InlineString` and does not include a null 27 // terminator. 28 inline constexpr size_t kBase64EncodedBufferSizeBytes = 29 tokenizer::Base64EncodedBufferSize(kEncodingBufferSizeBytes); 30 31 /// Encodes a binary tokenized log in the prefixed Base64 format. Calls 32 /// @cpp_func{pw::tokenizer::PrefixedBase64Encode} for a string sized to fit a 33 /// `kEncodingBufferSizeBytes` tokenized log. PrefixedBase64Encode(span<const std::byte> binary_message)34inline InlineString<kBase64EncodedBufferSizeBytes> PrefixedBase64Encode( 35 span<const std::byte> binary_message) { 36 return tokenizer::PrefixedBase64Encode<kEncodingBufferSizeBytes>( 37 binary_message); 38 } 39 40 #ifndef PW_EXCLUDE_FROM_DOXYGEN // Doxygen fails to parse this, so skip it. 41 42 template <typename T, 43 typename = std::enable_if_t<sizeof(T) == sizeof(std::byte)>> PrefixedBase64Encode(const T * log_buffer,size_t size_bytes)44inline InlineString<kBase64EncodedBufferSizeBytes> PrefixedBase64Encode( 45 const T* log_buffer, size_t size_bytes) { 46 return PrefixedBase64Encode(as_bytes(span(log_buffer, size_bytes))); 47 } 48 49 #endif // PW_EXCLUDE_FROM_DOXYGEN 50 51 } // namespace pw::log_tokenized 52