Hunter的大杂烩 技术学习笔记

August 12, 2008

apache log patch

Filed under: 技术话题 — hunter @ 6:00 pm

修改 apache log 格式中的 %T 输出格式(从秒修改为微秒)

diff -uprBN –exclude=’*.o’ apache_1.3.33/src/include/httpd.h apache_1.3.33-patch/src/include/httpd.h
— apache_1.3.33/src/include/httpd.h   2004-10-28 00:34:01.000000000 +0800
+++ apache_1.3.33-patch/src/include/httpd.h 2008-08-12 16:14:14.000000000 +0800
@@ -667,6 +667,7 @@ struct request_rec {
     const char *hostname;  /* Host, as set by full URI or Host: */

     time_t request_time;   /* When the request started */
+    uint32_t request_time_usec;    /* When the request started */

     const char *status_line;   /* Status line, if set by script */
     int status;            /* In any case */


diff -uprBN –exclude=’*.o’ apache_1.3.33/src/main/http_main.c apache_1.3.33-patch/src/main/http_main.c
— apache_1.3.33/src/main/http_main.c  2004-09-16 07:45:17.000000000 +0800
+++ apache_1.3.33-patch/src/main/http_main.c    2008-08-12 17:05:57.000000000 +0800
@@ -1536,7 +1536,10 @@ static void timeout(int sig)
    if (!current_conn->keptalive) {
        /* in some cases we come here before setting the time */
        if (log_req->request_time == 0) {
–                log_req->request_time = time(NULL);
+               struct timeval stVal;
+               gettimeofday(&stVal, NULL);
+                log_req->request_time = stVal.tv_sec;
+                log_req->request_time_usec = stVal.tv_usec;
        }
        ap_log_transaction(log_req);
    }
diff -uprBN –exclude=’*.o’ apache_1.3.33/src/main/http_protocol.c apache_1.3.33-patch/src/main/http_protocol.c
— apache_1.3.33/src/main/http_protocol.c  2004-09-16 07:45:18.000000000 +0800
+++ apache_1.3.33-patch/src/main/http_protocol.c    2008-08-12 17:09:43.000000000 +0800
@@ -990,11 +990,16 @@ static int read_request_line(request_rec
     ap_bsetflag(conn->client, B_SAFEREAD, 1);
     while ((len = ap_getline(l, sizeof(l), conn->client, 0)) < = 0) {          if ((len < 0) || ap_bgetflag(conn->client, B_EOF) || !conn->keepalives) {
+       struct timeval stVal;
             ap_bsetflag(conn->client, B_SAFEREAD, 0);
        /* this is a hack to make sure that request time is set,
         * it’s not perfect, but it’s better than nothing
         */
–       r->request_time = time(0);
+       //r->request_time = time(0);
+       gettimeofday(&stVal, NULL);
+       r->request_time = stVal.tv_sec;
+       r->request_time_usec = stVal.tv_usec;
+
             return 0;
         }
     }
@@ -1005,7 +1010,12 @@ static int read_request_line(request_rec

     ap_bsetflag(conn->client, B_SAFEREAD, 0);

–    r->request_time = time(NULL);
+    {
+       struct timeval stVal;
+       gettimeofday(&stVal, NULL);
+       r->request_time = stVal.tv_sec;
+       r->request_time_usec = stVal.tv_usec;
+    }
     r->the_request = ap_pstrdup(r->pool, l);
     r->method = ap_getword_white(r->pool, &ll);
     uri = ap_getword_white(r->pool, &ll);
diff -uprBN –exclude=’*.o’ apache_1.3.33/src/main/http_request.c apache_1.3.33-patch/src/main/http_request.c
— apache_1.3.33/src/main/http_request.c   2004-08-28 19:35:27.000000000 +0800
+++ apache_1.3.33-patch/src/main/http_request.c 2008-08-12 17:07:36.000000000 +0800
@@ -764,6 +764,7 @@ API_EXPORT(request_rec *) ap_sub_req_met
     rnew = make_sub_request(r);
     rnew->hostname       = r->hostname;
     rnew->request_time   = r->request_time;
+    rnew->request_time_usec   = r->request_time_usec;
     rnew->connection     = r->connection;
     rnew->server         = r->server;
     rnew->request_config = ap_create_request_config(rnew->pool);
@@ -859,6 +860,7 @@ API_EXPORT(request_rec *) ap_sub_req_loo
     rnew = make_sub_request(r);
     rnew->hostname       = r->hostname;
     rnew->request_time   = r->request_time;
+    rnew->request_time_usec   = r->request_time_usec;
     rnew->connection     = r->connection;
     rnew->server         = r->server;
     rnew->request_config = ap_create_request_config(rnew->pool);
@@ -1392,6 +1394,7 @@ static request_rec *internal_internal_re
     new->proto_num       = r->proto_num;
     new->hostname        = r->hostname;
     new->request_time    = r->request_time;
+    new->request_time_usec    = r->request_time_usec;
     new->main            = r->main;

     new->headers_in      = r->headers_in;
diff -uprBN –exclude=’*.o’ apache_1.3.33/src/modules/standard/mod_log_config.c apache_1.3.33-patch/src/modules/standard/mod_log_config.c
— apache_1.3.33/src/modules/standard/mod_log_config.c 2004-02-21 04:37:40.000000000 +0800
+++ apache_1.3.33-patch/src/modules/standard/mod_log_config.c   2008-08-12 17:17:09.000000000 +0800
@@ -404,7 +404,10 @@ static const char *log_request_time(requ

 static const char *log_request_duration(request_rec *r, char *a)
 {
–    return ap_psprintf(r->pool, “%ld”, time(NULL) – r->request_time);
+    struct timeval stVal;
+   gettimeofday(&stVal, NULL);
+   uint64_t ddwDiff = stVal.tv_sec*1000000+stVal.tv_usec-(r->request_time*1000000 + r->request_time_usec);
+    return ap_psprintf(r->pool, “%lld”, ddwDiff );
 }

 /* These next two routines use the canonical name:port so that log

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress