]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: add '%Tl' to log-format
authorYuxans Yao <yuxans@gmail.com>
Fri, 19 Oct 2012 02:36:09 +0000 (10:36 +0800)
committerWilly Tarreau <w@1wt.eu>
Mon, 29 Oct 2012 10:55:26 +0000 (11:55 +0100)
The '%Tl' is similar to '%T', but using local timezone.

doc/configuration.txt
include/common/standard.h
include/types/log.h
src/haproxy.c
src/log.c
src/standard.c

index acf3099de83e7d78de893766423568f25009729e..949a38398aa651d9a3e02fdbfb45b552c48f722d 100644 (file)
@@ -10056,6 +10056,7 @@ Please refer to the table below for currently defined variables :
   |   | %Sp  | server_port                                   | numeric     |
   |   | %T   | gmt_date_time                                 | date        |
   |   | %Tc  | Tc                                            | numeric     |
+  |   | %Tl  | local_date_time                               | date        |
   | H | %Tq  | Tq                                            | numeric     |
   | H | %Tr  | Tr                                            | numeric     |
   |   | %Ts  | timestamp                                     | numeric     |
index ff87cd132e3af4054ec8aeded261a3de8c17b8d0..4813065742de3ba66ccd7cf3e85dcdfca94f4d89 100644 (file)
@@ -676,6 +676,9 @@ char *human_time(int t, short hz_div);
 
 extern const char *monthname[];
 
+/* numeric timezone (that is, the hour and minute offset from UTC) */
+char localtimezone[6];
+
 /* date2str_log: write a date in the format :
  *     sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
  *             tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
@@ -693,6 +696,13 @@ char *date2str_log(char *dest, struct tm *tm, struct timeval *date, size_t size)
  */
 char *gmt2str_log(char *dst, struct tm *tm, size_t size);
 
+/* localdate2str_log: write a date in the format :
+ * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
+ * return a pointer to the last char written (\0) or
+ * NULL if there isn't enough space.
+ */
+char *localdate2str_log(char *dst, struct tm *tm, size_t size);
+
 /* Dynamically allocates a string of the proper length to hold the formatted
  * output. NULL is returned on error. The caller is responsible for freeing the
  * memory area using free(). The resulting string is returned in <out> if the
index 8c28310d044c8db39423a91dbf448776dc6a9659..ce0c717911da9f5355a54bffe7a1b0349858347e 100644 (file)
@@ -57,6 +57,7 @@ enum {
        LOG_FMT_PID,
        LOG_FMT_DATE,
        LOG_FMT_DATEGMT,
+       LOG_FMT_DATELOCAL,
        LOG_FMT_TS,
        LOG_FMT_MS,
        LOG_FMT_FRONTEND,
index bfbcb328789860a11d0122d314ad4573b588f3bd..e2520328b6f91df9aba647a4466570ad3d724c42 100644 (file)
@@ -402,6 +402,7 @@ void init(int argc, char **argv)
        struct wordlist *wl;
        char *progname;
        char *change_dir = NULL;
+       struct tm curtime;
 
        trash = malloc(global.tune.bufsize);
 
@@ -428,6 +429,10 @@ void init(int argc, char **argv)
        tv_update_date(-1,-1);
        start_date = now;
 
+       /* Get the numeric timezone. */
+       get_localtime(start_date.tv_sec, &curtime);
+       strftime(localtimezone, 6, "%z", &curtime);
+
        signal_init();
        init_task();
        init_session();
index 752f275b433980c1be2aad535da719d9b5481ee5..79158e07b6cd402e56ebf626f6f436aca8ba27a5 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -80,6 +80,7 @@ static const struct logformat_type logformat_keywords[] = {
        { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */
        { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL },      /* date */
        { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL },   /* date GMT */
+       { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL },   /* date local timezone */
        { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL },   /* timestamp GMT */
        { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL },       /* accept date millisecond */
        { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL },  /* frontend */
@@ -943,6 +944,15 @@ int build_logline(struct session *s, char *dst, size_t maxsize, struct list *lis
                                last_isspace = 0;
                                break;
 
+                       case LOG_FMT_DATELOCAL: // %Tl
+                               get_localtime(s->logs.accept_date.tv_sec, &tm);
+                               ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog);
+                               if (ret == NULL)
+                                       goto out;
+                               tmplog = ret;
+                               last_isspace = 0;
+                               break;
+
                        case LOG_FMT_TS: // %Ts
                                get_gmtime(s->logs.accept_date.tv_sec, &tm);
                                if (tmp->options & LOG_OPT_HEXA) {
index c26d7a035f3e4bf432ae6fd7686eceb91c9e0604..10c25a3029ef3ebeccc147126571dd6e30072d3e 100644 (file)
@@ -1730,7 +1730,7 @@ char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
  */
 char *gmt2str_log(char *dst, struct tm *tm, size_t size)
 {
-       if (size < 27) /* the size is fixed: 24 chars + \0 */
+       if (size < 27) /* the size is fixed: 26 chars + \0 */
                return NULL;
 
        dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
@@ -1756,6 +1756,36 @@ char *gmt2str_log(char *dst, struct tm *tm, size_t size)
        return dst;
 }
 
+/* localdate2str_log: write a date in the format :
+ * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
+ * * return a pointer to the last char written (\0) or
+ * * NULL if there isn't enough space.
+ */
+char *localdate2str_log(char *dst, struct tm *tm, size_t size)
+{
+       if (size < 27) /* the size is fixed: 26 chars + \0 */
+               return NULL;
+
+       dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
+       *dst++ = '/';
+       memcpy(dst, monthname[tm->tm_mon], 3); // month
+       dst += 3;
+       *dst++ = '/';
+       dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
+       *dst++ = ':';
+       dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
+       *dst++ = ':';
+       dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
+       *dst++ = ':';
+       dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
+       *dst++ = ' ';
+       memcpy(dst, localtimezone, 5); // timezone
+       dst += 5;
+       *dst = '\0';
+
+       return dst;
+}
+
 /* Dynamically allocates a string of the proper length to hold the formatted
  * output. NULL is returned on error. The caller is responsible for freeing the
  * memory area using free(). The resulting string is returned in <out> if the