From c37e78510f2ac4d9bb4c44eddf33352eda72fd0f Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Wed, 3 Feb 2010 00:19:24 +0100 Subject: Makes date/time formats configurable. --- sysdep/unix/config.Y | 19 ++++++++++++++++ sysdep/unix/io.c | 61 ++++++++++++++++++++-------------------------------- sysdep/unix/log.c | 14 +++--------- sysdep/unix/timer.h | 15 +++++++------ 4 files changed, 54 insertions(+), 55 deletions(-) (limited to 'sysdep/unix') diff --git a/sysdep/unix/config.Y b/sysdep/unix/config.Y index 46c5862..8c2b690 100644 --- a/sysdep/unix/config.Y +++ b/sysdep/unix/config.Y @@ -14,10 +14,12 @@ CF_HDR CF_DECLS CF_KEYWORDS(LOG, SYSLOG, ALL, DEBUG, TRACE, INFO, REMOTE, WARNING, ERROR, AUTH, FATAL, BUG, STDERR, SOFT) +CF_KEYWORDS(TIMEFORMAT, ISO, SHORT, LONG, BASE) %type log_mask log_mask_list log_cat %type log_file %type cfg_name +%type timeformat_which CF_GRAMMAR @@ -75,7 +77,24 @@ mrtdump_base: } ; +CF_ADDTO(conf, timeformat_base) +timeformat_which: + ROUTE { $$ = &new_config->tf_route; } + | PROTOCOL { $$ = &new_config->tf_proto; } + | BASE { $$ = &new_config->tf_base; } + | LOG { $$ = &new_config->tf_log; } + +timeformat_spec: + timeformat_which TEXT { *$1 = (struct timeformat){$2, NULL, 0}; } + | timeformat_which TEXT expr TEXT { *$1 = (struct timeformat){$2, $4, $3}; } + | timeformat_which ISO SHORT { *$1 = (struct timeformat){"%T", "%F", 20*3600}; } + | timeformat_which ISO LONG { *$1 = (struct timeformat){"%F %T", NULL, 0}; } + ; + +timeformat_base: + TIMEFORMAT timeformat_spec ';' + ; /* Unix specific commands */ diff --git a/sysdep/unix/io.c b/sysdep/unix/io.c index 39f29c5..296b6b3 100644 --- a/sysdep/unix/io.c +++ b/sysdep/unix/io.c @@ -410,23 +410,22 @@ tm_parse_date(char *x) return t; } -/** - * tm_format_date - convert date to textual representation - * @x: destination buffer of size %TM_DATE_BUFFER_SIZE - * @t: time - * - * This function formats the given relative time value @t to a textual - * date representation (dd-mm-yyyy) in real time.. - */ -void -tm_format_date(char *x, bird_clock_t t) +static void +tm_format_reltime(char *x, struct tm *tm, bird_clock_t delta) { - struct tm *tm; + static char *month_names[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - tm = localtime(&t); - bsprintf(x, "%02d-%02d-%04d", tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900); + if (delta < 20*3600) + bsprintf(x, "%02d:%02d", tm->tm_hour, tm->tm_min); + else if (delta < 360*86400) + bsprintf(x, "%s%02d", month_names[tm->tm_mon], tm->tm_mday); + else + bsprintf(x, "%d", tm->tm_year+1900); } +#include "conf/conf.h" + /** * tm_format_datetime - convert date and time to textual representation * @x: destination buffer of size %TM_DATETIME_BUFFER_SIZE @@ -436,39 +435,25 @@ tm_format_date(char *x, bird_clock_t t) * date/time representation (dd-mm-yyyy hh:mm:ss) in real time. */ void -tm_format_datetime(char *x, bird_clock_t t) +tm_format_datetime(char *x, struct timeformat *fmt_spec, bird_clock_t t) { + const char *fmt_used; struct tm *tm; bird_clock_t delta = now - t; t = now_real - delta; tm = localtime(&t); - if (strftime(x, TM_DATETIME_BUFFER_SIZE, "%d-%m-%Y %H:%M:%S", tm) == TM_DATETIME_BUFFER_SIZE) - strcpy(x, ""); -} -/** - * tm_format_reltime - convert date and time to relative textual representation - * @x: destination buffer of size %TM_RELTIME_BUFFER_SIZE - * @t: time - * - * This function formats the given relative time value @t to a short - * textual representation in real time, relative to the current time. - */ -void -tm_format_reltime(char *x, bird_clock_t t) -{ - struct tm *tm; - static char *month_names[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + if (fmt_spec->fmt1 == NULL) + return tm_format_reltime(x, tm, delta); - bird_clock_t delta = now - t; - t = now_real - delta; - tm = localtime(&t); - if (delta < 20*3600) - bsprintf(x, "%02d:%02d", tm->tm_hour, tm->tm_min); - else if (delta < 360*86400) - bsprintf(x, "%s%02d", month_names[tm->tm_mon], tm->tm_mday); + if ((fmt_spec->limit == 0) || (delta < fmt_spec->limit)) + fmt_used = fmt_spec->fmt1; else - bsprintf(x, "%d", tm->tm_year+1900); + fmt_used = fmt_spec->fmt2; + + int rv = strftime(x, TM_DATETIME_BUFFER_SIZE, fmt_used, tm); + if (((rv == 0) && fmt_used[0]) || (rv == TM_DATETIME_BUFFER_SIZE)) + strcpy(x, ""); } /** diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c index dad0c5d..f227549 100644 --- a/sysdep/unix/log.c +++ b/sysdep/unix/log.c @@ -79,21 +79,13 @@ vlog(int class, char *msg, va_list args) continue; if (l->fh) { - time_t now = time(NULL); - struct tm *tm = localtime(&now); - if (l->terminal_flag) fputs("bird: ", l->fh); else { - fprintf(l->fh, "%02d-%02d-%04d %02d:%02d:%02d <%s> ", - tm->tm_mday, - tm->tm_mon+1, - tm->tm_year+1900, - tm->tm_hour, - tm->tm_min, - tm->tm_sec, - class_names[class]); + byte tbuf[TM_DATETIME_BUFFER_SIZE]; + tm_format_datetime(tbuf, &config->tf_log, now); + fprintf(l->fh, "%s <%s> ", tbuf, class_names[class]); } fputs(buf, l->fh); fputc('\n', l->fh); diff --git a/sysdep/unix/timer.h b/sysdep/unix/timer.h index 761cb42..3ed6ff1 100644 --- a/sysdep/unix/timer.h +++ b/sysdep/unix/timer.h @@ -33,14 +33,17 @@ void tm_dump_all(void); extern bird_clock_t now; /* Relative, monotonic time in seconds */ extern bird_clock_t now_real; /* Time in seconds since fixed known epoch */ +struct timeformat { + char *fmt1, *fmt2; + bird_clock_t limit; +}; + bird_clock_t tm_parse_date(char *); /* Convert date to bird_clock_t */ bird_clock_t tm_parse_datetime(char *); /* Convert date to bird_clock_t */ -void tm_format_date(char *, bird_clock_t); /* Convert bird_clock_t to date */ -#define TM_DATE_BUFFER_SIZE 12 /* Buffer size required by tm_format_date */ -void tm_format_datetime(char *, bird_clock_t); /* Convert bird_clock_t to date + time */ -#define TM_DATETIME_BUFFER_SIZE 64 /* Buffer size required by tm_format_datetime */ -void tm_format_reltime(char *, bird_clock_t); /* Convert bird_clock_t to relative datetime string */ -#define TM_RELTIME_BUFFER_SIZE 12 /* Buffer size required by tm_format_reltime */ + +#define TM_DATETIME_BUFFER_SIZE 32 /* Buffer size required by tm_format_datetime */ +void +tm_format_datetime(char *x, struct timeformat *fmt_spec, bird_clock_t t); #ifdef TIME_T_IS_64BIT #define TIME_INFINITY 0x7fffffffffffffff -- cgit v1.2.3