[ale] line timestamp command

Horkan Smith ale at horkan.net
Wed Nov 13 07:43:52 EST 2013


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


More information about the Ale mailing list