Skip to content

Commit 6cfb110

Browse files
authored
cleanup(storage): make logs more readable (#9761)
1 parent a9d2dd5 commit 6cfb110

10 files changed

+35
-288
lines changed

google/cloud/internal/binary_data_as_debug_string.cc

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,72 +13,24 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/internal/binary_data_as_debug_string.h"
16-
#include <array>
16+
#include <algorithm>
1717
#include <cctype>
18-
#include <limits>
1918

2019
namespace google {
2120
namespace cloud {
2221
namespace rest_internal {
2322
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
23+
2424
std::string BinaryDataAsDebugString(char const* data, std::size_t size,
2525
std::size_t max_output_bytes) {
26-
// We want about 2/3 of a standard 80 column terminal to be used by the hex
27-
// representation and the other 1/3 (because it is half as wide) with the
28-
// text representation. Setting this value to 24 uses 72 columns: 48 for the
29-
// hex representation, 24 for text, and one space. We could use 25 or 26, we
30-
// chose (somewhat arbitrarily) 24 as it is 16 + 8 and thus more "round" in
31-
// base 2.
32-
auto constexpr kTextWidth = 24;
33-
std::string result;
34-
std::string text_column(kTextWidth, ' ');
35-
std::string hex_column(2 * kTextWidth, ' ');
36-
37-
// Capture everything (which we use anyway) because:
38-
// - clang-tidy complains if you capture a constexpr or const
39-
// - MSVC does not automatically capture constexpr types unless we do this
40-
auto flush = [&] {
41-
result += text_column;
42-
result += ' ';
43-
result += hex_column;
44-
result += '\n';
45-
text_column = std::string(kTextWidth, ' ');
46-
hex_column = std::string(2 * kTextWidth, ' ');
47-
};
48-
49-
// Limit the output to the first `max_output_bytes`.
50-
std::size_t n = size;
51-
if (max_output_bytes > 0 && max_output_bytes < size) {
52-
n = max_output_bytes;
53-
}
54-
55-
std::size_t count = 0;
56-
for (char const* c = data; c != data + n; ++c) {
57-
// std::isprint() actually takes an int argument, signed, without this
58-
// explicit conversion MSVC in Debug mode asserts an invalid argument, and
59-
// pops up a nice dialog box that breaks the CI builds.
60-
int cval = static_cast<unsigned char>(*c);
61-
if (std::isprint(cval) != 0) {
62-
text_column[count] = *c;
63-
} else {
64-
text_column[count] = '.';
65-
}
66-
auto constexpr kCharHexWidth = 2;
67-
std::array<char, kCharHexWidth + 1> buf{};
68-
snprintf(buf.data(), buf.size(), "%02x", cval);
69-
hex_column[2 * count] = buf[0];
70-
hex_column[2 * count + 1] = buf[1];
71-
++count;
72-
if (count == kTextWidth) {
73-
flush();
74-
count = 0;
75-
}
76-
}
77-
if (count != 0) {
78-
flush();
79-
}
80-
return result;
26+
auto dump = std::string{
27+
data, max_output_bytes == 0 ? size : std::min(size, max_output_bytes)};
28+
std::transform(dump.begin(), dump.end(), dump.begin(),
29+
[](auto c) { return std::isprint(c) ? c : '.'; });
30+
if (max_output_bytes == 0 || size <= max_output_bytes) return dump;
31+
return dump + "...<truncated>...";
8132
}
33+
8234
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
8335
} // namespace rest_internal
8436
} // namespace cloud

google/cloud/internal/binary_data_as_debug_string.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ namespace google {
2222
namespace cloud {
2323
namespace rest_internal {
2424
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
25+
2526
/**
2627
* Formats a block of data for debug printing.
2728
*
28-
* Takes a block of data, possible with non-printable characters and creates
29-
* a string with two columns. The first column is 24 characters wide and has
30-
* the non-printable characters replaced by periods. The second column is 48
31-
* characters wide and contains the hexdump of the data. The columns are
32-
* separated by a single space.
29+
* Converts non-printable characters to '.'. If requested, it truncates the
30+
* output and adds a '...<truncated>...' marker when it does so.
3331
*/
3432
std::string BinaryDataAsDebugString(char const* data, std::size_t size,
3533
std::size_t max_output_bytes = 0);
34+
3635
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
3736
} // namespace rest_internal
3837
} // namespace cloud

google/cloud/internal/binary_data_as_debug_string_test.cc

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,20 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2222
namespace {
2323

2424
TEST(BinaryDataAsDebugStringTest, Simple) {
25-
auto actual = BinaryDataAsDebugString("123abc", 6);
26-
EXPECT_EQ(
27-
"123abc "
28-
"313233616263 \n",
29-
actual);
30-
}
31-
32-
TEST(BinaryDataAsDebugStringTest, Multiline) {
33-
auto actual =
34-
BinaryDataAsDebugString(" 123456789 123456789 123456789 123456789", 40);
35-
EXPECT_EQ(
36-
" 123456789 123456789 123 "
37-
"203132333435363738392031323334353637383920313233\n"
38-
"456789 123456789 "
39-
"34353637383920313233343536373839 \n",
40-
actual);
41-
}
42-
43-
TEST(BinaryDataAsDebugStringTest, Blanks) {
44-
auto actual = BinaryDataAsDebugString("\n \r \t \v \b \a \f ", 14);
45-
EXPECT_EQ(
46-
". . . . . . . "
47-
"0a200d2009200b20082007200c20 \n",
48-
actual);
49-
}
50-
51-
TEST(BinaryDataAsDebugStringTest, NonPrintable) {
52-
auto actual = BinaryDataAsDebugString("\x03\xf1 abcd", 7);
53-
EXPECT_EQ(
54-
".. abcd "
55-
"03f12061626364 \n",
56-
actual);
57-
}
58-
59-
TEST(BinaryDataAsDebugStringTest, Limit) {
60-
auto actual = BinaryDataAsDebugString(
61-
" 123456789 123456789 123456789 123456789", 40, 24);
62-
EXPECT_EQ(
63-
" 123456789 123456789 123 "
64-
"203132333435363738392031323334353637383920313233\n",
65-
actual);
25+
struct TestCase {
26+
std::string input;
27+
std::size_t max;
28+
std::string expected;
29+
} cases[] = {
30+
{"123abc", 0, "123abc"},
31+
{"234abc", 3, "234...<truncated>..."},
32+
{"3\n4\n5abc", 0, "3.4.5abc"},
33+
{"3\n4\n5a\n\n\nbc", 5, "3.4.5...<truncated>..."},
34+
};
35+
for (auto const& t : cases) {
36+
EXPECT_EQ(t.expected,
37+
BinaryDataAsDebugString(t.input.data(), t.input.size(), t.max));
38+
}
6639
}
6740

6841
} // namespace

google/cloud/storage/google_cloud_cpp_storage.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ google_cloud_cpp_storage_srcs = [
148148
"idempotency_policy.cc",
149149
"internal/access_control_common_parser.cc",
150150
"internal/access_token_credentials.cc",
151-
"internal/binary_data_as_debug_string.cc",
152151
"internal/bucket_access_control_parser.cc",
153152
"internal/bucket_acl_requests.cc",
154153
"internal/bucket_metadata_parser.cc",

google/cloud/storage/google_cloud_cpp_storage.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ add_library(
4747
internal/access_control_common_parser.h
4848
internal/access_token_credentials.cc
4949
internal/access_token_credentials.h
50-
internal/binary_data_as_debug_string.cc
5150
internal/binary_data_as_debug_string.h
5251
internal/bucket_access_control_parser.cc
5352
internal/bucket_access_control_parser.h
@@ -437,7 +436,6 @@ if (BUILD_TESTING)
437436
internal/access_control_common_parser_test.cc
438437
internal/access_control_common_test.cc
439438
internal/access_token_credentials_test.cc
440-
internal/binary_data_as_debug_string_test.cc
441439
internal/bucket_acl_requests_test.cc
442440
internal/bucket_requests_test.cc
443441
internal/complex_option_test.cc

google/cloud/storage/internal/binary_data_as_debug_string.cc

Lines changed: 0 additions & 87 deletions
This file was deleted.

google/cloud/storage/internal/binary_data_as_debug_string.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_BINARY_DATA_AS_DEBUG_STRING_H
1717

1818
#include "google/cloud/storage/version.h"
19-
#include <string>
19+
#include "google/cloud/internal/binary_data_as_debug_string.h"
2020

2121
namespace google {
2222
namespace cloud {
2323
namespace storage {
2424
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2525
namespace internal {
26-
/**
27-
* Formats a block of data for debug printing.
28-
*
29-
* Takes a block of data, possible with non-printable characters and creates
30-
* a string with two columns. The first column is 24 characters wide and has
31-
* the non-printable characters replaced by periods. The second column is 48
32-
* characters wide and contains the hexdump of the data. The columns are
33-
* separated by a single space.
34-
*/
35-
std::string BinaryDataAsDebugString(char const* data, std::size_t size,
36-
std::size_t max_output_bytes = 0);
26+
27+
using ::google::cloud::rest_internal::BinaryDataAsDebugString;
28+
3729
} // namespace internal
3830
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
3931
} // namespace storage

google/cloud/storage/internal/binary_data_as_debug_string_test.cc

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)