Commit 012ca4cc authored by gejun's avatar gejun

Rename BinaryPrinter to ToPrintable & fix PrintMessage for http

parent 977892a3
...@@ -458,7 +458,7 @@ ssize_t HttpMessage::ParseFromIOBuf(const butil::IOBuf &buf) { ...@@ -458,7 +458,7 @@ ssize_t HttpMessage::ParseFromIOBuf(const butil::IOBuf &buf) {
if (_parser.http_errno != 0) { if (_parser.http_errno != 0) {
// May try HTTP on other formats, failure is norm. // May try HTTP on other formats, failure is norm.
RPC_VLOG << "Fail to parse http message, parser=" << _parser RPC_VLOG << "Fail to parse http message, parser=" << _parser
<< ", buf=`" << butil::PrintedAsBinary(buf) << '\''; << ", buf=" << butil::ToPrintable(buf);
return -1; return -1;
} }
if (Completed()) { if (Completed()) {
......
...@@ -240,7 +240,7 @@ void InputMessenger::OnNewMessages(Socket* m) { ...@@ -240,7 +240,7 @@ void InputMessenger::OnNewMessages(Socket* m) {
} else if (pr.error() == PARSE_ERROR_TRY_OTHERS) { } else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
LOG(WARNING) LOG(WARNING)
<< "Close " << *m << " due to unknown message: " << "Close " << *m << " due to unknown message: "
<< butil::PrintedAsBinary(m->_read_buf); << butil::ToPrintable(m->_read_buf);
m->SetFailed(EINVAL, "Close %s due to unknown message", m->SetFailed(EINVAL, "Close %s due to unknown message",
m->description().c_str()); m->description().c_str());
return; return;
......
...@@ -1606,7 +1606,7 @@ void H2UnsentRequest::Print(std::ostream& os) const { ...@@ -1606,7 +1606,7 @@ void H2UnsentRequest::Print(std::ostream& os) const {
if (!body->empty()) { if (!body->empty()) {
os << "> \n"; os << "> \n";
} }
os << butil::BinaryPrinter(*body, FLAGS_http_verbose_max_body_length); os << butil::ToPrintable(*body, FLAGS_http_verbose_max_body_length);
} }
...@@ -1741,7 +1741,7 @@ void H2UnsentResponse::Print(std::ostream& os) const { ...@@ -1741,7 +1741,7 @@ void H2UnsentResponse::Print(std::ostream& os) const {
if (!_data.empty()) { if (!_data.empty()) {
os << "> \n"; os << "> \n";
} }
os << butil::BinaryPrinter(_data, FLAGS_http_verbose_max_body_length); os << butil::ToPrintable(_data, FLAGS_http_verbose_max_body_length);
} }
void PackH2Request(butil::IOBuf*, void PackH2Request(butil::IOBuf*,
......
...@@ -211,13 +211,12 @@ static void PrintMessage(const butil::IOBuf& inbuf, ...@@ -211,13 +211,12 @@ static void PrintMessage(const butil::IOBuf& inbuf,
if (buf2.size() == last_size) { if (buf2.size() == last_size) {
buf2.pop_back(2); // remove "> " buf2.pop_back(2); // remove "> "
} }
buf2.append(buf1);
if (!has_content) { if (!has_content) {
std::cerr << buf2 << std::endl; buf2.append(buf1);
} else { } else {
std::cerr << butil::PrintedAsBinary( buf2.append(butil::ToPrintableString(buf1, FLAGS_http_verbose_max_body_length));
buf2, buf2.size() + FLAGS_http_verbose_max_body_length) << std::endl;
} }
std::cerr << buf2 << std::endl;
} }
static void AddGrpcPrefix(butil::IOBuf* body, bool compressed) { static void AddGrpcPrefix(butil::IOBuf* body, bool compressed) {
......
...@@ -390,14 +390,14 @@ std::ostream& operator<<(std::ostream& os, const RtmpAudioMessage& msg) { ...@@ -390,14 +390,14 @@ std::ostream& operator<<(std::ostream& os, const RtmpAudioMessage& msg) {
<< " rate=" << FlvSoundRate2Str(msg.rate) << " rate=" << FlvSoundRate2Str(msg.rate)
<< " bits=" << FlvSoundBits2Str(msg.bits) << " bits=" << FlvSoundBits2Str(msg.bits)
<< " type=" << FlvSoundType2Str(msg.type) << " type=" << FlvSoundType2Str(msg.type)
<< " data=" << butil::PrintedAsBinary(msg.data) << '}'; << " data=" << butil::ToPrintable(msg.data) << '}';
} }
std::ostream& operator<<(std::ostream& os, const RtmpVideoMessage& msg) { std::ostream& operator<<(std::ostream& os, const RtmpVideoMessage& msg) {
return os << "VideoMessage{timestamp=" << msg.timestamp return os << "VideoMessage{timestamp=" << msg.timestamp
<< " type=" << FlvVideoFrameType2Str(msg.frame_type) << " type=" << FlvVideoFrameType2Str(msg.frame_type)
<< " codec=" << FlvVideoCodec2Str(msg.codec) << " codec=" << FlvVideoCodec2Str(msg.codec)
<< " data=" << butil::PrintedAsBinary(msg.data) << '}'; << " data=" << butil::ToPrintable(msg.data) << '}';
} }
butil::Status RtmpAACMessage::Create(const RtmpAudioMessage& msg) { butil::Status RtmpAACMessage::Create(const RtmpAudioMessage& msg) {
......
...@@ -130,7 +130,7 @@ static void PrintString(Appender* appender, const StringPiece& s, size_t max_len ...@@ -130,7 +130,7 @@ static void PrintString(Appender* appender, const StringPiece& s, size_t max_len
} }
} }
void BinaryPrinter::Print(std::ostream& os) const { void ToPrintable::Print(std::ostream& os) const {
OStreamAppender appender(os); OStreamAppender appender(os);
if (_iobuf) { if (_iobuf) {
PrintIOBuf(&appender, *_iobuf, _max_length); PrintIOBuf(&appender, *_iobuf, _max_length);
......
...@@ -26,24 +26,17 @@ class IOBuf; ...@@ -26,24 +26,17 @@ class IOBuf;
// Print binary content within max length. // Print binary content within max length.
// The printing format is optimized for humans and may change in future. // The printing format is optimized for humans and may change in future.
class BinaryPrinter { class ToPrintable {
public: public:
static const size_t DEFAULT_MAX_LENGTH = 64; static const size_t DEFAULT_MAX_LENGTH = 64;
explicit BinaryPrinter(const IOBuf& data) ToPrintable(const IOBuf& b, size_t max_length = DEFAULT_MAX_LENGTH)
: _iobuf(&data), _max_length(DEFAULT_MAX_LENGTH) {}
BinaryPrinter(const IOBuf& b, size_t max_length)
: _iobuf(&b), _max_length(max_length) {} : _iobuf(&b), _max_length(max_length) {}
explicit BinaryPrinter(const StringPiece& str) ToPrintable(const StringPiece& str, size_t max_length = DEFAULT_MAX_LENGTH)
: _iobuf(NULL), _str(str), _max_length(DEFAULT_MAX_LENGTH) {}
BinaryPrinter(const StringPiece& str, size_t max_length)
: _iobuf(NULL), _str(str), _max_length(max_length) {} : _iobuf(NULL), _str(str), _max_length(max_length) {}
explicit BinaryPrinter(const void* data, size_t n) ToPrintable(const void* data, size_t n, size_t max_length = DEFAULT_MAX_LENGTH)
: _iobuf(NULL), _str((const char*)data, n), _max_length(DEFAULT_MAX_LENGTH) {}
explicit BinaryPrinter(const void* data, size_t n, size_t max_length)
: _iobuf(NULL), _str((const char*)data, n), _max_length(max_length) {} : _iobuf(NULL), _str((const char*)data, n), _max_length(max_length) {}
void Print(std::ostream& os) const; void Print(std::ostream& os) const;
...@@ -55,20 +48,20 @@ private: ...@@ -55,20 +48,20 @@ private:
}; };
// Keep old name for compatibility. // Keep old name for compatibility.
typedef BinaryPrinter PrintedAsBinary; typedef ToPrintable PrintedAsBinary;
inline std::ostream& operator<<(std::ostream& os, const BinaryPrinter& p) { inline std::ostream& operator<<(std::ostream& os, const ToPrintable& p) {
p.Print(os); p.Print(os);
return os; return os;
} }
// Convert binary data to a printable string. // Convert binary data to a printable string.
std::string ToPrintableString(const IOBuf& data, std::string ToPrintableString(const IOBuf& data,
size_t max_length = BinaryPrinter::DEFAULT_MAX_LENGTH); size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
std::string ToPrintableString(const StringPiece& data, std::string ToPrintableString(const StringPiece& data,
size_t max_length = BinaryPrinter::DEFAULT_MAX_LENGTH); size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
std::string ToPrintableString(const void* data, size_t n, std::string ToPrintableString(const void* data, size_t n,
size_t max_length = BinaryPrinter::DEFAULT_MAX_LENGTH); size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
} // namespace butil } // namespace butil
......
...@@ -28,7 +28,7 @@ TEST_F(HPackTest, header_with_indexing) { ...@@ -28,7 +28,7 @@ TEST_F(HPackTest, header_with_indexing) {
butil::IOBufAppender buf; butil::IOBufAppender buf;
p1.Encode(&buf, h, options); p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size(); const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = { uint8_t expected[] = {
0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79,
0x0d, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x0d, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64,
...@@ -58,7 +58,7 @@ TEST_F(HPackTest, header_without_indexing) { ...@@ -58,7 +58,7 @@ TEST_F(HPackTest, header_without_indexing) {
butil::IOBufAppender buf; butil::IOBufAppender buf;
p1.Encode(&buf, h, options); p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size(); const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = { uint8_t expected[] = {
0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61, 0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61,
0x74, 0x68, 0x74, 0x68,
...@@ -88,7 +88,7 @@ TEST_F(HPackTest, header_never_indexed) { ...@@ -88,7 +88,7 @@ TEST_F(HPackTest, header_never_indexed) {
butil::IOBufAppender buf; butil::IOBufAppender buf;
p1.Encode(&buf, h, options); p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size(); const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = { uint8_t expected[] = {
0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74,
...@@ -116,7 +116,7 @@ TEST_F(HPackTest, indexed_header) { ...@@ -116,7 +116,7 @@ TEST_F(HPackTest, indexed_header) {
butil::IOBufAppender buf; butil::IOBufAppender buf;
p1.Encode(&buf, h, options); p1.Encode(&buf, h, options);
const ssize_t nwrite = buf.buf().size(); const ssize_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = { uint8_t expected[] = {
0x82, 0x82,
}; };
...@@ -255,7 +255,7 @@ TEST_F(HPackTest, requests_with_huffman) { ...@@ -255,7 +255,7 @@ TEST_F(HPackTest, requests_with_huffman) {
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b,
0xa0, 0xab, 0x90, 0xf4, 0xff 0xa0, 0xab, 0x90, 0xf4, 0xff
}; };
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1)))); ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) { for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h; brpc::HPacker::Header h;
...@@ -356,7 +356,7 @@ TEST_F(HPackTest, responses_without_huffman) { ...@@ -356,7 +356,7 @@ TEST_F(HPackTest, responses_without_huffman) {
0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65,
0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
}; };
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1)))); ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) { for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h; brpc::HPacker::Header h;
...@@ -461,7 +461,7 @@ TEST_F(HPackTest, responses_with_huffman) { ...@@ -461,7 +461,7 @@ TEST_F(HPackTest, responses_with_huffman) {
0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8,
0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3,
}; };
LOG(INFO) << butil::PrintedAsBinary(buf.buf()); LOG(INFO) << butil::ToPrintable(buf.buf());
ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1)))); ASSERT_TRUE(buf.buf().equals(butil::StringPiece((char*)expected1, sizeof(expected1))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) { for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h; brpc::HPacker::Header h;
......
...@@ -1532,10 +1532,10 @@ TEST_F(IOBufTest, printed_as_binary) { ...@@ -1532,10 +1532,10 @@ TEST_F(IOBufTest, printed_as_binary) {
"\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA" "\\EC\\ED\\EE\\EF\\F0\\F1\\F2\\F3\\F4\\F5\\F6\\F7\\F8\\F9\\FA"
"\\FB\\FC\\FD\\FE\\FF"; "\\FB\\FC\\FD\\FE\\FF";
std::ostringstream os; std::ostringstream os;
os << butil::PrintedAsBinary(buf, 256); os << butil::ToPrintable(buf, 256);
ASSERT_STREQ(OUTPUT, os.str().c_str()); ASSERT_STREQ(OUTPUT, os.str().c_str());
os.str(""); os.str("");
os << butil::PrintedAsBinary(str, 256); os << butil::ToPrintable(str, 256);
ASSERT_STREQ(OUTPUT, os.str().c_str()); ASSERT_STREQ(OUTPUT, os.str().c_str());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment