aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2018-06-22 11:04:44 +0300
committerSergey Poznyakoff <gray@gnu.org>2018-06-22 12:23:18 +0300
commita954b80eb5f01bf96946c28a07b3648650bfae16 (patch)
tree1bef349de44ff52ee51176bcffb1ee688d11d5c9
parentab01873f6f7e26fde46e6a7808742289d6be28d6 (diff)
downloadaddts-a954b80eb5f01bf96946c28a07b3648650bfae16.tar.gz
addts-a954b80eb5f01bf96946c28a07b3648650bfae16.tar.bz2
Rewrite state map. Implement suffix mode (-s option). Remove -i (ignore) option.
-rw-r--r--addts.120
-rw-r--r--addts.c78
2 files changed, 67 insertions, 31 deletions
diff --git a/addts.1 b/addts.1
index bd4664b..fce0eb3 100644
--- a/addts.1
+++ b/addts.1
@@ -1,13 +1,12 @@
-.TH ADDTS 1 "June 20, 2018" ADDTS "User Commands"
+.TH ADDTS 1 "June 22, 2018" ADDTS "User Commands"
.SH NAME
addts \- add timestamps at the beginning of each line
.SH SYNOPSIS
.nh
.na
\fBaddts\fR\
- [\fB\-au\fR]\
+ [\fB\-asu\fR]\
[\fB\-f \fIFMT\fR]\
- [\fB\-i \fINUM\fR]\
[\fB\-w \fICHR\fR]\
[\fIFILE\fR]
.PP
@@ -16,12 +15,19 @@ addts \- add timestamps at the beginning of each line
.hy
.SH DESCRIPTION
Reads standard input and writes it to standard output, adding a
-timestamp at the begginning of each line. If \fIFILE\fR is suppied,
+timestamp at the begginning (or end) of each line. If \fIFILE\fR is suppied,
writes to it instead. Typical usage is for Apache forensic log:
.PP
.EX
ForensicLog "|/usr/bin/addts -a /var/log/httpd/forensic.log"
.EE
+.PP
+To add timestamps to the end of each line, formatting it as an
+additional header:
+.PP
+.EX
+ForensicLog "|/usr/bin/addts -a -s -f '|X-TS:%c' /var/log/httpd/forensic.log"
+.EE
.SH OPTIONS
.TP
.B \-a
@@ -37,8 +43,8 @@ which is replaced with micronseconds.
.sp
See also \fB\-w\fR, below.
.TP
-.BI \-i " NUM"
-Pass first \fINUM\fR lines unchanged.
+.B \-s
+Add timestamp to the end of each line (suffix mode).
.TP
.B \-u
Report times in UTC.
@@ -55,6 +61,8 @@ ForensicLog "|/usr/bin/addts -w_ -f %Y-%m-%d_%H:%M:%S.%@:_ \\
.EE
.SH BUGS
Formatted timestamp cannot be longer than 511 bytes.
+.PP
+Empty lines are left unchanged (no timestamp added).
.SH "SEE ALSO"
.BR strftime (3).
.SH AUTHORS
diff --git a/addts.c b/addts.c
index abd3c9a..07fae4f 100644
--- a/addts.c
+++ b/addts.c
@@ -8,21 +8,65 @@
static char default_fmt[] = "%Y-%m-%d %H:%M:%S: ";
+enum mode {
+ PREFIX,
+ SUFFIX,
+ NMODE
+};
+
+enum state {
+ SINI, /* initial */
+ SEOL, /* end of line */
+ SLIN, /* Midle of line */
+ STIM, /* Time to insert timestamp */
+ NSTATE
+};
+
+enum alpha {
+ ACHR,
+ AEOL,
+ NALPHA
+};
+
+int prefix_trans[NSTATE][NALPHA] = {
+ /* ACHR AEOL */
+ { STIM, SEOL }, /* SINI */
+ { STIM, SEOL }, /* SEOL */
+ { SLIN, SEOL }, /* SLIN */
+ { SLIN, SEOL } /* STIM */
+};
+
+int suffix_trans[NSTATE][NALPHA] = {
+ /* ACHR AEOL */
+ { SLIN, SEOL }, /* SINI */
+ { SLIN, SEOL }, /* SEOL */
+ { SLIN, STIM }, /* SLIN */
+ { SLIN, SEOL } /* STIM */
+};
+
+static inline int
+alpha(int c)
+{
+ return c == '\n' ? AEOL : ACHR;
+}
+
int
main(int argc, char **argv)
{
int c;
char buf[512];
- int eol = 0;
int utc_opt = 0;
int append_opt = 0;
int ws_opt = 0;
- unsigned long ignore = 0;
+ int end_opt = 0;
char *p;
char *fmt = NULL;
FILE *fp;
- while ((c = getopt(argc, argv, "?af:i:uw:")) != EOF) {
+ enum state state = SINI;
+ int (*trans)[NALPHA] = prefix_trans;
+
+ while ((c = getopt(argc, argv, "?af:suw:")) != EOF) {
switch (c) {
case 'a':
append_opt = 1;
@@ -30,15 +74,8 @@ main(int argc, char **argv)
case 'f':
fmt = optarg;
break;
- case 'i':
- errno = 0;
- ignore = strtoul(optarg, &p, 10);
- if (errno || *p) {
- fprintf(stderr,
- "%s: -i argument is not a number\n",
- argv[0]);
- return 1;
- }
+ case 's':
+ trans = suffix_trans;
break;
case 'u':
utc_opt = 1;
@@ -48,7 +85,8 @@ main(int argc, char **argv)
break;
default:
if (optopt == 0) {
- printf("usage: %s [-a] [-f FMT] [-i N] [-u] [OUT-FILE]\n", argv[0]);
+ printf("usage: %s [-asu] [-f FMT] [OUT-FILE]\n",
+ argv[0]);
return 0;
}
return 1;
@@ -84,18 +122,9 @@ main(int argc, char **argv)
}
setvbuf(fp, NULL, _IOLBF, 0);
- if (ignore)
- ignore--;
- else
- eol = 1;
-
while ((c = getchar()) != EOF) {
- if (c == '\n') {
- if (ignore)
- ignore--;
- else
- eol = 1;
- } else if (eol) {
+ state = trans[state][alpha(c)];
+ if (state == STIM) {
struct timeval tv;
struct tm *tm;
size_t sz;
@@ -116,7 +145,6 @@ main(int argc, char **argv)
start = p + 2;
}
fwrite(start, 1, sz, fp);
- eol = 0;
}
fputc(c, fp);
}

Return to:

Send suggestions and report system problems to the System administrator.