2013-05-03 10:02:03 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2007-2012, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
2013-06-03 18:45:19 +04:00
|
|
|
*
|
2013-05-03 10:02:03 +04: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 18:45:19 +04:00
|
|
|
*
|
2013-05-03 10:02:03 +04: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 18:45:19 +04:00
|
|
|
*
|
2013-05-03 10:02:03 +04: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;
|
2022-03-17 01:38:08 +03:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2013-05-03 10:02:03 +04:00
|
|
|
* (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 18:46:03 +04:00
|
|
|
|
2022-02-20 02:35:40 +03:00
|
|
|
#ifdef __CYGWIN__
|
2022-03-17 01:38:08 +03:00
|
|
|
# include <alloca.h>
|
2022-02-20 02:35:40 +03:00
|
|
|
#endif
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2011-06-13 18:46:03 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-06-19 00:42:07 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-09-09 07:31:16 +03:00
|
|
|
#include "base/injector.bind.hh"
|
2021-10-03 09:17:33 +03:00
|
|
|
#include "base/injector.hh"
|
2022-03-17 01:38:08 +03:00
|
|
|
#include "config.h"
|
2022-05-11 07:58:32 +03:00
|
|
|
#include "data_parser.hh"
|
2022-03-17 01:38:08 +03:00
|
|
|
#include "data_scanner.hh"
|
|
|
|
#include "elem_to_json.hh"
|
2013-06-03 18:45:19 +04:00
|
|
|
#include "log_format.hh"
|
2013-06-29 22:00:34 +04:00
|
|
|
#include "log_format_loader.hh"
|
2022-05-11 07:58:32 +03:00
|
|
|
#include "logfile.hh"
|
2015-03-17 09:10:34 +03:00
|
|
|
#include "pretty_printer.hh"
|
|
|
|
#include "shared_buffer.hh"
|
2022-05-11 07:58:32 +03:00
|
|
|
#include "view_curses.hh"
|
2011-06-13 18:46:03 +04:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
const char* TMP_NAME = "scanned.tmp";
|
2011-06-19 00:42:07 +04:00
|
|
|
|
2023-09-09 07:31:16 +03:00
|
|
|
static auto bound_file_options_hier
|
|
|
|
= injector::bind<lnav::safe_file_options_hier>::to_singleton();
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2011-06-13 18:46:03 +04:00
|
|
|
{
|
2022-03-17 01:38:08 +03:00
|
|
|
int c, retval = EXIT_SUCCESS;
|
2015-03-17 09:10:34 +03:00
|
|
|
bool prompt = false, is_log = false, pretty_print = false;
|
2022-09-16 07:11:30 +03:00
|
|
|
bool scanner_details = false;
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2021-10-03 09:17:33 +03:00
|
|
|
{
|
2022-03-17 01:38:08 +03:00
|
|
|
static auto builtin_formats
|
|
|
|
= injector::get<std::vector<std::shared_ptr<log_format>>>();
|
2021-10-03 09:17:33 +03:00
|
|
|
auto& root_formats = log_format::get_root_formats();
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
log_format::get_root_formats().insert(root_formats.begin(),
|
|
|
|
builtin_formats.begin(),
|
|
|
|
builtin_formats.end());
|
2021-10-03 09:17:33 +03:00
|
|
|
builtin_formats.clear();
|
|
|
|
}
|
|
|
|
|
2013-06-29 22:00:34 +04:00
|
|
|
{
|
2024-05-22 09:45:48 +03:00
|
|
|
std::vector<std::filesystem::path> paths;
|
2022-04-13 02:07:13 +03:00
|
|
|
std::vector<lnav::console::user_message> errors;
|
2013-06-29 22:00:34 +04:00
|
|
|
|
2014-03-03 10:26:41 +04:00
|
|
|
load_formats(paths, errors);
|
2013-06-29 22:00:34 +04:00
|
|
|
}
|
|
|
|
|
2022-09-16 07:11:30 +03:00
|
|
|
while ((c = getopt(argc, argv, "pPls")) != -1) {
|
2013-06-03 18:45:19 +04:00
|
|
|
switch (c) {
|
2022-03-17 01:38:08 +03:00
|
|
|
case 'p':
|
|
|
|
prompt = true;
|
|
|
|
break;
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
case 'P':
|
|
|
|
pretty_print = true;
|
|
|
|
break;
|
2015-03-17 09:10:34 +03:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
case 'l':
|
|
|
|
is_log = true;
|
|
|
|
break;
|
2014-04-07 09:11:04 +04:00
|
|
|
|
2022-09-16 07:11:30 +03:00
|
|
|
case 's':
|
|
|
|
scanner_details = true;
|
|
|
|
break;
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
default:
|
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
break;
|
2013-06-03 18:45:19 +04:00
|
|
|
}
|
2011-06-13 18:46:03 +04:00
|
|
|
}
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2011-06-19 00:42:07 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
if (retval != EXIT_SUCCESS) {
|
|
|
|
} else if (argc < 1) {
|
2013-06-04 17:53:25 +04:00
|
|
|
fprintf(stderr, "error: expecting file name argument(s)\n");
|
2013-06-03 18:45:19 +04:00
|
|
|
retval = EXIT_FAILURE;
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2013-06-04 17:53:25 +04:00
|
|
|
for (int lpc = 0; lpc < argc; lpc++) {
|
2022-03-31 18:59:19 +03:00
|
|
|
std::unique_ptr<std::ifstream> in_ptr;
|
|
|
|
std::istream* in;
|
2022-03-17 01:38:08 +03:00
|
|
|
FILE* out;
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2013-06-04 17:53:25 +04:00
|
|
|
if (strcmp(argv[lpc], "-") == 0) {
|
2022-03-31 18:59:19 +03:00
|
|
|
in = &std::cin;
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2022-03-31 18:59:19 +03:00
|
|
|
auto ifs = std::make_unique<std::ifstream>(argv[lpc]);
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2013-06-04 17:53:25 +04:00
|
|
|
if (!ifs->is_open()) {
|
|
|
|
fprintf(stderr, "error: unable to open file\n");
|
|
|
|
retval = EXIT_FAILURE;
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2021-02-26 09:22:16 +03:00
|
|
|
in_ptr = std::move(ifs);
|
|
|
|
in = in_ptr.get();
|
2013-06-04 17:53:25 +04:00
|
|
|
}
|
2013-06-03 18:45:19 +04:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:22:16 +03:00
|
|
|
if ((out = fopen(TMP_NAME, "w")) == nullptr) {
|
2022-03-17 01:38:08 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"error: unable to temporary file for writing\n");
|
2013-06-04 17:53:25 +04:00
|
|
|
retval = EXIT_FAILURE;
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2022-03-31 18:59:19 +03:00
|
|
|
std::shared_ptr<log_format> format;
|
2022-03-17 01:38:08 +03:00
|
|
|
char* log_line;
|
2013-06-04 17:53:25 +04:00
|
|
|
bool found = false;
|
2022-03-17 01:38:08 +03:00
|
|
|
char cmd[2048];
|
2022-03-31 18:59:19 +03:00
|
|
|
std::string line;
|
2022-03-17 01:38:08 +03:00
|
|
|
int rc;
|
2013-06-04 17:53:25 +04:00
|
|
|
|
|
|
|
getline(*in, line);
|
|
|
|
if (strcmp(argv[lpc], "-") == 0) {
|
|
|
|
line = " " + line;
|
|
|
|
}
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
log_line = (char*) alloca(line.length());
|
2013-06-04 17:53:25 +04:00
|
|
|
strcpy(log_line, &line[13]);
|
2022-03-31 18:59:19 +03:00
|
|
|
auto sub_line = line.substr(13);
|
2014-10-20 09:16:40 +04:00
|
|
|
struct line_range body(0, sub_line.length());
|
|
|
|
shared_buffer share_manager;
|
2022-07-25 09:29:46 +03:00
|
|
|
logline_value_vector ll_values;
|
|
|
|
auto& sbr = ll_values.lvv_sbr;
|
2014-10-20 09:16:40 +04:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
sbr.share(
|
|
|
|
share_manager, (char*) sub_line.c_str(), sub_line.size());
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
auto& root_formats = log_format::get_root_formats();
|
2022-03-31 18:59:19 +03:00
|
|
|
std::vector<std::shared_ptr<log_format>>::iterator iter;
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2014-04-07 09:11:04 +04:00
|
|
|
if (is_log) {
|
2023-06-25 09:12:29 +03:00
|
|
|
std::vector<logline> index;
|
2022-05-11 07:58:32 +03:00
|
|
|
logfile_open_options loo;
|
|
|
|
auto open_res = logfile::open(argv[lpc], loo);
|
|
|
|
auto lf = open_res.unwrap();
|
2022-06-29 08:23:56 +03:00
|
|
|
ArenaAlloc::Alloc<char> allocator;
|
|
|
|
scan_batch_context sbc{allocator};
|
2014-04-07 09:11:04 +04:00
|
|
|
for (iter = root_formats.begin();
|
2021-01-01 00:55:21 +03:00
|
|
|
iter != root_formats.end() && !found;
|
2022-09-10 19:28:07 +03:00
|
|
|
++iter)
|
|
|
|
{
|
2022-03-17 01:38:08 +03:00
|
|
|
line_info li = {{13}};
|
2019-06-15 16:32:02 +03:00
|
|
|
|
2014-04-07 09:11:04 +04:00
|
|
|
(*iter)->clear();
|
2023-06-20 19:38:11 +03:00
|
|
|
if ((*iter)
|
|
|
|
->scan(*lf, index, li, sbr, sbc)
|
|
|
|
.is<log_format::scan_match>())
|
2022-09-10 19:28:07 +03:00
|
|
|
{
|
2014-04-07 09:11:04 +04:00
|
|
|
format = (*iter)->specialized();
|
|
|
|
found = true;
|
|
|
|
}
|
2013-06-04 17:53:25 +04:00
|
|
|
}
|
2021-01-01 00:55:21 +03:00
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
fprintf(stderr, "error: log sample does not match\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-06-04 17:53:25 +04:00
|
|
|
}
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2018-05-10 16:44:03 +03:00
|
|
|
string_attrs_t sa;
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2021-02-26 09:22:16 +03:00
|
|
|
if (format.get() != nullptr) {
|
2024-05-07 08:36:03 +03:00
|
|
|
format->annotate(nullptr, 0, sa, ll_values, false);
|
2020-11-20 08:36:51 +03:00
|
|
|
body = find_string_attr_range(sa, &SA_BODY);
|
2013-06-04 17:53:25 +04:00
|
|
|
}
|
2013-06-03 18:45:19 +04:00
|
|
|
|
2013-06-08 17:10:18 +04:00
|
|
|
data_parser::TRACE_FILE = fopen("scanned.dpt", "w");
|
2024-01-26 07:53:01 +03:00
|
|
|
setvbuf(data_parser::TRACE_FILE, nullptr, _IONBF, 0);
|
2013-06-08 17:10:18 +04:00
|
|
|
|
2022-09-10 19:28:07 +03:00
|
|
|
data_scanner ds(sub_line, body.lr_start);
|
2022-09-16 07:11:30 +03:00
|
|
|
|
|
|
|
if (scanner_details) {
|
|
|
|
fprintf(out,
|
|
|
|
" %s\n",
|
|
|
|
ds.get_input().to_string().c_str());
|
|
|
|
while (true) {
|
|
|
|
auto tok_res = ds.tokenize2();
|
|
|
|
|
|
|
|
if (!tok_res) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out,
|
|
|
|
"%4s %3d:%-3d ",
|
|
|
|
data_scanner::token2name(tok_res->tr_token),
|
|
|
|
tok_res->tr_capture.c_begin,
|
|
|
|
tok_res->tr_capture.c_end);
|
|
|
|
size_t cap_index = 0;
|
|
|
|
for (; cap_index < tok_res->tr_capture.c_end;
|
|
|
|
cap_index++)
|
|
|
|
{
|
|
|
|
if (cap_index == tok_res->tr_capture.c_begin) {
|
|
|
|
fputc('^', out);
|
|
|
|
} else if (cap_index
|
|
|
|
== (tok_res->tr_capture.c_end - 1))
|
|
|
|
{
|
|
|
|
fputc('^', out);
|
|
|
|
} else if (cap_index > tok_res->tr_capture.c_begin)
|
|
|
|
{
|
|
|
|
fputc('-', out);
|
|
|
|
} else {
|
|
|
|
fputc(' ', out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; cap_index < (int) ds.get_input().length();
|
|
|
|
cap_index++)
|
|
|
|
{
|
|
|
|
fputc(' ', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sub = tok_res->to_string();
|
|
|
|
fprintf(out, " %s\n", sub.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ds.reset();
|
2022-03-17 01:38:08 +03:00
|
|
|
data_parser dp(&ds);
|
2022-03-31 18:59:19 +03:00
|
|
|
std::string msg_format;
|
2013-06-04 17:53:25 +04:00
|
|
|
|
2015-08-26 19:41:45 +03:00
|
|
|
dp.dp_msg_format = &msg_format;
|
2013-06-04 17:53:25 +04:00
|
|
|
dp.parse();
|
|
|
|
dp.print(out, dp.dp_pairs);
|
2022-03-17 01:38:08 +03:00
|
|
|
fprintf(
|
|
|
|
out, "msg :%s\n", sub_line.c_str() + body.lr_start);
|
2015-08-26 19:41:45 +03:00
|
|
|
fprintf(out, "format :%s\n", msg_format.c_str());
|
2015-03-17 09:10:34 +03:00
|
|
|
|
|
|
|
if (pretty_print) {
|
2022-09-10 19:28:07 +03:00
|
|
|
data_scanner ds2(sub_line, body.lr_start);
|
2018-05-10 16:44:03 +03:00
|
|
|
pretty_printer pp(&ds2, sa);
|
|
|
|
attr_line_t pretty_out;
|
2015-03-17 09:10:34 +03:00
|
|
|
|
2018-05-10 16:44:03 +03:00
|
|
|
pp.append_to(pretty_out);
|
|
|
|
fprintf(out, "\n--\n%s", pretty_out.get_string().c_str());
|
2015-03-17 09:10:34 +03:00
|
|
|
}
|
2016-04-29 09:34:13 +03:00
|
|
|
|
|
|
|
auto_mem<yajl_gen_t> gen(yajl_gen_free);
|
|
|
|
|
2021-02-26 09:22:16 +03:00
|
|
|
gen = yajl_gen_alloc(nullptr);
|
2016-04-29 09:34:13 +03:00
|
|
|
yajl_gen_config(gen.in(), yajl_gen_beautify, true);
|
|
|
|
|
|
|
|
elements_to_json(gen, dp, &dp.dp_pairs);
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
const unsigned char* buf;
|
2016-04-29 09:34:13 +03:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
yajl_gen_get_buf(gen, &buf, &len);
|
|
|
|
fwrite(buf, 1, len, out);
|
|
|
|
|
2013-06-04 17:53:25 +04:00
|
|
|
fclose(out);
|
|
|
|
|
2023-06-19 07:07:26 +03:00
|
|
|
snprintf(
|
|
|
|
cmd, sizeof(cmd), "diff -u %s %s", argv[lpc], TMP_NAME);
|
2013-06-04 17:53:25 +04:00
|
|
|
rc = system(cmd);
|
|
|
|
if (rc != 0) {
|
|
|
|
if (prompt) {
|
|
|
|
char resp[4];
|
|
|
|
|
2022-03-17 01:38:08 +03:00
|
|
|
printf("\nOriginal line:\n%s\n",
|
|
|
|
sub_line.c_str() + body.lr_start);
|
|
|
|
printf(
|
|
|
|
"Would you like to update the original file? "
|
|
|
|
"(y/N) ");
|
2013-06-04 17:53:25 +04:00
|
|
|
fflush(stdout);
|
2015-04-02 23:44:27 +03:00
|
|
|
log_perror(scanf("%3s", resp));
|
2013-06-05 06:45:04 +04:00
|
|
|
if (strcasecmp(resp, "y") == 0) {
|
2013-06-04 17:53:25 +04:00
|
|
|
rename(TMP_NAME, argv[lpc]);
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2013-06-04 17:53:25 +04:00
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
2022-03-17 01:38:08 +03:00
|
|
|
} else {
|
2013-06-04 17:53:25 +04:00
|
|
|
fprintf(stderr, "error: mismatch\n");
|
2013-06-03 18:45:19 +04:00
|
|
|
retval = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2013-06-08 17:10:18 +04:00
|
|
|
|
|
|
|
fclose(data_parser::TRACE_FILE);
|
2021-02-26 09:22:16 +03:00
|
|
|
data_parser::TRACE_FILE = nullptr;
|
2013-06-03 18:45:19 +04:00
|
|
|
}
|
|
|
|
}
|
2011-06-14 18:21:53 +04:00
|
|
|
}
|
2011-06-19 00:42:07 +04:00
|
|
|
|
|
|
|
return retval;
|
2011-06-13 18:46:03 +04:00
|
|
|
}
|