优化http调试日志性能

This commit is contained in:
ziyue 2021-08-12 21:02:07 +08:00
parent 7b60f8b093
commit 0f6d1135eb
5 changed files with 52 additions and 16 deletions

View File

@ -217,10 +217,7 @@ static inline void addHttpListener(){
consumed = true;
if(api_debug){
auto newInvoker = [invoker, parser](int code,
const HttpSession::KeyValue &headerOut,
const HttpBody::Ptr &body) {
auto newInvoker = [invoker, parser](int code, const HttpSession::KeyValue &headerOut, const HttpBody::Ptr &body) {
//body默认为空
ssize_t size = 0;
if (body && body->remainSize()) {
@ -228,18 +225,23 @@ static inline void addHttpListener(){
size = body->remainSize();
}
LogContextCapturer log(getLogger(), LDebug, __FILE__, "http api debug", __LINE__);
log << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n";
log << "# header:\r\n";
for (auto &pr : parser.getHeader()) {
log << pr.first << " : " << pr.second << "\r\n";
}
auto &content = parser.Content();
log << "# content:\r\n" << (content.size() > 4 * 1024 ? content.substr(0, 4 * 1024) : content) << "\r\n";
if (size && size < 4 * 1024) {
string contentOut = body->readData(size)->toString();
DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"
<< "# content:\r\n" << parser.Content() << "\r\n"
<< "# response:\r\n"
<< contentOut << "\r\n";
invoker(code, headerOut, contentOut);
auto response = body->readData(size);
log << "# response:\r\n" << response->data() << "\r\n";
invoker(code, headerOut, response);
} else {
DebugL << "\r\n# request:\r\n" << parser.Method() << " " << parser.FullUrl() << "\r\n"
<< "# content:\r\n" << parser.Content() << "\r\n"
<< "# response size:"
<< size << "\r\n";
log << "# response size:" << size << "\r\n";
invoker(code, headerOut, body);
}
};

View File

@ -247,4 +247,16 @@ string HttpMultiFormBody::multiFormBodyPrefix(const HttpArgs &args,const string
return std::move(body);
}
HttpBufferBody::HttpBufferBody(Buffer::Ptr buffer) {
_buffer = std::move(buffer);
}
ssize_t HttpBufferBody::remainSize() {
return _buffer ? _buffer->size() : 0;
}
Buffer::Ptr HttpBufferBody::readData(size_t size) {
return Buffer::Ptr(std::move(_buffer));
}
}//namespace mediakit

View File

@ -69,7 +69,8 @@ class HttpStringBody : public HttpBody{
public:
typedef std::shared_ptr<HttpStringBody> Ptr;
HttpStringBody(const string &str);
virtual ~HttpStringBody(){}
~HttpStringBody() override = default;
ssize_t remainSize() override;
Buffer::Ptr readData(size_t size) override ;
@ -78,6 +79,22 @@ private:
mutable string _str;
};
/**
* Buffer类型的content
*/
class HttpBufferBody : public HttpBody{
public:
typedef std::shared_ptr<HttpBufferBody> Ptr;
HttpBufferBody(Buffer::Ptr buffer);
~HttpBufferBody() override = default;
ssize_t remainSize() override;
Buffer::Ptr readData(size_t size) override;
private:
Buffer::Ptr _buffer;
};
/**
* content
*/
@ -93,7 +110,7 @@ public:
*/
HttpFileBody(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
HttpFileBody(const string &file_path);
~HttpFileBody(){};
~HttpFileBody() override = default;
ssize_t remainSize() override ;
Buffer::Ptr readData(size_t size) override;

View File

@ -514,6 +514,10 @@ void HttpFileManager::onAccessPath(TcpSession &sender, Parser &parser, const Htt
////////////////////////////////////HttpResponseInvokerImp//////////////////////////////////////
void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const Buffer::Ptr &body) const {
return operator()(code, headerOut, std::make_shared<HttpBufferBody>(body));
}
void HttpResponseInvokerImp::operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const{
if (_lambad) {
_lambad(code, headerOut, body);

View File

@ -31,6 +31,7 @@ public:
HttpResponseInvokerImp(const HttpResponseInvokerLambda0 &lambda);
HttpResponseInvokerImp(const HttpResponseInvokerLambda1 &lambda);
void operator()(int code, const StrCaseMap &headerOut, const Buffer::Ptr &body) const;
void operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const;
void operator()(int code, const StrCaseMap &headerOut, const string &body) const;