At various times I've wanted to add timestamps to logfiles as they are generated.. usually this has involved perl or something to do it. finally I broke down and just added a small bit to date(1) the -s option tells date to add a timestamp on the front of every line read in through stdin and put it out through stdout. teh format of the datestamp is governed exactly as usual so: %ls | ./date -s +"%+: " Fri Aug 11 19:53:34 PDT 2006: CVS Fri Aug 11 19:53:34 PDT 2006: Makefile Fri Aug 11 19:53:34 PDT 2006: date Fri Aug 11 19:53:34 PDT 2006: date.1 Fri Aug 11 19:53:34 PDT 2006: date.1.gz Fri Aug 11 19:53:34 PDT 2006: date.c Fri Aug 11 19:53:34 PDT 2006: date.o Fri Aug 11 19:53:34 PDT 2006: extern.h Fri Aug 11 19:53:34 PDT 2006: netdate.c Fri Aug 11 19:53:34 PDT 2006: netdate.o Fri Aug 11 19:53:34 PDT 2006: vary.c Fri Aug 11 19:53:34 PDT 2006: vary.h Fri Aug 11 19:53:34 PDT 2006: vary.o %ls | ./date -s +"%s: " 1155351474: CVS 1155351474: Makefile 1155351474: date 1155351474: date.1 1155351474: date.1.gz 1155351474: date.c 1155351474: date.o 1155351474: extern.h 1155351474: netdate.c 1155351474: netdate.o 1155351474: vary.c 1155351474: vary.h 1155351474: vary.o % I attach the diff. I'm sure that someone who is a more competent practicioner of userland C programming can probably clean this up abit. do people think this is a worthwhile addition? An easy to imagine use for this is to add it in the makefile for /usr so that "make buildworld" datestamped its output (for example). it makes it easy to timestamp output from a console logger for example: ? date ? date.1.gz Index: date.c =================================================================== RCS file: /usr/local/cvsroot/freebsd/src/bin/date/date.c,v retrieving revision 1.47 diff -u -r1.47 date.c --- date.c 10 Jan 2005 08:39:21 -0000 1.47 +++ date.c 12 Aug 2006 02:53:18 -0000 _at__at_ -74,7 +74,7 _at__at_ { struct timezone tz; int ch, rflag; - int jflag, nflag; + int jflag, nflag, sflag; const char *format; char buf[1024]; char *endptr, *fmt; _at__at_ -89,9 +89,9 _at__at_ (void) setlocale(LC_TIME, ""); tz.tz_dsttime = tz.tz_minuteswest = 0; rflag = 0; - jflag = nflag = 0; + sflag = jflag = nflag = 0; set_timezone = 0; - while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) + while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:s")) != -1) switch((char)ch) { case 'd': /* daylight savings time */ tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; _at__at_ -114,6 +114,9 _at__at_ if (*tmp != 0) usage(); break; + case 's': /* don't set network */ + sflag = 1; /* stream mode */ + break; case 't': /* minutes west of UTC */ /* error check; don't allow "PST" */ tz.tz_minuteswest = strtol(optarg, &endptr, 10); _at__at_ -160,19 +163,47 _at__at_ if (*argv && **argv == '+') format = *argv + 1; - lt = *localtime(&tval); - badv = vary_apply(v, <); - if (badv) { - fprintf(stderr, "%s: Cannot apply date adjustment\n", - badv->arg); + if (sflag) { + char linebuf[2048]; + time_t otval = 0; + + while (fgets(linebuf, 2048, stdin)) { + if (!rflag && time(&tval) == -1) + err(1, "time"); + + if (tval != otval) { + lt = *localtime(&tval); + badv = vary_apply(v, <); + if (badv) { + fprintf(stderr, "%s: Cannot apply date adjustment\n", + badv->arg); + vary_destroy(v); + usage(); + } + (void)strftime(buf, sizeof(buf), format, <); + otval = tval; + } + (void)printf("%s", buf); + fputs(linebuf, stdout); + if (fflush(stdout)) { + err(1, "stdout"); + } + } + } else { + lt = *localtime(&tval); + badv = vary_apply(v, <); + if (badv) { + fprintf(stderr, "%s: Cannot apply date adjustment\n", + badv->arg); + vary_destroy(v); + usage(); + } vary_destroy(v); - usage(); + (void)strftime(buf, sizeof(buf), format, <); + (void)printf("%s\n", buf); + if (fflush(stdout)) + err(1, "stdout"); } - vary_destroy(v); - (void)strftime(buf, sizeof(buf), format, <); - (void)printf("%s\n", buf); - if (fflush(stdout)) - err(1, "stdout"); exit(retval); } _at__at_ -299,7 +330,7 _at__at_ usage(void) { (void)fprintf(stderr, "%s\n%s\n", - "usage: date [-jnu] [-d dst] [-r seconds] [-t west] " + "usage: date [-jnus] [-d dst] [-r seconds] [-t west] " "[-v[+|-]val[ymwdHMS]] ... ", " " "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");Received on Sat Aug 12 2006 - 01:03:45 UTC
This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:58 UTC