C/C++ API Reference
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
28
81template <std::size_t kBytesPerLine = 16>
82inline void LogBytes(int log_level, pw::ConstByteSpan bytes) {
83 // An input byte uses 3 bytes for the hex representation plus 1 for ASCII.
84 // 8 bytes go to offset, padding, and string termination.
85 const std::size_t kMaxLogLineLength = 8 + 4 * kBytesPerLine;
86
87 if (kBytesPerLine == 0) {
88 return;
89 }
90
91 std::array<char, kMaxLogLineLength> temp{};
92 const auto flags = pw::dump::FormattedHexDumper::Flags{
93 .bytes_per_line = kBytesPerLine,
94 .group_every = 1,
95 .show_ascii = true,
96 .show_header = false,
97 .prefix_mode = pw::dump::FormattedHexDumper::AddressMode::kOffset};
98 pw::dump::FormattedHexDumper hex_dumper(temp, flags);
99 if (hex_dumper.BeginDump(bytes).ok()) {
100 while (hex_dumper.DumpLine().ok()) {
101 PW_LOG(log_level,
105 "%s",
106 temp.data());
107 }
108 }
109}
110
111} // namespace pw::dump
constexpr bool ok() const
Definition: status.h:214
Definition: hex_dump.h:84
void LogBytes(int log_level, pw::ConstByteSpan bytes)
Definition: log_bytes.h:82
uint8_t bytes_per_line
Sets the number of source data bytes to print in each formatted line.
Definition: hex_dump.h:94
Status BeginDump(ConstByteSpan data)
#define PW_LOG_LEVEL
Definition: options.h:54
#define PW_LOG_MODULE_NAME
Definition: options.h:45
#define PW_LOG_FLAGS
Definition: options.h:62
#define PW_LOG( level, verbosity, module, flags,...)
Definition: log.h:73
Hexdump utilities.
Definition: hex_dump.h:24
Definition: hex_dump.h:92