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) {
if (_parser.http_errno != 0) {
// May try HTTP on other formats, failure is norm.
RPC_VLOG << "Fail to parse http message, parser=" << _parser
<< ", buf=`" << butil::PrintedAsBinary(buf) << '\'';
<< ", buf=" << butil::ToPrintable(buf);
return -1;
}
if (Completed()) {
......
......@@ -240,7 +240,7 @@ void InputMessenger::OnNewMessages(Socket* m) {
} else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
LOG(WARNING)
<< "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->description().c_str());
return;
......
......@@ -1606,7 +1606,7 @@ void H2UnsentRequest::Print(std::ostream& os) const {
if (!body->empty()) {
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 {
if (!_data.empty()) {
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*,
......
......@@ -211,13 +211,12 @@ static void PrintMessage(const butil::IOBuf& inbuf,
if (buf2.size() == last_size) {
buf2.pop_back(2); // remove "> "
}
buf2.append(buf1);
if (!has_content) {
std::cerr << buf2 << std::endl;
buf2.append(buf1);
} else {
std::cerr << butil::PrintedAsBinary(
buf2, buf2.size() + FLAGS_http_verbose_max_body_length) << std::endl;
buf2.append(butil::ToPrintableString(buf1, FLAGS_http_verbose_max_body_length));
}
std::cerr << buf2 << std::endl;
}
static void AddGrpcPrefix(butil::IOBuf* body, bool compressed) {
......
......@@ -390,14 +390,14 @@ std::ostream& operator<<(std::ostream& os, const RtmpAudioMessage& msg) {
<< " rate=" << FlvSoundRate2Str(msg.rate)
<< " bits=" << FlvSoundBits2Str(msg.bits)
<< " type=" << FlvSoundType2Str(msg.type)
<< " data=" << butil::PrintedAsBinary(msg.data) << '}';
<< " data=" << butil::ToPrintable(msg.data) << '}';
}
std::ostream& operator<<(std::ostream& os, const RtmpVideoMessage& msg) {
return os << "VideoMessage{timestamp=" << msg.timestamp
<< " type=" << FlvVideoFrameType2Str(msg.frame_type)
<< " codec=" << FlvVideoCodec2Str(msg.codec)
<< " data=" << butil::PrintedAsBinary(msg.data) << '}';
<< " data=" << butil::ToPrintable(msg.data) << '}';
}
butil::Status RtmpAACMessage::Create(const RtmpAudioMessage& msg) {
......
......@@ -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);
if (_iobuf) {
PrintIOBuf(&appender, *_iobuf, _max_length);
......
......@@ -26,28 +26,21 @@ class IOBuf;
// Print binary content within max length.
// The printing format is optimized for humans and may change in future.
class BinaryPrinter {
class ToPrintable {
public:
static const size_t DEFAULT_MAX_LENGTH = 64;
explicit BinaryPrinter(const IOBuf& data)
: _iobuf(&data), _max_length(DEFAULT_MAX_LENGTH) {}
BinaryPrinter(const IOBuf& b, size_t max_length)
ToPrintable(const IOBuf& b, size_t max_length = DEFAULT_MAX_LENGTH)
: _iobuf(&b), _max_length(max_length) {}
explicit BinaryPrinter(const StringPiece& str)
: _iobuf(NULL), _str(str), _max_length(DEFAULT_MAX_LENGTH) {}
BinaryPrinter(const StringPiece& str, size_t max_length)
ToPrintable(const StringPiece& str, size_t max_length = DEFAULT_MAX_LENGTH)
: _iobuf(NULL), _str(str), _max_length(max_length) {}
explicit BinaryPrinter(const void* data, size_t n)
: _iobuf(NULL), _str((const char*)data, n), _max_length(DEFAULT_MAX_LENGTH) {}
explicit BinaryPrinter(const void* data, size_t n, size_t max_length)
ToPrintable(const void* data, size_t n, size_t max_length = DEFAULT_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;
private:
const IOBuf* _iobuf;
StringPiece _str;
......@@ -55,20 +48,20 @@ private:
};
// 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);
return os;
}
// Convert binary data to a printable string.
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,
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,
size_t max_length = BinaryPrinter::DEFAULT_MAX_LENGTH);
size_t max_length = ToPrintable::DEFAULT_MAX_LENGTH);
} // namespace butil
......
......@@ -28,7 +28,7 @@ TEST_F(HPackTest, header_with_indexing) {
butil::IOBufAppender buf;
p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf());
LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = {
0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79,
0x0d, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x68, 0x65, 0x61, 0x64,
......@@ -58,7 +58,7 @@ TEST_F(HPackTest, header_without_indexing) {
butil::IOBufAppender buf;
p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf());
LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = {
0x04, 0x0c, 0x2f, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x61,
0x74, 0x68,
......@@ -88,7 +88,7 @@ TEST_F(HPackTest, header_never_indexed) {
butil::IOBufAppender buf;
p1.Encode(&buf, h, options);
const size_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf());
LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = {
0x10, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64,
0x06, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74,
......@@ -116,7 +116,7 @@ TEST_F(HPackTest, indexed_header) {
butil::IOBufAppender buf;
p1.Encode(&buf, h, options);
const ssize_t nwrite = buf.buf().size();
LOG(INFO) << butil::PrintedAsBinary(buf.buf());
LOG(INFO) << butil::ToPrintable(buf.buf());
uint8_t expected[] = {
0x82,
};
......@@ -255,7 +255,7 @@ TEST_F(HPackTest, requests_with_huffman) {
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b,
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))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h;
......@@ -356,7 +356,7 @@ TEST_F(HPackTest, responses_without_huffman) {
0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65,
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))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h;
......@@ -461,7 +461,7 @@ TEST_F(HPackTest, responses_with_huffman) {
0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8,
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))));
for (size_t i = 0; i < ARRAY_SIZE(header1); ++i) {
brpc::HPacker::Header h;
......
......@@ -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"
"\\FB\\FC\\FD\\FE\\FF";
std::ostringstream os;
os << butil::PrintedAsBinary(buf, 256);
os << butil::ToPrintable(buf, 256);
ASSERT_STREQ(OUTPUT, os.str().c_str());
os.str("");
os << butil::PrintedAsBinary(str, 256);
os << butil::ToPrintable(str, 256);
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