nghttpx: Cache string representation of time for logging

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-21 23:52:30 +09:00
parent 5770c6bd04
commit 91ae5291cc
3 changed files with 36 additions and 10 deletions

View File

@ -122,16 +122,17 @@ Log::~Log() {
char buf[4096];
auto tty = wconf->errorlog_tty;
auto time_now = util::format_common_log(std::chrono::system_clock::now());
wconf->update_tstamp(std::chrono::system_clock::now());
auto &time_local = wconf->time_local_str;
if (severity_ == NOTICE) {
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n", time_now.c_str(),
get_config()->pid, tty ? SEVERITY_COLOR[severity_] : "",
SEVERITY_STR[severity_], tty ? "\033[0m" : "",
stream_.str().c_str());
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s\n",
time_local.c_str(), get_config()->pid,
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
tty ? "\033[0m" : "", stream_.str().c_str());
} else {
rv = snprintf(buf, sizeof(buf), "%s PID%d [%s%s%s] %s%s:%d%s %s\n",
time_now.c_str(), get_config()->pid,
time_local.c_str(), get_config()->pid,
tty ? SEVERITY_COLOR[severity_] : "", SEVERITY_STR[severity_],
tty ? "\033[0m" : "", tty ? "\033[1;30m" : "", filename_,
linenum_, tty ? "\033[0m" : "", stream_.str().c_str());
@ -171,6 +172,10 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
auto p = buf;
auto avail = sizeof(buf) - 2;
wconf->update_tstamp(lgsp->time_now);
auto &time_local = wconf->time_local_str;
auto &time_iso8601 = wconf->time_iso8601_str;
for (auto &lf : lfv) {
switch (lf.type) {
case SHRPX_LOGF_LITERAL:
@ -180,12 +185,10 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv, LogSpec *lgsp) {
std::tie(p, avail) = copy(lgsp->remote_addr, avail, p);
break;
case SHRPX_LOGF_TIME_LOCAL:
std::tie(p, avail) =
copy(util::format_common_log(lgsp->time_now).c_str(), avail, p);
std::tie(p, avail) = copy(time_local.c_str(), avail, p);
break;
case SHRPX_LOGF_TIME_ISO8601:
std::tie(p, avail) =
copy(util::format_iso8601(lgsp->time_now).c_str(), avail, p);
std::tie(p, avail) = copy(time_iso8601.c_str(), avail, p);
break;
case SHRPX_LOGF_REQUEST:
std::tie(p, avail) = copy(lgsp->method, avail, p);

View File

@ -23,6 +23,9 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "shrpx_worker_config.h"
#include "util.h"
using namespace nghttp2;
namespace shrpx {
@ -35,4 +38,18 @@ thread_local
#endif // NOTHREADS
WorkerConfig *worker_config = new WorkerConfig();
void
WorkerConfig::update_tstamp(const std::chrono::system_clock::time_point &now) {
auto t0 = std::chrono::system_clock::to_time_t(time_str_updated_);
auto t1 = std::chrono::system_clock::to_time_t(now);
if (t0 == t1) {
return;
}
time_str_updated_ = now;
time_local_str = util::format_common_log(now);
time_iso8601_str = util::format_iso8601(now);
}
} // namespace shrpx

View File

@ -27,6 +27,8 @@
#include "shrpx.h"
#include <chrono>
namespace shrpx {
namespace ssl {
@ -37,6 +39,9 @@ struct TicketKeys;
struct WorkerConfig {
std::shared_ptr<TicketKeys> ticket_keys;
std::chrono::system_clock::time_point time_str_updated_;
std::string time_local_str;
std::string time_iso8601_str;
ssl::CertLookupTree *cert_tree;
int accesslog_fd;
int errorlog_fd;
@ -45,6 +50,7 @@ struct WorkerConfig {
bool graceful_shutdown;
WorkerConfig();
void update_tstamp(const std::chrono::system_clock::time_point &now);
};
// We need WorkerConfig per thread