<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 12pt; color: #000000'>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. <div><br></div><div>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.</div><div><br></div><div>Scott<br><br><hr id="zwchr"><div style="color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Horkan Smith" <ale@horkan.net><br><b>To: </b>"Atlanta Linux Enthusiasts" <ale@ale.org><br><b>Sent: </b>Wednesday, November 13, 2013 7:43:52 AM<br><b>Subject: </b>Re: [ale] line timestamp command<br><br>While I was procrastinating on something else....<br><br>-----begin-----<br><br>/* add_timestamp.c */<br>/* This messy, poorly commented code was authored by Horkan Smith, and I hearby release it into the public domain. No warranties expressed or implied. */<br><br>/* Copy stdin to stdout, printing the current time at the start of each line. */<br><br>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#include <time.h><br><br><br>/* dump the current time to 'outstream' using 'ctime()' format w/out newline */<br>void showtime (FILE *outstream) {<br><br> time_t t;<br> char *p;<br> int len;<br><br> /* current time */<br> t = time(NULL);<br><br> /* shouldn't ever happen w/ a NULL ptr arg, but it doesn't hurt. */<br> if ( ((time_t) -1) == t ) {<br> perror("\ntime(NULL)");<br> exit(-1);<br> }<br><br> /* return a pointer to a string version of current time */<br> /* note: not thread safe - use ctime_r() if you use threads! */<br> p = ctime(&t);<br><br> /* We've got to get rid of the newline at the end */<br> len = strlen(p) -1;<br><br> if ((len >= 0) && (*(p + len) == '\n')) {<br> *(p + len) = (char) 0;<br> }<br><br> /* could use printf, but sometimes it'll link smaller this way. */<br> fputs(p, outstream); fputc(':', outstream); fputc(' ', outstream);<br><br>}<br><br>int main (int argc, char *argv[], char *envp[]) {<br><br> FILE *instream = stdin;<br> FILE *outstream = stdout;<br> int ch;<br> int lastch;<br><br> /* the ctime() man page says ctime is asctime(localtime(&t)) */<br> /* the localtime() man page suggests calling tzset before using localtime(),<br> if you want to be portable */<br> tzset();<br><br><br> /* main loop,<br> get a char<br> if the last one was a newline, write the timestamp<br> write the char out<br> */<br><br> lastch = '\n';<br> while (EOF != (ch = fgetc(instream))) {<br> <br> if ('\n' == lastch) {<br> showtime(outstream);<br> fflush(outstream);<br> }<br><br> fputc(ch, outstream);<br> lastch = ch;<br><br> }<br>}<br><br>------end------<br><br>On Tue, Nov 12, 2013 at 01:34:22PM -0500, Scott Plante wrote:<br>> 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. <br>> <br>> <br>> I'm looking for something that would perform the function of this script, maybe with an option for format: <br>> <br>> <br>> while read line; <br>> do <br>> echo $(date +"%D %T") "$line"; <br>> done <br>> <br>> <br>> <br>> Scott <br><br>> _______________________________________________<br>> Ale mailing list<br>> Ale@ale.org<br>> http://mail.ale.org/mailman/listinfo/ale<br>> See JOBS, ANNOUNCE and SCHOOLS lists at<br>> http://mail.ale.org/mailman/listinfo<br><br><br>-- <br>Horkan Smith<br>678-777-3263 cell, ale@horkan.net<br>_______________________________________________<br>Ale mailing list<br>Ale@ale.org<br>http://mail.ale.org/mailman/listinfo/ale<br>See JOBS, ANNOUNCE and SCHOOLS lists at<br>http://mail.ale.org/mailman/listinfo<br></div><br></div></div></body></html>