Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hex strings to prevent broken log lines #13128

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Firestore/core/src/remote/grpc_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ GrpcStream::GrpcStream(
}

GrpcStream::~GrpcStream() {
LOG_DEBUG("GrpcStream('%s'): destroying stream", this);
LOG_DEBUG("GrpcStream('%x'): destroying stream", this);
HARD_ASSERT(completions_.empty(),
"GrpcStream is being destroyed without proper shutdown");
MaybeUnregister();
Expand Down Expand Up @@ -160,14 +160,14 @@ void GrpcStream::MaybeWrite(absl::optional<BufferedWrite> maybe_write) {
}

void GrpcStream::FinishImmediately() {
LOG_DEBUG("GrpcStream('%s'): finishing without notifying observers", this);
LOG_DEBUG("GrpcStream('%x'): finishing without notifying observers", this);

Shutdown();
UnsetObserver();
}

void GrpcStream::FinishAndNotify(const Status& status) {
LOG_DEBUG("GrpcStream('%s'): finishing and notifying observers", this);
LOG_DEBUG("GrpcStream('%x'): finishing and notifying observers", this);

Shutdown();

Expand All @@ -181,7 +181,7 @@ void GrpcStream::FinishAndNotify(const Status& status) {
}

void GrpcStream::Shutdown() {
LOG_DEBUG("GrpcStream('%s'): shutting down; completions: %s, is finished: %s",
LOG_DEBUG("GrpcStream('%x'): shutting down; completions: %s, is finished: %s",
this, completions_.size(), is_grpc_call_finished_);

MaybeUnregister();
Expand Down Expand Up @@ -216,7 +216,7 @@ void GrpcStream::MaybeUnregister() {
}

void GrpcStream::FinishGrpcCall(const OnSuccess& callback) {
LOG_DEBUG("GrpcStream('%s'): finishing the underlying call", this);
LOG_DEBUG("GrpcStream('%x'): finishing the underlying call", this);

HARD_ASSERT(!is_grpc_call_finished_, "FinishGrpcCall called twice");
is_grpc_call_finished_ = true;
Expand All @@ -229,7 +229,7 @@ void GrpcStream::FinishGrpcCall(const OnSuccess& callback) {
}

void GrpcStream::FastFinishCompletionsBlocking() {
LOG_DEBUG("GrpcStream('%s'): fast finishing %s completion(s)", this,
LOG_DEBUG("GrpcStream('%x'): fast finishing %s completion(s)", this,
completions_.size());

// TODO(varconst): reset buffered_writer_? Should not be necessary, because it
Expand Down Expand Up @@ -344,7 +344,7 @@ std::shared_ptr<GrpcCompletion> GrpcStream::NewCompletion(
} else {
// Use the same error-handling for all operations; all errors are
// unrecoverable.
LOG_DEBUG("GrpcStream('%s'): operation of type %s failed", this,
LOG_DEBUG("GrpcStream('%x'): operation of type %s failed", this,
completion->type());
OnOperationFailed();
}
Expand Down
10 changes: 5 additions & 5 deletions Firestore/core/src/remote/remote_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ void RemoteStore::Start() {
[this](ConnectivityMonitor::NetworkStatus network_status) {
if (network_status == ConnectivityMonitor::NetworkStatus::Unavailable) {
LOG_DEBUG(
"RemoteStore %s ignoring connectivity callback for unavailable "
"RemoteStore %x ignoring connectivity callback for unavailable "
"network",
this);
return;
}

if (CanUseNetwork()) {
LOG_DEBUG("RemoteStore %s restarting streams as connectivity changed",
LOG_DEBUG("RemoteStore %x restarting streams as connectivity changed",
this);
RestartNetwork();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ void RemoteStore::DisableNetworkInternal() {
}

void RemoteStore::Shutdown() {
LOG_DEBUG("RemoteStore %s shutting down", this);
LOG_DEBUG("RemoteStore %x shutting down", this);
is_network_enabled_ = false;
DisableNetworkInternal();

Expand Down Expand Up @@ -514,7 +514,7 @@ void RemoteStore::HandleHandshakeError(const Status& status) {
if (Datastore::IsPermanentError(status)) {
std::string token = util::ToString(write_stream_->last_stream_token());
LOG_DEBUG(
"RemoteStore %s error before completed handshake; resetting "
"RemoteStore %x error before completed handshake; resetting "
"stream token %s: "
"error code: '%s', details: '%s'",
this, token, status.code(), status.error_message());
Expand Down Expand Up @@ -590,7 +590,7 @@ void RemoteStore::HandleCredentialChange() {
// Tear down and re-create our network streams. This will ensure we get a
// fresh auth token for the new user and re-fill the write pipeline with new
// mutations from the `LocalStore` (since mutations are per-user).
LOG_DEBUG("RemoteStore %s restarting streams for new credential", this);
LOG_DEBUG("RemoteStore %x restarting streams for new credential", this);
RestartNetwork();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/remote/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void Stream::Write(grpc::ByteBuffer&& message) {

std::string Stream::GetDebugDescription() const {
EnsureOnQueue();
return StringFormat("%s (%s)", GetDebugName(), this);
return StringFormat("%s (%x)", GetDebugName(), this);
}

} // namespace remote
Expand Down
24 changes: 22 additions & 2 deletions Firestore/core/src/util/string_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include "Firestore/core/src/util/string_format.h"

#include <string>
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"

namespace firebase {
namespace firestore {
namespace util {
Expand All @@ -39,7 +43,7 @@ __attribute__((no_sanitize_address)) std::string StringFormatPieces(

auto pieces_iter = pieces.begin();
auto pieces_end = pieces.end();
auto append_next_piece = [&](std::string* dest) {
auto append_next_string_piece = [&](std::string* dest) {
if (pieces_iter == pieces_end) {
dest->append(kMissing);
} else {
Expand All @@ -49,6 +53,17 @@ __attribute__((no_sanitize_address)) std::string StringFormatPieces(
}
};

auto append_next_hex_piece = [&](std::string* dest) {
if (pieces_iter == pieces_end) {
dest->append(kMissing);
} else {
std::string hex =
absl::BytesToHexString(absl::string_view(pieces_iter->data()));
dest->append(hex.data(), hex.size());
++pieces_iter;
}
};

auto append_specifier = [&](char spec) {
switch (spec) {
case '%':
Expand All @@ -57,7 +72,12 @@ __attribute__((no_sanitize_address)) std::string StringFormatPieces(
break;

case 's': {
append_next_piece(&result);
append_next_string_piece(&result);
break;
}

case 'x': {
append_next_hex_piece(&result);
break;
}

Expand Down
4 changes: 4 additions & 0 deletions Firestore/core/test/unit/util/string_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ TEST(StringFormatTest, Mixed) {
StringFormat("%s%%%s%%%s%%%s", "World", true, 42, 1.5));
}

TEST(StringFormatTest, Hex) {
EXPECT_EQ("test=42", StringFormat("test=%x", "B"));
}

TEST(StringFormatTest, Literal) {
EXPECT_EQ("Hello %", StringFormat("Hello %%"));
EXPECT_EQ("% World", StringFormat("%% World"));
Expand Down
Loading