Skip to content

Commit

Permalink
Use filter_fieldclass_info::as_string to print fields
Browse files Browse the repository at this point in the history
list_fields() is used by programs that use the falcosecurity libs but
are not falco, with its strong notion of event types.

Replace the old version, which used printf and character-by-character
iteration over line strings, with using
sinsp_filter_factory::check_infos_to_fieldclass_infos to get a generic
representation of field classes, and
filter_fieldclass_info::as_string to format the field class info as a
string.

markdown output is now in a dedicated (static) function list_fields_markdown().

This allows for a consistent display of field info across programs.

Signed-off-by: Mark Stemm <[email protected]>
  • Loading branch information
mstemm committed Dec 9, 2021
1 parent e9d4334 commit 345aa4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 98 deletions.
122 changes: 25 additions & 97 deletions userspace/libsinsp/fields_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
//
// Various helper functions to render stuff on the screen
//
#define __STDC_FORMAT_MACROS
#include <stdio.h>
#include <iostream>
#include <assert.h>
Expand All @@ -27,111 +26,40 @@ limitations under the License.
#include <sinsp.h>
#include "fields_info.h"

// Must match the value in the zsh tab completion
#define DESCRIPTION_TEXT_START 16


#define CONSOLE_LINE_LEN 79
#define PRINTF_WRAP_CPROC(x) #x
#define PRINTF_WRAP(x) PRINTF_WRAP_CPROC(x)

void list_fields(bool verbose, bool markdown, bool names_only)
static void list_fields_markdown(std::list<gen_event_filter_factory::filter_fieldclass_info> &fld_classes)
{
uint32_t j, l, m;
int32_t k;
vector<const filter_check_info*> fc_plugins;
sinsp::get_filtercheck_fields_info(fc_plugins);

for(j = 0; j < fc_plugins.size(); j++)
for(auto &fld_class : fld_classes)
{
const filter_check_info* fci = fc_plugins[j];
printf("\n## Field Class: %s\n\n", fld_class.name.c_str());
printf("%s\n\n", fld_class.desc.c_str());
printf("Name | Type | Description\n");
printf(":----|:-----|:-----------\n");

if(fci->m_flags & filter_check_info::FL_HIDDEN)
for(auto &fld_info : fld_class.fields)
{
continue;
}

if(!names_only)
{
if(markdown)
{
printf("\n## Field Class: %s\n\n", fci->m_name.c_str());
printf("%s\n\n", fci->m_desc.c_str());
printf("Name | Type | Description\n");
printf(":----|:-----|:-----------\n");
}
else
{
printf("\n----------------------\n");
printf("Field Class: %s\n\n", fci->m_name.c_str());
printf("%s\n\n", fci->m_desc.c_str());
}
printf("`%s` | %s | %s\n", fld_info.name.c_str(), fld_info.data_type.c_str(), fld_info.desc.c_str());
}
}
}

for(k = 0; k < fci->m_nfields; k++)
{
const filtercheck_field_info* fld = &fci->m_fields[k];

if(fld->m_flags & EPF_TABLE_ONLY)
{
continue;
}

if(names_only)
{
printf("%s\n", fld->m_name);
}
else if(markdown)
{
printf("`%s` | %s | %s\n", fld->m_name, param_type_to_string(fld->m_type), fld->m_description);
}
else
{
printf("%s", fld->m_name);
uint32_t namelen = (uint32_t)strlen(fld->m_name);

if(namelen >= DESCRIPTION_TEXT_START)
{
printf("\n");
namelen = 0;
}

for(l = 0; l < DESCRIPTION_TEXT_START - namelen; l++)
{
printf(" ");
}

string desc(fld->m_description);

if(fld->m_flags & EPF_FILTER_ONLY)
{
desc = "(FILTER ONLY) " + desc;
}

if(verbose)
{
desc += string(" Type:") + param_type_to_string(fld->m_type) + ".";
}

size_t desclen = desc.size();

for(l = 0; l < desclen; l++)
{
if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0)
{
printf("\n");
void list_fields(bool verbose, bool markdown)
{
vector<const filter_check_info*> fc_plugins;
std::list<gen_event_filter_factory::filter_fieldclass_info> fld_classes;

for(m = 0; m < DESCRIPTION_TEXT_START; m++)
{
printf(" ");
}
}
sinsp::get_filtercheck_fields_info(fc_plugins);

printf("%c", desc[l]);
}
fld_classes = sinsp_filter_factory::check_infos_to_fieldclass_infos(fc_plugins);

printf("\n");
}
if(markdown)
{
list_fields_markdown(fld_classes);
}
else
{
for(auto &fld_class : fld_classes)
{
printf("%s\n", fld_class.as_string(verbose).c_str());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/fields_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ class sinsp;
//
// Printer functions
//
void list_fields(bool verbose, bool markdown, bool names_only=false);
void list_fields(bool verbose, bool markdown=false);
void list_events(sinsp* inspector, bool markdown=false);

0 comments on commit 345aa4e

Please sign in to comment.