[pmg-devel] [PATCH pmg-log-filter v2 2/8] add -i option for specifying an external file
Dominik Csapak
d.csapak at proxmox.com
Thu Feb 21 09:42:01 CET 2019
where '-' is stdin
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from v1:
* check also for length == 1 when checking '-'
pmg-log-tracker.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/pmg-log-tracker.c b/pmg-log-tracker.c
index 8d5e02f..21145fa 100644
--- a/pmg-log-tracker.c
+++ b/pmg-log-tracker.c
@@ -1766,6 +1766,7 @@ main (int argc, char * const argv[])
{
char linebuf[linebufsize];
char *line;
+ char *inputfile = NULL;
const char *text;
const char *idx1;
@@ -1799,7 +1800,7 @@ main (int argc, char * const argv[])
exit (-1);
}
- while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:l:vgn")) != -1) {
+ while ((opt = getopt (argc, argv, "f:t:s:e:h:m:q:x:i:l:vgn")) != -1) {
if (opt == 'f') {
parser->from = epool_strdup (&parser->ep, optarg);
} else if (opt == 't') {
@@ -1832,6 +1833,8 @@ main (int argc, char * const argv[])
}
} else if (opt == 'x') {
parser->strmatch = epool_strdup (&parser->ep, optarg);
+ } else if (opt == 'i') {
+ inputfile = optarg;
} else if (opt == 'l') {
char *l;
parser->limit = strtoul (optarg, &l, 0);
@@ -1911,7 +1914,9 @@ main (int argc, char * const argv[])
}
int filecount;
- if ((filecount = parser_count_files (parser)) <= 0) {
+ if (inputfile) {
+ filecount = 1;
+ } else if ((filecount = parser_count_files (parser)) <= 0) {
fprintf (stderr, "unable to access log files\n");
exit (-1);
}
@@ -1960,7 +1965,14 @@ main (int argc, char * const argv[])
cur_year = parser->year[i];
if (i <= 1) {
- if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue;
+ if (inputfile && strlen(inputfile) == 1 && *inputfile == '-') {
+ stream = (gpointer) stdin;
+ } else if (inputfile) {
+ if (!(stream = (gpointer) fopen (inputfile, "r"))) {
+ fprintf(stderr, "unable to open log file\n");
+ exit (-1);
+ }
+ } else if (!(stream = (gpointer) fopen (logfiles[i], "r"))) continue;
} else {
if (!(stream = (gpointer) gzopen (logfiles[i], "r"))) continue;
}
--
2.11.0
More information about the pmg-devel
mailing list