<div dir="ltr"><div>Hi, Horkan.</div><div><br></div>That reminds me of an issue I encountered when writing this kind of C program. If you put it in a pipe line, so that the C library's standard I/O subsystem can't see that stdout is a terminal, like this, where I add cat,<div>
<br></div><div><div> ecashin@atala horkan$ { echo test; sleep 2; echo test; } | ./a.out | cat</div></div><div><br></div><div>... then the output doesn't come out as some users might expect---delays are introduced while stdio waits for more output to buffer. In this case, the timestamp appears right away, but its message only appears when the second timestamped message does.</div>
<div><br></div><div>You can tell stdio to line-buffer the output and get consistent behavior in cases where this matters---i.e., where a human might be watching and wondering, "Where the heck is my output?" :)</div>
<div><br></div><div><div> ecashin@atala horkan$ gcc -Wall ts.c </div><div> ecashin@atala horkan$ { echo test; sleep 2; echo test; } | ./a.out | cat</div><div> Thu Nov 14 08:47:23 2013: test</div><div> Thu Nov 14 08:47:25 2013: test</div>
<div> ecashin@atala horkan$ diff -u ts.c.orig ts.c</div><div> --- ts.c.orig 2013-11-14 08:44:08.000000000 -0500</div><div> +++ ts.c 2013-11-14 08:47:15.000000000 -0500</div><div> @@ -48,6 +48,8 @@</div><div> int ch;</div>
<div> int lastch;</div><div> </div><div> + setlinebuf(stdout);</div><div> +</div><div> /* the ctime() man page says ctime is asctime(localtime(&t)) */</div><div> /* the localtime() man page suggests calling tzset before using localtime(),</div>
<div> if you want to be portable */</div><div> @@ -72,4 +74,6 @@</div><div> lastch = ch;</div><div> </div><div> }</div><div> +</div><div> + return 0;</div><div> }</div><div> ecashin@atala horkan$ </div>
</div><div><br></div><div>I could swear that many years ago, that (setvbuf stuff) didn't work for me on Linux, but who knows.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 13, 2013 at 7:43 AM, Horkan Smith <span dir="ltr"><<a href="mailto:ale@horkan.net" target="_blank">ale@horkan.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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>
<div class="HOEnZb"><div class="h5"><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>
</div></div><div class="im HOEnZb">> _______________________________________________<br>
> Ale mailing list<br>
> <a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
> <a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
> See JOBS, ANNOUNCE and SCHOOLS lists at<br>
> <a href="http://mail.ale.org/mailman/listinfo" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
<br>
<br>
</div><span class="HOEnZb"><font color="#888888">--<br>
Horkan Smith<br>
<a href="tel:678-777-3263" value="+16787773263">678-777-3263</a> cell, <a href="mailto:ale@horkan.net">ale@horkan.net</a><br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br> Ed Cashin <<a href="mailto:ecashin@noserose.net">ecashin@noserose.net</a>><br> <a href="http://noserose.net/e/">http://noserose.net/e/</a><br>
<a href="http://www.coraid.com/">http://www.coraid.com/</a>
</div><div class="gmail_extra"><br></div></div>