Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(userspace/libsinsp)!: deprecate gen_event class family #1659

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion userspace/libsinsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ add_library(sinsp
sinsp_filtercheck_user.cpp
sinsp_filtercheck_utils.cpp
filter_check_list.cpp
gen_filter.cpp
ifinfo.cpp
memmem.cpp
logger.cpp
Expand Down
26 changes: 3 additions & 23 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ limitations under the License.
#include <libsinsp/sinsp_public.h>
#include <libsinsp/sinsp_event_source.h>
#include <libscap/scap.h>
#include <libsinsp/gen_filter.h>
#include <libsinsp/settings.h>
#include <libsinsp/sinsp_exception.h>
#include <libsinsp/fdinfo.h>
Expand Down Expand Up @@ -65,19 +64,6 @@ enum filtercheck_field_flags
EPF_DEPRECATED = 1 << 10,///< this field is deprecated.
};

/*!
\brief Information about a filter/formatting field.
*/
struct filtercheck_field_info
{
ppm_param_type m_type; ///< Field type.
uint32_t m_flags; ///< Field flags.
ppm_print_format m_print_format; ///< If this is a numeric field, this flag specifies if it should be rendered as octal, decimal or hex.
char m_name[64]; ///< Field name.
char m_display[64]; ///< Field display name (short description). May be empty.
char m_description[1024]; ///< Field description.
};

/** @defgroup event Event manipulation
* Classes to manipulate events, extract their content and convert them into strings.
* @{
Expand Down Expand Up @@ -179,7 +165,7 @@ inline std::string_view get_event_param_as<std::string_view>(const sinsp_evt_par
events and their parameters, including parsing, formatting and extracting
state like the event process or FD.
*/
class SINSP_PUBLIC sinsp_evt : public gen_event
class SINSP_PUBLIC sinsp_evt
{
public:
/*!
Expand Down Expand Up @@ -269,7 +255,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event

\note For a list of event types, refer to \ref etypes.
*/
inline uint16_t get_type() const override
virtual inline uint16_t get_type() const
{
return m_pevt->type;
}
Expand Down Expand Up @@ -320,7 +306,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event

\return The event timestamp, in nanoseconds from epoch
*/
inline uint64_t get_ts() const override
virtual inline uint64_t get_ts() const
{
return m_pevt->ts;
}
Expand Down Expand Up @@ -451,12 +437,6 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
bool is_filtered_out() const;
scap_dump_flags get_dump_flags(OUT bool* should_drop) const;

// todo(jasondellaluce): this is deprecated and will need to be removed
inline uint16_t get_source() const override
{
return ESRC_SINSP;
}

/*!
\brief Returns true if this event represents a system call error,
false otherwise.
Expand Down
33 changes: 12 additions & 21 deletions userspace/libsinsp/eventformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ sinsp_evt_formatter::sinsp_evt_formatter(sinsp* inspector,
: m_inspector(inspector),
m_available_checks(available_checks)
{
gen_event_formatter::output_format of = gen_event_formatter::OF_NORMAL;
output_format of = sinsp_evt_formatter::OF_NORMAL;

if(m_inspector->get_buffer_format() == sinsp_evt::PF_JSON
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONEOLS
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEX
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONHEXASCII
|| m_inspector->get_buffer_format() == sinsp_evt::PF_JSONBASE64)
{
of = gen_event_formatter::OF_JSON;
of = sinsp_evt_formatter::OF_JSON;
}

set_format(of, fmt);
Expand All @@ -63,7 +63,7 @@ sinsp_evt_formatter::~sinsp_evt_formatter()
}
}

void sinsp_evt_formatter::set_format(gen_event_formatter::output_format of, const std::string& fmt)
void sinsp_evt_formatter::set_format(output_format of, const std::string& fmt)
{
uint32_t j;
uint32_t last_nontoken_str_start = 0;
Expand Down Expand Up @@ -222,10 +222,8 @@ bool sinsp_evt_formatter::resolve_tokens(sinsp_evt *evt, std::map<std::string,st
return retval;
}

bool sinsp_evt_formatter::get_field_values(gen_event *gevt, std::map<std::string, std::string> &fields)
bool sinsp_evt_formatter::get_field_values(sinsp_evt *evt, std::map<std::string, std::string> &fields)
{
sinsp_evt *evt = static_cast<sinsp_evt *>(gevt);

return resolve_tokens(evt, fields);
}

Expand All @@ -242,18 +240,16 @@ void sinsp_evt_formatter::get_field_names(std::vector<std::string> &fields)
}
}

gen_event_formatter::output_format sinsp_evt_formatter::get_output_format()
sinsp_evt_formatter::output_format sinsp_evt_formatter::get_output_format()
{
return m_output_format;
}

bool sinsp_evt_formatter::tostring_withformat(gen_event* gevt, std::string &output, gen_event_formatter::output_format of)
bool sinsp_evt_formatter::tostring_withformat(sinsp_evt* evt, std::string &output, output_format of)
{
bool retval = true;
const filtercheck_field_info* fi;

sinsp_evt *evt = static_cast<sinsp_evt *>(gevt);

uint32_t j = 0;
output.clear();

Expand Down Expand Up @@ -329,31 +325,26 @@ bool sinsp_evt_formatter::tostring_withformat(gen_event* gevt, std::string &outp
return retval;
}

bool sinsp_evt_formatter::tostring(gen_event* gevt, std::string &output)
{
return tostring_withformat(gevt, output, m_output_format);
}

bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT std::string* res)
bool sinsp_evt_formatter::tostring(sinsp_evt* evt, std::string& res)
{
return tostring_withformat(evt, *res, m_output_format);
return tostring_withformat(evt, res, m_output_format);
}

sinsp_evt_formatter_factory::sinsp_evt_formatter_factory(sinsp *inspector, filter_check_list &available_checks)
: m_inspector(inspector),
m_available_checks(available_checks),
m_output_format(gen_event_formatter::OF_NORMAL)
m_output_format(sinsp_evt_formatter::OF_NORMAL)
{
}

void sinsp_evt_formatter_factory::set_output_format(gen_event_formatter::output_format of)
void sinsp_evt_formatter_factory::set_output_format(sinsp_evt_formatter::output_format of)
{
m_formatters.clear();

m_output_format = of;
}

std::shared_ptr<gen_event_formatter> sinsp_evt_formatter_factory::create_formatter(const std::string &format)
std::shared_ptr<sinsp_evt_formatter> sinsp_evt_formatter_factory::create_formatter(const std::string &format)
{
auto it = m_formatters.find(format);

Expand All @@ -362,7 +353,7 @@ std::shared_ptr<gen_event_formatter> sinsp_evt_formatter_factory::create_formatt
return it->second;
}

std::shared_ptr<gen_event_formatter> ret;
std::shared_ptr<sinsp_evt_formatter> ret;

ret.reset(new sinsp_evt_formatter(m_inspector, m_available_checks));

Expand Down
52 changes: 30 additions & 22 deletions userspace/libsinsp/eventformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ limitations under the License.
#include <json/json.h>

#include <libsinsp/filter_check_list.h>
#include <libsinsp/gen_filter.h>

class sinsp_filter_check;
#include <libsinsp/filter.h>

/** @defgroup event Event manipulation
* @{
Expand All @@ -36,9 +34,14 @@ class sinsp_filter_check;
This class can be used to format an event into a string, based on an arbitrary
format.
*/
class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter
class SINSP_PUBLIC sinsp_evt_formatter
{
public:
enum output_format {
OF_NORMAL = 0,
OF_JSON = 1
};

/*!
\brief Constructs a formatter.

Expand All @@ -52,8 +55,6 @@ class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter

sinsp_evt_formatter(sinsp* inspector, const std::string& fmt, filter_check_list &available_checks);

void set_format(gen_event_formatter::output_format of, const std::string& fmt) override;

virtual ~sinsp_evt_formatter();

/*!
Expand All @@ -68,13 +69,15 @@ class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter
*/
bool resolve_tokens(sinsp_evt *evt, std::map<std::string,std::string>& values);

// For compatibility with gen_event_filter_factory
virtual void set_format(output_format of, const std::string& fmt);

// For compatibility with sinsp_filter_factory
// interface. It just calls resolve_tokens().
bool get_field_values(gen_event *evt, std::map<std::string, std::string> &fields) override;
virtual bool get_field_values(sinsp_evt *evt, std::map<std::string, std::string> &fields);

void get_field_names(std::vector<std::string> &fields) override;
virtual void get_field_names(std::vector<std::string> &fields);

gen_event_formatter::output_format get_output_format() override;
virtual output_format get_output_format();

/*!
\brief Fills res with the string rendering of the event.
Expand All @@ -85,12 +88,17 @@ class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter
\return true if the string should be shown (based on the initial *),
false otherwise.
*/
bool tostring(sinsp_evt* evt, OUT std::string* res);

// For compatibility with gen_event_formatter
bool tostring(gen_event* evt, std::string &output) override;

bool tostring_withformat(gen_event* evt, std::string &output, gen_event_formatter::output_format of) override;
inline bool tostring(sinsp_evt* evt, OUT std::string* res)
{
if (!res)
{
return false;
}
return tostring(evt, *res);
}
virtual bool tostring(sinsp_evt* evt, std::string &output);

virtual bool tostring_withformat(sinsp_evt* evt, std::string &output, output_format of);

/*!
\brief Fills res with end of capture string rendering of the event.
Expand All @@ -102,7 +110,7 @@ class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter
bool on_capture_end(OUT std::string* res);

private:
gen_event_formatter::output_format m_output_format;
output_format m_output_format;

// vector of (full string of the token, filtercheck) pairs
// e.g. ("proc.aname[2], ptr to sinsp_filter_check_thread)
Expand All @@ -117,22 +125,22 @@ class SINSP_PUBLIC sinsp_evt_formatter : public gen_event_formatter
Json::FastWriter m_writer;
};

class sinsp_evt_formatter_factory : public gen_event_formatter_factory
class sinsp_evt_formatter_factory
{
public:
sinsp_evt_formatter_factory(sinsp *inspector, filter_check_list &available_checks);
virtual ~sinsp_evt_formatter_factory() = default;

void set_output_format(gen_event_formatter::output_format of) override;
virtual void set_output_format(sinsp_evt_formatter::output_format of);

std::shared_ptr<gen_event_formatter> create_formatter(const std::string &format) override;
virtual std::shared_ptr<sinsp_evt_formatter> create_formatter(const std::string &format);

protected:

// Maps from output string to formatter
std::map<std::string, std::shared_ptr<gen_event_formatter>> m_formatters;
std::map<std::string, std::shared_ptr<sinsp_evt_formatter>> m_formatters;

sinsp *m_inspector;
filter_check_list &m_available_checks;
gen_event_formatter::output_format m_output_format;
sinsp_evt_formatter::output_format m_output_format;
};
Loading
Loading