diff --git a/ts/GNUmakefile b/ts/GNUmakefile index c84daf8..a6cdd40 100644 --- a/ts/GNUmakefile +++ b/ts/GNUmakefile @@ -6,12 +6,15 @@ CONFDIR_exists=$(shell [ -d $(CONFDIR) ] && echo ok) all: configure ts clean: - rm ts + rm -f ts ts.o install: \ $(BINDIR)/ts \ -ts: ts.pl +ts: %: %.o + + +# $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ %: %.pl customize sh ./customize < $< > $@ diff --git a/ts/ts.c b/ts/ts.c new file mode 100644 index 0000000..8d96653 --- /dev/null +++ b/ts/ts.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +static char *cmnd; + +static void usage(void) { + fprintf(stderr, "Usage: %s [-u]\n", cmnd); + exit(1); +} + + +int main(int argc, char **argv) { + int c; + int use_microseconds = 0; + int print_ts = 1; + + cmnd = argv[0]; + + while ((c = getopt(argc, argv, "u")) != EOF) { + switch (c) { + case 'u': + use_microseconds = 1; + break; + default: + usage(); + } + } + while ((c = getchar()) != EOF) { + if (print_ts) { + struct timeval tv; + char s[sizeof("yyyy-mm-ddThh:mm:ss")]; + gettimeofday(&tv, NULL); + strftime(s, sizeof(s), "%Y-%m-%dT%H:%M:%S", + localtime(&tv.tv_sec)); + fputs(s, stdout); + if (use_microseconds) { + printf(".%06ld ", (long)tv.tv_usec); + } + putchar(' '); + print_ts = 0; + } + putchar(c); + if (c == '\n') print_ts = 1; + } + if (!print_ts) putchar('\n'); // force newline at end of output + return 0; +} diff --git a/ts/ts.pl b/ts/ts.pl index 0039843..f7440f9 100644 --- a/ts/ts.pl +++ b/ts/ts.pl @@ -1,8 +1,19 @@ #!@@@perl@@@ -wT use strict; use POSIX; +use Time::HiRes qw(gettimeofday); +my $use_microseconds; +if ($ARGV[0] eq '-u') { + $use_microseconds = 1; + shift; +} while (<>) { chomp; - my $now = strftime('%Y-%m-%dT%H:%M:%S', localtime); - print "$now $_\n"; + my ($seconds, $microseconds) = gettimeofday; + my $now = strftime('%Y-%m-%dT%H:%M:%S', localtime($seconds)); + if ($use_microseconds) { + printf "%s.%06d %s\n", $now, $microseconds, $_; + } else { + print "$now $_\n"; + } }