[ale] line timestamp command

Scott Plante splante at insightsys.com
Tue Nov 19 12:26:37 EST 2013


You are very kind to go to all that trouble. In many cases I'm still using the commands I was using 10-30 years ago, and I mostly wanted to make sure a fairly standard command to do it hadn't crept in the mix along the way. Thanks also to Doug for the ruby snippet and the other help. In my case I don't have root on this particular box so using logger would have been more trouble than it's worth but I do want to learn more about customizing syslog. I see uucp is still one of the standard syslog facilities! That takes me back. 


If this C program were to be a standard util included in Linux, I'd like to see the same time format options as the date command. Does this seem like something of general use? I can see piping a slow rsync to it or other commands that generate output periodically. Usually I suppose it would feed to a proper logging system that would put the timestamps on, but it still seems useful for ad hoc usage. 


Scott 

----- Original Message -----

From: "Horkan Smith" <ale at horkan.net> 
To: "Atlanta Linux Enthusiasts" <ale at ale.org> 
Sent: Wednesday, November 13, 2013 7:43:52 AM 
Subject: Re: [ale] line timestamp command 

While I was procrastinating on something else.... 

-----begin----- 

/* add_timestamp.c */ 
/* This messy, poorly commented code was authored by Horkan Smith, and I hearby release it into the public domain. No warranties expressed or implied. */ 

/* Copy stdin to stdout, printing the current time at the start of each line. */ 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <time.h> 


/* dump the current time to 'outstream' using 'ctime()' format w/out newline */ 
void showtime (FILE *outstream) { 

time_t t; 
char *p; 
int len; 

/* current time */ 
t = time(NULL); 

/* shouldn't ever happen w/ a NULL ptr arg, but it doesn't hurt. */ 
if ( ((time_t) -1) == t ) { 
perror("\ntime(NULL)"); 
exit(-1); 
} 

/* return a pointer to a string version of current time */ 
/* note: not thread safe - use ctime_r() if you use threads! */ 
p = ctime(&t); 

/* We've got to get rid of the newline at the end */ 
len = strlen(p) -1; 

if ((len >= 0) && (*(p + len) == '\n')) { 
*(p + len) = (char) 0; 
} 

/* could use printf, but sometimes it'll link smaller this way. */ 
fputs(p, outstream); fputc(':', outstream); fputc(' ', outstream); 

} 

int main (int argc, char *argv[], char *envp[]) { 

FILE *instream = stdin; 
FILE *outstream = stdout; 
int ch; 
int lastch; 

/* the ctime() man page says ctime is asctime(localtime(&t)) */ 
/* the localtime() man page suggests calling tzset before using localtime(), 
if you want to be portable */ 
tzset(); 


/* main loop, 
get a char 
if the last one was a newline, write the timestamp 
write the char out 
*/ 

lastch = '\n'; 
while (EOF != (ch = fgetc(instream))) { 

if ('\n' == lastch) { 
showtime(outstream); 
fflush(outstream); 
} 

fputc(ch, outstream); 
lastch = ch; 

} 
} 

------end------ 

On Tue, Nov 12, 2013 at 01:34:22PM -0500, Scott Plante wrote: 
> Does anyone happen to know of a command line tool that will read lines from standard input and write them to std out, pre-pending a timestamp? I have a process that emits messages to std out periodically as it processes and I'd like to write that to a log file, but with a time at the start of the line. I could do it with a script but a nice little command would be better, if it exists. 
> 
> 
> I'm looking for something that would perform the function of this script, maybe with an option for format: 
> 
> 
> while read line; 
> do 
> echo $(date +"%D %T") "$line"; 
> done 
> 
> 
> 
> Scott 

> _______________________________________________ 
> Ale mailing list 
> Ale at ale.org 
> http://mail.ale.org/mailman/listinfo/ale 
> See JOBS, ANNOUNCE and SCHOOLS lists at 
> http://mail.ale.org/mailman/listinfo 


-- 
Horkan Smith 
678-777-3263 cell, ale at horkan.net 
_______________________________________________ 
Ale mailing list 
Ale at ale.org 
http://mail.ale.org/mailman/listinfo/ale 
See JOBS, ANNOUNCE and SCHOOLS lists at 
http://mail.ale.org/mailman/listinfo 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20131119/6132cf2d/attachment.html>


More information about the Ale mailing list