From owner-freebsd-questions@FreeBSD.ORG Mon May 19 06:26:03 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6D14637B404 for ; Mon, 19 May 2003 06:26:03 -0700 (PDT) Received: from thalia.otenet.gr (thalia.otenet.gr [195.170.0.8]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3DC0843F75 for ; Mon, 19 May 2003 06:26:01 -0700 (PDT) (envelope-from keramida@ceid.upatras.gr) Received: from gothmog.gr (patr530-a019.otenet.gr [212.205.215.19]) by thalia.otenet.gr (8.12.9/8.12.9) with ESMTP id h4JDPWP7000920; Mon, 19 May 2003 16:25:47 +0300 (EEST) Received: from gothmog.gr (gothmog [127.0.0.1]) by gothmog.gr (8.12.9/8.12.9) with ESMTP id h4JDPBWI005902; Mon, 19 May 2003 16:25:21 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from giorgos@localhost) by gothmog.gr (8.12.9/8.12.9/Submit) id h4JD7cYd005583; Mon, 19 May 2003 16:07:38 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Mon, 19 May 2003 16:07:38 +0300 From: Giorgos Keramidas To: Andy Farkas Message-ID: <20030519130738.GE5081@gothmog.gr> References: <20030519174723.B93323-100000@hewey.af.speednet.com.au> <20030519113017.GA46308@zi025.glhnet.mhn.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030519113017.GA46308@zi025.glhnet.mhn.de> cc: freebsd-questions@freebsd.org Subject: Re: timestamping a text stream X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 May 2003 13:26:03 -0000 On 2003-05-19 13:30, Simon Barner wrote: >Andy Farkas wrote: >> Does anybody know of a program similar to script(1) or tee(1) that >> will timestamp each line of input as it happens? > > You can use this perl script: > > #!/usr/bin/perl -w > # This is timestamp.pl > > use strict; > > my $line=undef; > my $stamp; > while (defined ($line = <>)) { > $stamp = localtime (time ()); > print ("$stamp: $line"); > } Or alternatively, for even more detail in the logs (namely subsecond accuracy in the timestamps), you can use gettimeofday() instead of localtime(): #!/usr/bin/perl -wT use POSIX qw(strftime); require 'sys/syscall.ph'; $| = 1; $TIMEVAL_T = "LL"; while (defined($line = )) { $now = pack($TIMEVAL_T, ()); syscall(&SYS_gettimeofday, $now, 0) != -1 or die "gettimeofday: $!"; @now = unpack($TIMEVAL_T, $now); $ts = strftime("%Y.%m.%d.%H.%M.%S.", localtime($now[0])) . sprintf("%06d", $now[1]); chomp $line; print "$ts| $line\n"; } The time() call of libc will call gettimeofday() anyway in FreeBSD, and strip the subsecond data returned by that system call, so you might as well call gettimeofday() directly :) - Giorgos