2020-11-17 18:04:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2020, 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;
|
2022-03-16 22:38:08 +00: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
|
2020-11-17 18:04:23 +00: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.
|
|
|
|
*
|
|
|
|
* @file log_format_fwd.hh
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lnav_log_format_fwd_hh
|
|
|
|
#define lnav_log_format_fwd_hh
|
|
|
|
|
2022-09-10 16:28:07 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-11-17 18:04:23 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2022-06-29 05:23:56 +00:00
|
|
|
#include "ArenaAlloc/arenaalloc.h"
|
2022-05-11 04:58:32 +00:00
|
|
|
#include "base/file_range.hh"
|
2022-04-12 23:07:13 +00:00
|
|
|
#include "base/string_attr_type.hh"
|
2020-11-17 18:04:23 +00:00
|
|
|
#include "byte_array.hh"
|
|
|
|
#include "log_level.hh"
|
2022-09-10 16:28:07 +00:00
|
|
|
#include "pcrepp/pcre2pp.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "ptimec.hh"
|
2022-07-07 17:05:06 +00:00
|
|
|
#include "robin_hood/robin_hood.h"
|
2022-09-10 16:28:07 +00:00
|
|
|
#include "yajlpp/yajlpp.hh"
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
class log_format;
|
|
|
|
|
2022-06-11 04:17:02 +00:00
|
|
|
struct opid_time_range {
|
|
|
|
struct timeval otr_begin;
|
|
|
|
struct timeval otr_end;
|
|
|
|
};
|
|
|
|
|
2022-07-07 17:05:06 +00:00
|
|
|
using log_opid_map = robin_hood::unordered_map<string_fragment,
|
|
|
|
opid_time_range,
|
|
|
|
frag_hasher,
|
|
|
|
std::equal_to<string_fragment>>;
|
2022-06-11 04:17:02 +00:00
|
|
|
|
|
|
|
struct scan_batch_context {
|
2022-06-29 05:23:56 +00:00
|
|
|
ArenaAlloc::Alloc<char>& sbc_allocator;
|
2022-06-11 04:17:02 +00:00
|
|
|
log_opid_map sbc_opids;
|
2022-07-07 17:05:06 +00:00
|
|
|
std::string sbc_cached_level_strings[4];
|
|
|
|
log_level_t sbc_cached_level_values[4];
|
|
|
|
size_t sbc_cached_level_count{0};
|
2022-06-11 04:17:02 +00:00
|
|
|
};
|
|
|
|
|
2020-11-17 18:04:23 +00:00
|
|
|
/**
|
|
|
|
* Metadata for a single line in a log file.
|
|
|
|
*/
|
|
|
|
class logline {
|
|
|
|
public:
|
2022-04-01 06:21:59 +00:00
|
|
|
static string_attr_type<void> L_PREFIX;
|
|
|
|
static string_attr_type<void> L_TIMESTAMP;
|
|
|
|
static string_attr_type<std::shared_ptr<logfile>> L_FILE;
|
|
|
|
static string_attr_type<bookmark_metadata*> L_PARTITION;
|
|
|
|
static string_attr_type<void> L_MODULE;
|
|
|
|
static string_attr_type<void> L_OPID;
|
|
|
|
static string_attr_type<bookmark_metadata*> L_META;
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a logline object with the given values.
|
|
|
|
*
|
|
|
|
* @param off The offset of the line in the file.
|
|
|
|
* @param t The timestamp for the line.
|
|
|
|
* @param millis The millisecond timestamp for the line.
|
|
|
|
* @param l The logging level.
|
|
|
|
*/
|
2021-02-13 20:41:48 +00:00
|
|
|
logline(file_off_t off,
|
2020-11-17 18:04:23 +00:00
|
|
|
time_t t,
|
|
|
|
uint16_t millis,
|
|
|
|
log_level_t l,
|
|
|
|
uint8_t mod = 0,
|
|
|
|
uint8_t opid = 0)
|
2022-03-16 22:38:08 +00:00
|
|
|
: ll_offset(off), ll_time(t), ll_millis(millis), ll_opid(opid),
|
|
|
|
ll_sub_offset(0), ll_valid_utf(1), ll_level(l), ll_module_id(mod),
|
2021-05-14 05:00:26 +00:00
|
|
|
ll_expr_mark(0)
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
|
|
|
memset(this->ll_schema, 0, sizeof(this->ll_schema));
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2021-02-13 20:41:48 +00:00
|
|
|
logline(file_off_t off,
|
2022-03-16 22:38:08 +00:00
|
|
|
const struct timeval& tv,
|
2020-11-17 18:04:23 +00:00
|
|
|
log_level_t l,
|
|
|
|
uint8_t mod = 0,
|
|
|
|
uint8_t opid = 0)
|
2022-03-16 22:38:08 +00:00
|
|
|
: ll_offset(off), ll_opid(opid), ll_sub_offset(0), ll_valid_utf(1),
|
|
|
|
ll_level(l), ll_module_id(mod), ll_expr_mark(0)
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
|
|
|
this->set_time(tv);
|
|
|
|
memset(this->ll_schema, 0, sizeof(this->ll_schema));
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/** @return The offset of the line in the file. */
|
2022-04-30 20:05:42 +00:00
|
|
|
file_off_t get_offset() const { return this->ll_offset; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
uint16_t get_sub_offset() const { return this->ll_sub_offset; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_sub_offset(uint16_t suboff) { this->ll_sub_offset = suboff; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/** @return The timestamp for the line. */
|
2022-04-30 20:05:42 +00:00
|
|
|
time_t get_time() const { return this->ll_time; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void to_exttm(struct exttm& tm_out) const
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
tm_out.et_tm = *gmtime(&this->ll_time);
|
|
|
|
tm_out.et_nsec = this->ll_millis * 1000 * 1000;
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_time(time_t t) { this->ll_time = t; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/** @return The millisecond timestamp for the line. */
|
2022-04-30 20:05:42 +00:00
|
|
|
uint16_t get_millis() const { return this->ll_millis; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_millis(uint16_t m) { this->ll_millis = m; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
uint64_t get_time_in_millis() const
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
return (this->ll_time * 1000ULL + (uint64_t) this->ll_millis);
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
struct timeval get_timeval() const
|
|
|
|
{
|
|
|
|
struct timeval retval = {this->ll_time, this->ll_millis * 1000};
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
return retval;
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_time(const struct timeval& tv)
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
this->ll_time = tv.tv_sec;
|
|
|
|
this->ll_millis = tv.tv_usec / 1000;
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_ignore(bool val)
|
|
|
|
{
|
2020-12-06 05:51:46 +00:00
|
|
|
if (val) {
|
|
|
|
this->ll_level |= LEVEL_IGNORE;
|
2022-03-16 22:38:08 +00:00
|
|
|
} else {
|
2020-12-06 05:51:46 +00:00
|
|
|
this->ll_level &= ~LEVEL_IGNORE;
|
|
|
|
}
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-12-06 05:51:46 +00:00
|
|
|
|
2022-08-20 03:01:25 +00:00
|
|
|
bool is_ignored() const { return this->ll_level & LEVEL_IGNORE; }
|
2020-12-06 05:51:46 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_mark(bool val)
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
if (val) {
|
|
|
|
this->ll_level |= LEVEL_MARK;
|
2022-03-16 22:38:08 +00:00
|
|
|
} else {
|
2020-11-17 18:04:23 +00:00
|
|
|
this->ll_level &= ~LEVEL_MARK;
|
|
|
|
}
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool is_marked() const { return this->ll_level & LEVEL_MARK; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_expr_mark(bool val) { this->ll_expr_mark = val; }
|
2021-05-14 05:00:26 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool is_expr_marked() const { return this->ll_expr_mark; }
|
2021-05-14 05:00:26 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_time_skew(bool val)
|
|
|
|
{
|
2020-11-17 18:04:23 +00:00
|
|
|
if (val) {
|
|
|
|
this->ll_level |= LEVEL_TIME_SKEW;
|
2022-03-16 22:38:08 +00:00
|
|
|
} else {
|
2020-11-17 18:04:23 +00:00
|
|
|
this->ll_level &= ~LEVEL_TIME_SKEW;
|
|
|
|
}
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool is_time_skewed() const { return this->ll_level & LEVEL_TIME_SKEW; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_valid_utf(bool v) { this->ll_valid_utf = v; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool is_valid_utf() const { return this->ll_valid_utf; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/** @param l The logging level. */
|
2022-08-20 03:01:25 +00:00
|
|
|
void set_level(log_level_t l) { this->ll_level = l; };
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/** @return The logging level. */
|
2022-03-16 22:38:08 +00:00
|
|
|
log_level_t get_level_and_flags() const
|
|
|
|
{
|
|
|
|
return (log_level_t) this->ll_level;
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
log_level_t get_msg_level() const
|
|
|
|
{
|
|
|
|
return (log_level_t) (this->ll_level & ~LEVEL__FLAGS);
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* get_level_name() const
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
|
|
|
return level_names[this->ll_level & ~LEVEL__FLAGS];
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool is_message() const
|
|
|
|
{
|
|
|
|
return (this->ll_level & (LEVEL_IGNORE | LEVEL_CONTINUED)) == 0;
|
2020-12-06 05:51:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool is_continued() const { return this->ll_level & LEVEL_CONTINUED; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
uint8_t get_module_id() const { return this->ll_module_id; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
void set_opid(uint8_t opid) { this->ll_opid = opid; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
uint8_t get_opid() const { return this->ll_opid; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if there is a schema value set for this log line.
|
|
|
|
*/
|
|
|
|
bool has_schema() const
|
|
|
|
{
|
2022-03-16 22:38:08 +00:00
|
|
|
return (this->ll_schema[0] != 0 || this->ll_schema[1] != 0);
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the "schema" for this log line. The schema ID is used to match log
|
|
|
|
* lines that have a similar format when generating the logline table. The
|
|
|
|
* schema is set lazily so that startup is faster.
|
|
|
|
*
|
|
|
|
* @param ba The SHA-1 hash of the constant parts of this log line.
|
|
|
|
*/
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_schema(const byte_array<2, uint64_t>& ba)
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
|
|
|
memcpy(this->ll_schema, ba.in(), sizeof(this->ll_schema));
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
char get_schema() const { return this->ll_schema[0]; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a partial match of the given schema against this log line.
|
|
|
|
* Storing the full schema is not practical, so we just keep the first four
|
|
|
|
* bytes.
|
|
|
|
*
|
|
|
|
* @param ba The SHA-1 hash of the constant parts of a log line.
|
|
|
|
* @return True if the first four bytes of the given schema match the
|
|
|
|
* schema stored in this log line.
|
|
|
|
*/
|
2022-03-16 22:38:08 +00:00
|
|
|
bool match_schema(const byte_array<2, uint64_t>& ba) const
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
|
|
|
return memcmp(this->ll_schema, ba.in(), sizeof(this->ll_schema)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare loglines based on their timestamp.
|
|
|
|
*/
|
2022-03-16 22:38:08 +00:00
|
|
|
bool operator<(const logline& rhs) const
|
2020-11-17 18:04:23 +00:00
|
|
|
{
|
2022-03-16 22:38:08 +00:00
|
|
|
return (this->ll_time < rhs.ll_time)
|
|
|
|
|| (this->ll_time == rhs.ll_time && this->ll_millis < rhs.ll_millis)
|
|
|
|
|| (this->ll_time == rhs.ll_time && this->ll_millis == rhs.ll_millis
|
|
|
|
&& this->ll_offset < rhs.ll_offset)
|
|
|
|
|| (this->ll_time == rhs.ll_time && this->ll_millis == rhs.ll_millis
|
|
|
|
&& this->ll_offset == rhs.ll_offset
|
|
|
|
&& this->ll_sub_offset < rhs.ll_sub_offset);
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
bool operator<(const time_t& rhs) const { return this->ll_time < rhs; }
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool operator<(const struct timeval& rhs) const
|
|
|
|
{
|
|
|
|
return ((this->ll_time < rhs.tv_sec)
|
|
|
|
|| ((this->ll_time == rhs.tv_sec)
|
|
|
|
&& (this->ll_millis < (rhs.tv_usec / 1000))));
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2020-11-17 18:04:23 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool operator<=(const struct timeval& rhs) const
|
|
|
|
{
|
|
|
|
return ((this->ll_time < rhs.tv_sec)
|
|
|
|
|| ((this->ll_time == rhs.tv_sec)
|
|
|
|
&& (this->ll_millis <= (rhs.tv_usec / 1000))));
|
2022-04-30 20:05:42 +00:00
|
|
|
}
|
2022-03-16 22:38:08 +00:00
|
|
|
|
2020-11-17 18:04:23 +00:00
|
|
|
private:
|
2021-02-13 20:41:48 +00:00
|
|
|
file_off_t ll_offset;
|
|
|
|
time_t ll_time;
|
2020-11-17 18:04:23 +00:00
|
|
|
unsigned int ll_millis : 10;
|
|
|
|
unsigned int ll_opid : 6;
|
|
|
|
unsigned int ll_sub_offset : 15;
|
|
|
|
unsigned int ll_valid_utf : 1;
|
2022-03-16 22:38:08 +00:00
|
|
|
uint8_t ll_level;
|
|
|
|
uint8_t ll_module_id : 7;
|
|
|
|
uint8_t ll_expr_mark : 1;
|
|
|
|
char ll_schema[2];
|
2020-11-17 18:04:23 +00:00
|
|
|
};
|
|
|
|
|
2022-08-20 03:01:25 +00:00
|
|
|
struct format_tag_def {
|
2022-09-10 16:28:07 +00:00
|
|
|
explicit format_tag_def(std::string name) : ftd_name(std::move(name)) {}
|
2022-08-20 03:01:25 +00:00
|
|
|
|
|
|
|
struct path_restriction {
|
|
|
|
std::string p_glob;
|
|
|
|
|
|
|
|
bool matches(const char* fn) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string ftd_name;
|
2022-08-20 13:41:44 +00:00
|
|
|
std::string ftd_description;
|
2022-08-20 03:01:25 +00:00
|
|
|
std::vector<path_restriction> ftd_paths;
|
2022-09-10 16:28:07 +00:00
|
|
|
factory_container<lnav::pcre2pp::code, int>::with_default_args<PCRE2_DOTALL>
|
|
|
|
ftd_pattern;
|
2022-08-20 13:41:44 +00:00
|
|
|
log_level_t ftd_level{LEVEL_UNKNOWN};
|
2022-08-20 03:01:25 +00:00
|
|
|
};
|
|
|
|
|
2020-11-17 18:04:23 +00:00
|
|
|
#endif
|