mirror of https://github.com/tstack/lnav
[cleanup] remove unused stuff and add a test
parent
317f2d32cf
commit
b3ee092dc5
@ -1,210 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @file papertrail_proc.cc
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef HAVE_LIBCURL
|
||||
# include <curl/curl.h>
|
||||
|
||||
# include "papertrail_proc.hh"
|
||||
# include "yajl/api/yajl_parse.h"
|
||||
# include "yajlpp/yajlpp_def.hh"
|
||||
|
||||
const char* papertrail_proc::PT_SEARCH_URL
|
||||
= "https://papertrailapp.com/api/v1/events/search.json?min_id=%s&";
|
||||
|
||||
static int
|
||||
read_max_id(yajlpp_parse_context* ypc, const unsigned char* str, size_t len)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
ptp->ptp_last_max_id = std::string((const char*) str, len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
read_partial(yajlpp_parse_context* ypc, int val)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
if (val) {
|
||||
ptp->ptp_partial_read = true;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
read_limit(yajlpp_parse_context* ypc, int val)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
if (val) {
|
||||
ptp->ptp_partial_read = true;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
ignore_bool(yajlpp_parse_context* ypc, int val)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
ignore_str(yajlpp_parse_context* ypc, const unsigned char* str, size_t len)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
read_event_int(yajlpp_parse_context* ypc, long long val)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
yajl_gen_string(ptp->ptp_gen, ypc->get_path_fragment(2));
|
||||
yajl_gen_integer(ptp->ptp_gen, val);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
read_event_field(yajlpp_parse_context* ypc,
|
||||
const unsigned char* str,
|
||||
size_t len)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
yajl_gen_string(ptp->ptp_gen, ypc->get_path_fragment(2));
|
||||
yajl_gen_string(ptp->ptp_gen, str, len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
papertrail_proc::json_map_start(void* ctx)
|
||||
{
|
||||
yajlpp_parse_context* ypc = (yajlpp_parse_context*) ctx;
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
if (ypc->ypc_path_index_stack.size() == 3) {
|
||||
yajl_gen_map_open(ptp->ptp_gen);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
papertrail_proc::json_map_end(void* ctx)
|
||||
{
|
||||
yajlpp_parse_context* ypc = (yajlpp_parse_context*) ctx;
|
||||
papertrail_proc* ptp = (papertrail_proc*) ypc->ypc_userdata;
|
||||
|
||||
if (ypc->ypc_path_index_stack.size() == 2) {
|
||||
yajl_gen_map_close(ptp->ptp_gen);
|
||||
yajl_gen_reset(ptp->ptp_gen, "\n");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct json_path_container papertrail_proc::FORMAT_HANDLERS
|
||||
= json_path_container{
|
||||
json_path_handler("/max_id", read_max_id),
|
||||
json_path_handler("/(partial_results)", read_partial),
|
||||
json_path_handler("/(reached_record_limit|reached_time_limit)",
|
||||
read_limit),
|
||||
json_path_handler("/(min_id|min_time_at|max_time_at|"
|
||||
"reached_beginning|reached_end|tail|no_events)")
|
||||
.add_cb(ignore_bool)
|
||||
.add_cb(ignore_str),
|
||||
json_path_handler("/events#/\\w+")
|
||||
.add_cb(read_event_field)
|
||||
.add_cb(read_event_int)};
|
||||
|
||||
size_t
|
||||
papertrail_proc::write_cb(void* contents,
|
||||
size_t size,
|
||||
size_t nmemb,
|
||||
void* userp)
|
||||
{
|
||||
yajl_handle handle = (yajl_handle) userp;
|
||||
size_t realsize = size * nmemb;
|
||||
|
||||
if (yajl_parse(handle, (const unsigned char*) contents, realsize)
|
||||
!= yajl_status_ok)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return realsize;
|
||||
}
|
||||
|
||||
void
|
||||
papertrail_proc::yajl_writer(void* context, const char* str, size_t len)
|
||||
{
|
||||
papertrail_proc* ptp = (papertrail_proc*) context;
|
||||
|
||||
write(ptp->ptp_fd, str, len);
|
||||
}
|
||||
|
||||
long
|
||||
papertrail_proc::complete(CURLcode result)
|
||||
{
|
||||
curl_request::complete(result);
|
||||
|
||||
yajl_reset(this->ptp_jhandle.in());
|
||||
|
||||
if (result != CURLE_OK) {
|
||||
static const char* err_msg = "Unable to execute papertrail search -- ";
|
||||
|
||||
write(this->ptp_fd, err_msg, strlen(err_msg));
|
||||
write(
|
||||
this->ptp_fd, this->cr_error_buffer, strlen(this->cr_error_buffer));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (this->ptp_max_time) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->set_url();
|
||||
if (this->ptp_partial_read) {
|
||||
this->ptp_partial_read = false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 3000;
|
||||
}
|
||||
|
||||
#endif
|
@ -1,179 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015, Timothy Stack
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @file papertrail_proc.hh
|
||||
*/
|
||||
|
||||
#ifndef LNAV_PAPERTRAIL_PROC_HH
|
||||
#define LNAV_PAPERTRAIL_PROC_HH
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef HAVE_LIBCURL
|
||||
|
||||
# include <memory>
|
||||
# include <string>
|
||||
|
||||
# include <fcntl.h>
|
||||
# include <paths.h>
|
||||
# include <signal.h>
|
||||
# include <sys/wait.h>
|
||||
# include <unistd.h>
|
||||
|
||||
# include "base/auto_fd.hh"
|
||||
# include "base/auto_mem.hh"
|
||||
# include "curl_looper.hh"
|
||||
# include "line_buffer.hh"
|
||||
# include "yajlpp/yajlpp.hh"
|
||||
|
||||
class papertrail_proc : public curl_request {
|
||||
public:
|
||||
papertrail_proc(const std::string& search, time_t min_time, time_t max_time)
|
||||
: curl_request("papertrailapp.com"),
|
||||
ptp_jcontext(intern_string::lookup(this->cr_name), &FORMAT_HANDLERS),
|
||||
ptp_search(search), ptp_min_time(min_time), ptp_max_time(max_time)
|
||||
{
|
||||
char piper_tmpname[PATH_MAX];
|
||||
const char* tmpdir;
|
||||
|
||||
if ((tmpdir = getenv("TMPDIR")) == NULL) {
|
||||
tmpdir = _PATH_VARTMP;
|
||||
}
|
||||
snprintf(
|
||||
piper_tmpname, sizeof(piper_tmpname), "%s/lnav.pt.XXXXXX", tmpdir);
|
||||
if ((this->ptp_fd = mkstemp(piper_tmpname)) == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
unlink(piper_tmpname);
|
||||
|
||||
this->ptp_jcontext.ypc_alt_callbacks.yajl_start_map = json_map_start;
|
||||
this->ptp_jcontext.ypc_alt_callbacks.yajl_end_map = json_map_end;
|
||||
this->ptp_jcontext.ypc_userdata = this;
|
||||
this->ptp_jhandle = yajl_alloc(
|
||||
&this->ptp_jcontext.ypc_callbacks, NULL, &this->ptp_jcontext);
|
||||
|
||||
this->ptp_gen = yajl_gen_alloc(NULL);
|
||||
yajl_gen_config(
|
||||
this->ptp_gen, yajl_gen_print_callback, yajl_writer, this);
|
||||
|
||||
curl_easy_setopt(this->cr_handle, CURLOPT_WRITEFUNCTION, write_cb);
|
||||
curl_easy_setopt(
|
||||
this->cr_handle, CURLOPT_WRITEDATA, this->ptp_jhandle.in());
|
||||
curl_easy_setopt(this->cr_handle, CURLOPT_FAILONERROR, 1L);
|
||||
|
||||
this->ptp_api_key = getenv("PAPERTRAIL_API_TOKEN");
|
||||
|
||||
if (this->ptp_api_key == NULL) {
|
||||
this->ptp_error
|
||||
= "papertrail search requested, but PAPERTRAIL_API_TOKEN is "
|
||||
"not set";
|
||||
}
|
||||
|
||||
this->ptp_quoted_search = curl_easy_escape(
|
||||
this->cr_handle, this->ptp_search.c_str(), this->ptp_search.size());
|
||||
|
||||
log_perror(asprintf(this->ptp_token_header.out(),
|
||||
"X-Papertrail-Token: %s",
|
||||
this->ptp_api_key));
|
||||
this->ptp_header_list = curl_slist_append(this->ptp_header_list,
|
||||
this->ptp_token_header.in());
|
||||
|
||||
curl_easy_setopt(
|
||||
this->cr_handle, CURLOPT_HTTPHEADER, this->ptp_header_list.in());
|
||||
|
||||
this->set_url();
|
||||
}
|
||||
|
||||
~papertrail_proc() {}
|
||||
|
||||
auto_fd copy_fd() const { return this->ptp_fd.dup(); }
|
||||
|
||||
long complete(CURLcode result);
|
||||
|
||||
void set_url()
|
||||
{
|
||||
char base_url[1024];
|
||||
|
||||
snprintf(base_url,
|
||||
sizeof(base_url),
|
||||
PT_SEARCH_URL,
|
||||
this->ptp_last_max_id.c_str());
|
||||
if (this->ptp_min_time) {
|
||||
size_t base_len = strlen(base_url);
|
||||
snprintf(&base_url[base_len],
|
||||
sizeof(base_url) - base_len,
|
||||
"min_time=%ld&",
|
||||
this->ptp_min_time);
|
||||
}
|
||||
if (this->ptp_max_time) {
|
||||
size_t base_len = strlen(base_url);
|
||||
snprintf(&base_url[base_len],
|
||||
sizeof(base_url) - base_len,
|
||||
"max_time=%ld&",
|
||||
this->ptp_max_time);
|
||||
}
|
||||
log_perror(asprintf(this->ptp_url.out(),
|
||||
"%sq=%s",
|
||||
base_url,
|
||||
this->ptp_quoted_search.in()));
|
||||
curl_easy_setopt(this->cr_handle, CURLOPT_URL, this->ptp_url.in());
|
||||
}
|
||||
|
||||
static size_t write_cb(void* contents,
|
||||
size_t size,
|
||||
size_t nmemb,
|
||||
void* userp);
|
||||
|
||||
static int json_map_start(void* ctx);
|
||||
static int json_map_end(void* ctx);
|
||||
|
||||
static void yajl_writer(void* context, const char* str, size_t len);
|
||||
static struct json_path_container FORMAT_HANDLERS;
|
||||
static const char* PT_SEARCH_URL;
|
||||
|
||||
yajlpp_parse_context ptp_jcontext;
|
||||
auto_mem<yajl_handle_t> ptp_jhandle{yajl_free};
|
||||
auto_mem<yajl_gen_t> ptp_gen{yajl_gen_free};
|
||||
const char* ptp_api_key;
|
||||
const std::string ptp_search;
|
||||
auto_mem<const char> ptp_quoted_search{curl_free};
|
||||
auto_mem<char> ptp_url;
|
||||
auto_mem<char> ptp_token_header;
|
||||
auto_mem<struct curl_slist> ptp_header_list{curl_slist_free_all};
|
||||
auto_fd ptp_fd;
|
||||
std::string ptp_last_max_id;
|
||||
bool ptp_partial_read{false};
|
||||
std::string ptp_error;
|
||||
time_t ptp_min_time;
|
||||
time_t ptp_max_time;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif // LNAV_PAPERTRAIL_PROC_HH
|
@ -0,0 +1,11 @@
|
||||
view_name filter_id enabled type language pattern
|
||||
log 1 1 out regex blah
|
||||
name search
|
||||
log foobar
|
||||
text
|
||||
help
|
||||
histogram
|
||||
db
|
||||
schema
|
||||
pretty
|
||||
spectro
|
Loading…
Reference in New Issue