Skip to content

Commit

Permalink
new(libsinsp): fallback to plugin name when logging component is null
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <[email protected]>
  • Loading branch information
mrgian authored and poiana committed Jan 26, 2024
1 parent 5aca304 commit fd72377
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 25 deletions.
7 changes: 4 additions & 3 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ const char* sinsp_plugin::get_owner_last_error(ss_plugin_owner_t* o)
return t->m_last_owner_err.c_str();
}

static void plugin_log_fn(const char* component, const char* msg, ss_plugin_log_severity sev)
static void plugin_log_fn(ss_plugin_owner_t* o, const char* component, const char* msg, ss_plugin_log_severity sev)
{
std::string prefix = (component == NULL) ? "" : std::string(component) + ": ";
libsinsp_logger()->log(prefix + msg, (sinsp_logger::severity)sev);
auto t = static_cast<sinsp_plugin*>(o);
std::string prefix = (component == NULL) ? t->name() : std::string(component);
libsinsp_logger()->log(prefix + ": " + msg, (sinsp_logger::severity)sev);
}

std::shared_ptr<sinsp_plugin> sinsp_plugin::create(
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,17 +654,19 @@ TEST(sinsp_plugin, plugin_logging)
plugin_api api;
get_plugin_api_sample_plugin_extract(api);

// the plugin is logging with a NULL component, so we expect the component to fallback to the plugin name
api.get_name = [](){ return "plugin_name"; };

libsinsp_logger()->add_callback_log([](std::string&& str, sinsp_logger::severity sev) {
std::string expected = "some component: initializing plugin...";
std::string expected = "plugin_name: initializing plugin...";
ASSERT_TRUE(std::equal(expected.rbegin(), expected.rend(), str.rbegin()));
});

api.get_name = [](){ return "p1"; };
auto p = i.register_plugin(&api);
p->init("", tmp);

libsinsp_logger()->add_callback_log([](std::string&& str, sinsp_logger::severity sev) {
std::string expected = "some component: destroying plugin...";
std::string expected = "plugin_name: destroying plugin...";
ASSERT_TRUE(std::equal(expected.rbegin(), expected.rend(), str.rbegin()));
});
}
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/plugin_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct plugin_state
std::string strstorage;
const char* strptr;
std::vector<uint16_t> event_types;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -95,10 +96,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
{
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

// init config may indicate the comma-separated, event-types to filter
std::string cfg = in->config;
Expand Down Expand Up @@ -129,7 +131,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

delete ((plugin_state *) s);
}
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/plugin_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static constexpr const char* s_evt_data = "hello world";
struct plugin_state
{
std::string lasterr;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -88,10 +89,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
{
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

*rc = SS_PLUGIN_SUCCESS;
return ret;
Expand All @@ -100,7 +102,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

delete ((plugin_state *) s);
}
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/syscall_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct plugin_state
std::atomic<bool> async_thread_run;
uint8_t async_evt_buf[2048];
ss_plugin_event* async_evt;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -84,10 +85,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
*rc = SS_PLUGIN_SUCCESS;
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

ret->async_evt = (ss_plugin_event*) &ret->async_evt_buf;
ret->async_thread_run = false;
Expand All @@ -102,7 +104,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

// stop the async thread if it's running
if (ps->async_thread_run)
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/syscall_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct plugin_state
ss_plugin_table_field_t* thread_opencount_field;
ss_plugin_table_t* evtcount_table;
ss_plugin_table_field_t* evtcount_count_field;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -156,10 +157,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
*rc = SS_PLUGIN_SUCCESS;
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

// we have the extraction capability so the `in->tables` field should be != NULL
if (!in || !in->tables)
Expand Down Expand Up @@ -231,7 +233,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

delete ((plugin_state *) s);
}
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/syscall_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct plugin_state
ss_plugin_table_field_t* thread_opencount_field;
sample_table::ptr_t event_count_table;
ss_plugin_table_field_t* event_count_table_count_field;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -111,10 +112,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
*rc = SS_PLUGIN_SUCCESS;
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

if (!in || !in->tables)
{
Expand Down Expand Up @@ -171,7 +173,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

delete ((plugin_state *) s);
}
Expand Down
8 changes: 5 additions & 3 deletions userspace/libsinsp/test/plugins/syscall_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ limitations under the License.
struct plugin_state
{
std::string lasterr;
ss_plugin_owner_t* owner;
ss_plugin_log_fn_t log;
};

Expand Down Expand Up @@ -76,10 +77,11 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
{
plugin_state *ret = new plugin_state();

//set log function in the state
//save logger and owner in the state
ret->log = in->log_fn;
ret->owner = in->owner;

ret->log("some component", "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);
ret->log(ret->owner, NULL, "initializing plugin...", SS_PLUGIN_LOG_SEV_INFO);

*rc = SS_PLUGIN_SUCCESS;
return ret;
Expand All @@ -88,7 +90,7 @@ static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc
static void plugin_destroy(ss_plugin_t* s)
{
plugin_state *ps = (plugin_state *) s;
ps->log("some component", "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);
ps->log(ps->owner, NULL, "destroying plugin...", SS_PLUGIN_LOG_SEV_INFO);

delete ((plugin_state *) s);
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/plugin/plugin_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ typedef struct
// - msg: message to log
// (it doesn't have to be '\n' terminated)
// - sev: message severity as defined in ss_plugin_log_severity
typedef void (*ss_plugin_log_fn_t)(const char* component, const char* msg, ss_plugin_log_severity sev);
typedef void (*ss_plugin_log_fn_t)(ss_plugin_owner_t* o, const char* component, const char* msg, ss_plugin_log_severity sev);

// Input passed at the plugin through plugin_init(). This contain information
// common to any plugin, and also information useful only in case the plugin
Expand Down

0 comments on commit fd72377

Please sign in to comment.