2013-05-03 06:02:03 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2007-2012, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
2013-06-03 14:45:19 +00:00
|
|
|
*
|
2013-05-03 06:02:03 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2013-06-03 14:45:19 +00:00
|
|
|
*
|
2013-05-03 06:02:03 +00:00
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2013-06-03 14:45:19 +00:00
|
|
|
*
|
2013-05-03 06:02:03 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2011-06-13 14:46:03 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-06-18 20:42:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2011-06-13 14:46:03 +00:00
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "pcrepp/pcrepp.hh"
|
2014-01-25 17:29:35 +00:00
|
|
|
#include "textview_curses.hh"
|
2011-06-13 14:46:03 +00:00
|
|
|
#include "data_scanner.hh"
|
2011-06-14 03:07:39 +00:00
|
|
|
#include "data_parser.hh"
|
2013-06-03 14:45:19 +00:00
|
|
|
#include "log_format.hh"
|
2013-06-29 18:00:34 +00:00
|
|
|
#include "log_format_loader.hh"
|
2015-03-17 06:10:34 +00:00
|
|
|
#include "pretty_printer.hh"
|
|
|
|
#include "shared_buffer.hh"
|
2016-04-29 06:34:13 +00:00
|
|
|
#include "elem_to_json.hh"
|
2015-08-26 16:41:45 +00:00
|
|
|
#include "../src/data_parser.hh"
|
|
|
|
#include "../src/view_curses.hh"
|
2011-06-13 14:46:03 +00:00
|
|
|
|
2011-06-14 14:21:53 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2011-06-18 20:42:07 +00:00
|
|
|
const char *TMP_NAME = "scanned.tmp";
|
|
|
|
|
2016-12-06 00:34:30 +00:00
|
|
|
string execute_any(exec_context &ec, const string &cmdline_with_mode)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
void add_global_vars(exec_context &ec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-13 14:46:03 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-06-03 14:45:19 +00:00
|
|
|
int c, retval = EXIT_SUCCESS;
|
2015-03-17 06:10:34 +00:00
|
|
|
bool prompt = false, is_log = false, pretty_print = false;
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2013-06-29 18:00:34 +00:00
|
|
|
{
|
2020-10-21 05:55:46 +00:00
|
|
|
std::vector<ghc::filesystem::path> paths;
|
2019-07-30 05:18:32 +00:00
|
|
|
vector<string> errors;
|
2013-06-29 18:00:34 +00:00
|
|
|
|
2014-03-03 06:26:41 +00:00
|
|
|
load_formats(paths, errors);
|
2013-06-29 18:00:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 06:10:34 +00:00
|
|
|
while ((c = getopt(argc, argv, "pPl")) != -1) {
|
2013-06-03 14:45:19 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
prompt = true;
|
|
|
|
break;
|
|
|
|
|
2015-03-17 06:10:34 +00:00
|
|
|
case 'P':
|
|
|
|
pretty_print = true;
|
|
|
|
break;
|
|
|
|
|
2014-04-07 05:11:04 +00:00
|
|
|
case 'l':
|
|
|
|
is_log = true;
|
|
|
|
break;
|
|
|
|
|
2013-06-03 14:45:19 +00:00
|
|
|
default:
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
2011-06-13 14:46:03 +00:00
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2011-06-18 20:42:07 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2013-06-03 14:45:19 +00:00
|
|
|
if (retval != EXIT_SUCCESS) {}
|
2013-06-04 13:53:25 +00:00
|
|
|
else if (argc < 1) {
|
|
|
|
fprintf(stderr, "error: expecting file name argument(s)\n");
|
2013-06-03 14:45:19 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2011-06-18 20:42:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-06-04 13:53:25 +00:00
|
|
|
for (int lpc = 0; lpc < argc; lpc++) {
|
|
|
|
istream *in;
|
|
|
|
FILE * out;
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2013-06-04 13:53:25 +00:00
|
|
|
if (strcmp(argv[lpc], "-") == 0) {
|
|
|
|
in = &cin;
|
2013-06-03 14:45:19 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-06-04 13:53:25 +00:00
|
|
|
ifstream *ifs = new ifstream(argv[lpc]);
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2013-06-04 13:53:25 +00:00
|
|
|
if (!ifs->is_open()) {
|
|
|
|
fprintf(stderr, "error: unable to open file\n");
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
in = ifs;
|
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 13:53:25 +00:00
|
|
|
if ((out = fopen(TMP_NAME, "w")) == NULL) {
|
|
|
|
fprintf(stderr, "error: unable to temporary file for writing\n");
|
|
|
|
retval = EXIT_FAILURE;
|
2013-06-03 14:45:19 +00:00
|
|
|
}
|
2013-06-04 13:53:25 +00:00
|
|
|
else {
|
2020-12-01 19:27:03 +00:00
|
|
|
shared_ptr<log_format> format;
|
2013-06-12 04:10:59 +00:00
|
|
|
char *log_line;
|
2013-06-04 13:53:25 +00:00
|
|
|
bool found = false;
|
|
|
|
char cmd[2048];
|
|
|
|
string line;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
getline(*in, line);
|
|
|
|
if (strcmp(argv[lpc], "-") == 0) {
|
|
|
|
line = " " + line;
|
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2013-06-12 04:10:59 +00:00
|
|
|
log_line = (char *)alloca(line.length());
|
2013-06-04 13:53:25 +00:00
|
|
|
strcpy(log_line, &line[13]);
|
2014-10-20 05:16:40 +00:00
|
|
|
string sub_line = line.substr(13);
|
|
|
|
struct line_range body(0, sub_line.length());
|
|
|
|
shared_buffer share_manager;
|
|
|
|
shared_buffer_ref sbr;
|
|
|
|
|
|
|
|
sbr.share(share_manager, (char *)sub_line.c_str(), sub_line.size());
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2020-12-01 19:27:03 +00:00
|
|
|
auto &root_formats = log_format::get_root_formats();
|
|
|
|
vector<std::shared_ptr<log_format>>::iterator iter;
|
2013-06-04 13:53:25 +00:00
|
|
|
vector<logline> index;
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2014-04-07 05:11:04 +00:00
|
|
|
if (is_log) {
|
|
|
|
for (iter = root_formats.begin();
|
|
|
|
iter != root_formats.end() && !found;
|
|
|
|
++iter) {
|
2019-09-03 13:19:57 +00:00
|
|
|
line_info li = { {13}};
|
2019-06-15 13:32:02 +00:00
|
|
|
logfile *lf = nullptr; // XXX
|
|
|
|
|
2014-04-07 05:11:04 +00:00
|
|
|
(*iter)->clear();
|
2019-09-03 13:19:57 +00:00
|
|
|
if ((*iter)->scan(*lf, index, li, sbr) == log_format::SCAN_MATCH) {
|
2014-04-07 05:11:04 +00:00
|
|
|
format = (*iter)->specialized();
|
|
|
|
found = true;
|
|
|
|
}
|
2013-06-04 13:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
vector<logline_value> ll_values;
|
|
|
|
string_attrs_t sa;
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
if (format.get() != NULL) {
|
2019-03-13 23:37:41 +00:00
|
|
|
format->annotate(0, sbr, sa, ll_values);
|
2020-11-20 05:36:51 +00:00
|
|
|
body = find_string_attr_range(sa, &SA_BODY);
|
2013-06-04 13:53:25 +00:00
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
|
2013-06-08 13:10:18 +00:00
|
|
|
data_parser::TRACE_FILE = fopen("scanned.dpt", "w");
|
|
|
|
|
2013-06-04 13:53:25 +00:00
|
|
|
data_scanner ds(sub_line, body.lr_start, sub_line.length());
|
|
|
|
data_parser dp(&ds);
|
2015-08-26 16:41:45 +00:00
|
|
|
string msg_format;
|
2013-06-04 13:53:25 +00:00
|
|
|
|
2015-08-26 16:41:45 +00:00
|
|
|
dp.dp_msg_format = &msg_format;
|
2013-06-04 13:53:25 +00:00
|
|
|
dp.parse();
|
|
|
|
dp.print(out, dp.dp_pairs);
|
2015-08-26 16:41:45 +00:00
|
|
|
fprintf(out, "msg :%s\n", sub_line.c_str() + body.lr_start);
|
|
|
|
fprintf(out, "format :%s\n", msg_format.c_str());
|
2015-03-17 06:10:34 +00:00
|
|
|
|
|
|
|
if (pretty_print) {
|
|
|
|
data_scanner ds2(sub_line, body.lr_start, sub_line.length());
|
2018-05-10 13:44:03 +00:00
|
|
|
pretty_printer pp(&ds2, sa);
|
|
|
|
attr_line_t pretty_out;
|
2015-03-17 06:10:34 +00:00
|
|
|
|
2018-05-10 13:44:03 +00:00
|
|
|
pp.append_to(pretty_out);
|
|
|
|
fprintf(out, "\n--\n%s", pretty_out.get_string().c_str());
|
2015-03-17 06:10:34 +00:00
|
|
|
}
|
2016-04-29 06:34:13 +00:00
|
|
|
|
|
|
|
auto_mem<yajl_gen_t> gen(yajl_gen_free);
|
|
|
|
|
|
|
|
gen = yajl_gen_alloc(NULL);
|
|
|
|
yajl_gen_config(gen.in(), yajl_gen_beautify, true);
|
|
|
|
|
|
|
|
elements_to_json(gen, dp, &dp.dp_pairs);
|
|
|
|
|
|
|
|
const unsigned char *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
yajl_gen_get_buf(gen, &buf, &len);
|
|
|
|
fwrite(buf, 1, len, out);
|
|
|
|
|
2013-06-04 13:53:25 +00:00
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
sprintf(cmd, "diff -u %s %s", argv[lpc], TMP_NAME);
|
|
|
|
rc = system(cmd);
|
|
|
|
if (rc != 0) {
|
|
|
|
if (prompt) {
|
|
|
|
char resp[4];
|
|
|
|
|
2016-04-29 06:34:13 +00:00
|
|
|
printf("\nOriginal line:\n%s\n", sub_line.c_str() + body.lr_start);
|
2013-06-04 13:53:25 +00:00
|
|
|
printf("Would you like to update the original file? (y/N) ");
|
|
|
|
fflush(stdout);
|
2015-04-02 20:44:27 +00:00
|
|
|
log_perror(scanf("%3s", resp));
|
2013-06-05 02:45:04 +00:00
|
|
|
if (strcasecmp(resp, "y") == 0) {
|
2013-06-04 13:53:25 +00:00
|
|
|
rename(TMP_NAME, argv[lpc]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
2013-06-03 14:45:19 +00:00
|
|
|
}
|
2013-06-04 13:53:25 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "error: mismatch\n");
|
2013-06-03 14:45:19 +00:00
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-08 13:10:18 +00:00
|
|
|
|
|
|
|
fclose(data_parser::TRACE_FILE);
|
2013-06-08 13:27:36 +00:00
|
|
|
data_parser::TRACE_FILE = NULL;
|
2013-06-03 14:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-14 14:21:53 +00:00
|
|
|
}
|
2011-06-18 20:42:07 +00:00
|
|
|
|
|
|
|
return retval;
|
2011-06-13 14:46:03 +00:00
|
|
|
}
|