2003-08-23 17:44:14 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2005-04-22 15:42:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2003-08-23 17:44:14 +02:00
|
|
|
|
|
|
|
static char *cmnd;
|
|
|
|
|
|
|
|
static void usage(void) {
|
2009-02-02 12:39:33 +01:00
|
|
|
fprintf(stderr, "Usage: %s [-u] [-s] [-r] [-o file]\n", cmnd);
|
2003-08-23 17:44:14 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int c;
|
|
|
|
int use_microseconds = 0;
|
2009-02-02 12:39:33 +01:00
|
|
|
int use_seconds = 0;
|
2003-08-23 17:44:14 +02:00
|
|
|
int print_ts = 1;
|
2005-04-22 15:42:10 +02:00
|
|
|
char lastfile[FILENAME_MAX] = "", file[FILENAME_MAX] = "";
|
|
|
|
char *filepat = NULL;
|
|
|
|
FILE *fp = NULL;
|
2009-02-02 12:39:33 +01:00
|
|
|
struct timeval start = { 0, 0 };
|
2003-08-23 17:44:14 +02:00
|
|
|
|
|
|
|
cmnd = argv[0];
|
|
|
|
|
2009-02-02 12:39:33 +01:00
|
|
|
while ((c = getopt(argc, argv, "suro:")) != EOF) {
|
2003-08-23 17:44:14 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'u':
|
|
|
|
use_microseconds = 1;
|
|
|
|
break;
|
2005-04-22 15:42:10 +02:00
|
|
|
case 'o':
|
|
|
|
filepat = optarg;
|
|
|
|
break;
|
2009-02-02 12:39:33 +01:00
|
|
|
case 's':
|
|
|
|
use_seconds = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
use_seconds = 1;
|
|
|
|
use_microseconds = 1;
|
|
|
|
break;
|
2003-08-23 17:44:14 +02:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2005-04-22 15:42:10 +02:00
|
|
|
if (!filepat) {
|
|
|
|
fp = stdout;
|
|
|
|
setlinebuf(fp);
|
|
|
|
}
|
2003-08-23 17:44:14 +02:00
|
|
|
while ((c = getchar()) != EOF) {
|
|
|
|
if (print_ts) {
|
|
|
|
struct timeval tv;
|
|
|
|
char s[sizeof("yyyy-mm-ddThh:mm:ss")];
|
|
|
|
gettimeofday(&tv, NULL);
|
2005-04-22 15:42:10 +02:00
|
|
|
if (filepat) {
|
|
|
|
strftime(file, sizeof(file), filepat,
|
|
|
|
localtime(&tv.tv_sec));
|
|
|
|
if (strcmp(lastfile, file) != 0) {
|
|
|
|
if (fp) {
|
|
|
|
if (fclose(fp) == EOF) {
|
|
|
|
fprintf(stderr, "%s: cannot close %s: %s\n", argv[0], lastfile, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-04-25 22:28:19 +02:00
|
|
|
if ((fp = fopen(file, "a")) == NULL) {
|
2005-04-22 15:42:10 +02:00
|
|
|
fprintf(stderr, "%s: cannot open %s: %s\n", argv[0], file, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
setlinebuf(fp);
|
|
|
|
strcpy(lastfile, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 12:39:33 +01:00
|
|
|
if (use_seconds) {
|
|
|
|
tv.tv_usec -= start.tv_usec;
|
|
|
|
if (tv.tv_usec < 0) {
|
|
|
|
tv.tv_usec += 1000000;
|
|
|
|
tv.tv_sec -= 1;
|
|
|
|
}
|
|
|
|
tv.tv_sec -= start.tv_sec;
|
|
|
|
fprintf(fp, "%ld", (long)tv.tv_sec);
|
|
|
|
} else {
|
|
|
|
strftime(s, sizeof(s), "%Y-%m-%dT%H:%M:%S",
|
|
|
|
localtime(&tv.tv_sec));
|
|
|
|
fputs(s, fp);
|
|
|
|
}
|
2003-08-23 17:44:14 +02:00
|
|
|
if (use_microseconds) {
|
2005-04-22 15:42:10 +02:00
|
|
|
fprintf(fp, ".%06ld ", (long)tv.tv_usec);
|
2003-08-23 17:44:14 +02:00
|
|
|
}
|
2005-04-22 15:42:10 +02:00
|
|
|
putc(' ', fp);
|
2003-08-23 17:44:14 +02:00
|
|
|
print_ts = 0;
|
|
|
|
}
|
2005-04-22 15:42:10 +02:00
|
|
|
putc(c, fp);
|
2003-08-23 17:44:14 +02:00
|
|
|
if (c == '\n') print_ts = 1;
|
|
|
|
}
|
2005-04-22 15:42:10 +02:00
|
|
|
if (!print_ts) putc('\n', fp); // force newline at end of output
|
2003-08-23 17:44:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|