shrpx: Color HTTP headers in console log

This commit is contained in:
Tatsuhiro Tsujikawa 2012-12-09 21:36:02 +09:00
parent 9b1f36d274
commit 6ef9b7430d
8 changed files with 62 additions and 7 deletions

View File

@ -27,6 +27,7 @@
#include <sstream> #include <sstream>
#include "shrpx_config.h" #include "shrpx_config.h"
#include "shrpx_log.h"
#include "util.h" #include "util.h"
#include "uri.h" #include "uri.h"
@ -141,6 +142,37 @@ void capitalize(std::string& s, size_t offset)
} }
} }
std::string colorizeHeaders(const char *hdrs)
{
std::string nhdrs;
const char *p = strchr(hdrs, '\n');
if(!p) {
// Not valid HTTP header
return hdrs;
}
nhdrs.append(hdrs, p+1);
++p;
while(1) {
const char* np = strchr(p, ':');
if(!np) {
nhdrs.append(p);
break;
}
nhdrs += TTY_HTTP_HD;
nhdrs.append(p, np);
nhdrs += TTY_RST;
p = np;
np = strchr(p, '\n');
if(!np) {
nhdrs.append(p);
break;
}
nhdrs.append(p, np+1);
p = np+1;
}
return nhdrs;
}
} // namespace http } // namespace http
} // namespace shrpx } // namespace shrpx

View File

@ -41,6 +41,9 @@ std::string modify_location_header_value(const std::string& uri);
void capitalize(std::string& s, size_t offset); void capitalize(std::string& s, size_t offset);
// Adds ANSI color codes to HTTP headers |hdrs|.
std::string colorizeHeaders(const char *hdrs);
} // namespace http } // namespace http
} // namespace shrpx } // namespace shrpx

View File

@ -182,8 +182,16 @@ int HttpDownstreamConnection::push_request_headers()
hdrs += "\r\n"; hdrs += "\r\n";
if(ENABLE_LOG) { if(ENABLE_LOG) {
const char *hdrp;
std::string nhdrs;
if(get_config()->tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str();
} else {
hdrp = hdrs.c_str();
}
DCLOG(INFO, this) << "HTTP request headers. stream_id=" DCLOG(INFO, this) << "HTTP request headers. stream_id="
<< downstream_->get_stream_id() << "\n" << hdrs; << downstream_->get_stream_id() << "\n" << hdrp;
} }
evbuffer *output = bufferevent_get_output(bev_); evbuffer *output = bufferevent_get_output(bev_);
int rv; int rv;

View File

@ -149,7 +149,8 @@ int htp_hdrs_completecb(http_parser *htp)
<< downstream->get_request_minor() << "\n"; << downstream->get_request_minor() << "\n";
const Headers& headers = downstream->get_request_headers(); const Headers& headers = downstream->get_request_headers();
for(size_t i = 0; i < headers.size(); ++i) { for(size_t i = 0; i < headers.size(); ++i) {
ss << headers[i].first << ": " << headers[i].second << "\n"; ss << TTY_HTTP_HD << headers[i].first << TTY_RST << ": "
<< headers[i].second << "\n";
} }
ULOG(INFO, upstream) << "HTTP request headers\n" << ss.str(); ULOG(INFO, upstream) << "HTTP request headers\n" << ss.str();
} }
@ -631,7 +632,15 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream)
hdrs += "\r\n"; hdrs += "\r\n";
hdrs += "\r\n"; hdrs += "\r\n";
if(ENABLE_LOG) { if(ENABLE_LOG) {
ULOG(INFO, this) << "HTTP response headers\n" << hdrs; const char *hdrp;
std::string nhdrs;
if(get_config()->tty) {
nhdrs = http::colorizeHeaders(hdrs.c_str());
hdrp = nhdrs.c_str();
} else {
hdrp = hdrs.c_str();
}
ULOG(INFO, this) << "HTTP response headers\n" << hdrp;
} }
evbuffer *output = bufferevent_get_output(handler_->get_bev()); evbuffer *output = bufferevent_get_output(handler_->get_bev());
if(evbuffer_add(output, hdrs.c_str(), hdrs.size()) != 0) { if(evbuffer_add(output, hdrs.c_str(), hdrs.size()) != 0) {

View File

@ -89,6 +89,9 @@ private:
static int severity_thres_; static int severity_thres_;
}; };
#define TTY_HTTP_HD (get_config()->tty ? "\033[1;34m" : "")
#define TTY_RST (get_config()->tty ? "\033[0m" : "")
} // namespace shrpx } // namespace shrpx
#endif // SHRPX_LOG_H #endif // SHRPX_LOG_H

View File

@ -294,7 +294,7 @@ int SpdyDownstreamConnection::push_request_headers()
if(ENABLE_LOG) { if(ENABLE_LOG) {
std::stringstream ss; std::stringstream ss;
for(size_t i = 0; nv[i]; i += 2) { for(size_t i = 0; nv[i]; i += 2) {
ss << nv[i] << ": " << nv[i+1] << "\n"; ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
} }
DCLOG(INFO, this) << "HTTP request headers\n" << ss.str(); DCLOG(INFO, this) << "HTTP request headers\n" << ss.str();
} }

View File

@ -592,7 +592,7 @@ void on_ctrl_recv_callback
if(ENABLE_LOG) { if(ENABLE_LOG) {
std::stringstream ss; std::stringstream ss;
for(size_t i = 0; nv[i]; i += 2) { for(size_t i = 0; nv[i]; i += 2) {
ss << nv[i] << ": " << nv[i+1] << "\n"; ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
} }
SSLOG(INFO, spdy) << "HTTP response headers. stream_id=" SSLOG(INFO, spdy) << "HTTP response headers. stream_id="
<< frame->syn_reply.stream_id << frame->syn_reply.stream_id

View File

@ -198,7 +198,7 @@ void on_ctrl_recv_callback
if(ENABLE_LOG) { if(ENABLE_LOG) {
std::stringstream ss; std::stringstream ss;
for(size_t i = 0; nv[i]; i += 2) { for(size_t i = 0; nv[i]; i += 2) {
ss << nv[i] << ": " << nv[i+1] << "\n"; ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
} }
ULOG(INFO, upstream) << "HTTP request headers. stream_id=" ULOG(INFO, upstream) << "HTTP request headers. stream_id="
<< downstream->get_stream_id() << downstream->get_stream_id()
@ -777,7 +777,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream)
if(ENABLE_LOG) { if(ENABLE_LOG) {
std::stringstream ss; std::stringstream ss;
for(size_t i = 0; nv[i]; i += 2) { for(size_t i = 0; nv[i]; i += 2) {
ss << nv[i] << ": " << nv[i+1] << "\n"; ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i+1] << "\n";
} }
ULOG(INFO, this) << "HTTP response headers. stream_id=" ULOG(INFO, this) << "HTTP response headers. stream_id="
<< downstream->get_stream_id() << "\n" << downstream->get_stream_id() << "\n"