Pigweed
 
Loading...
Searching...
No Matches
log_bytes.h
1// Copyright 2024 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#pragma once
16
17#include <array>
18#include <cstddef>
19
20#include "pw_bytes/span.h"
21#include "pw_hex_dump/hex_dump.h"
22#include "pw_log/log.h"
23#include "pw_log/options.h"
24
25namespace pw::dump {
26
79template <std::size_t kBytesPerLine = 16>
80inline void LogBytes(int log_level, pw::ConstByteSpan bytes) {
81 // An input byte uses 3 bytes for the hex representation plus 1 for ASCII.
82 // 8 bytes go to offset, padding, and string termination.
83 const std::size_t kMaxLogLineLength = 8 + 4 * kBytesPerLine;
84
85 if (kBytesPerLine == 0) {
86 return;
87 }
88
89 std::array<char, kMaxLogLineLength> temp{};
90 const auto flags = pw::dump::FormattedHexDumper::Flags{
91 .bytes_per_line = kBytesPerLine,
92 .group_every = 1,
93 .show_ascii = true,
94 .show_header = false,
95 .prefix_mode = pw::dump::FormattedHexDumper::AddressMode::kOffset};
96 pw::dump::FormattedHexDumper hex_dumper(temp, flags);
97 if (hex_dumper.BeginDump(bytes).ok()) {
98 while (hex_dumper.DumpLine().ok()) {
99 PW_LOG(log_level,
100 PW_LOG_LEVEL,
101 PW_LOG_MODULE_NAME,
102 PW_LOG_FLAGS,
103 "%s",
104 temp.data());
105 }
106 }
107}
108
109} // namespace pw::dump
Definition: hex_dump.h:81
Definition: hex_dump.h:89
uint8_t bytes_per_line
Sets the number of source data bytes to print in each formatted line.
Definition: hex_dump.h:91