Skip to content

Commit

Permalink
new(userspace/falco): enable attaching libsinsp logger to the falco one
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
  • Loading branch information
jasondellaluce committed Jun 23, 2022
1 parent 555bf99 commit e188b78
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
66 changes: 66 additions & 0 deletions userspace/falco/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,51 @@ limitations under the License.
int falco_logger::level = LOG_INFO;
bool falco_logger::time_format_iso_8601 = false;

static inline sinsp_logger::severity level_falco_to_sinsp(const int level)
{
switch (level)
{
case LOG_EMERG:
return sinsp_logger::severity::SEV_FATAL;
case LOG_ALERT:
case LOG_CRIT:
return sinsp_logger::severity::SEV_CRITICAL;
case LOG_ERR:
return sinsp_logger::severity::SEV_ERROR;
case LOG_WARNING:
return sinsp_logger::severity::SEV_WARNING;
case LOG_NOTICE:
return sinsp_logger::severity::SEV_NOTICE;
case LOG_INFO:
return sinsp_logger::severity::SEV_INFO;
default:
case LOG_DEBUG:
return sinsp_logger::severity::SEV_DEBUG;
}
}

static inline int level_sinsp_to_falco(const sinsp_logger::severity severity)
{
switch (severity)
{
case sinsp_logger::severity::SEV_FATAL:
return LOG_EMERG;
case sinsp_logger::severity::SEV_CRITICAL:
return LOG_CRIT;
case sinsp_logger::severity::SEV_ERROR:
return LOG_ERR;
case sinsp_logger::severity::SEV_WARNING:
return LOG_WARNING;
case sinsp_logger::severity::SEV_NOTICE:
return LOG_NOTICE;
case sinsp_logger::severity::SEV_INFO:
return LOG_INFO;
default:
case sinsp_logger::severity::SEV_DEBUG:
return LOG_DEBUG;
}
}

void falco_logger::set_time_format_iso_8601(bool val)
{
falco_logger::time_format_iso_8601 = val;
Expand Down Expand Up @@ -66,6 +111,27 @@ void falco_logger::set_level(string &level)
{
throw falco_exception("Unknown log level " + level);
}

// configure the log level in libsinsp too
g_logger.set_severity(level_falco_to_sinsp(falco_logger::level));
}

void falco_logger::set_sinsp_logging(bool enable)
{
if (enable)
{
g_logger.add_callback_log(
[](std::string&& str, const sinsp_logger::severity sev)
{
falco_logger::log(level_sinsp_to_falco(sev), str);
});
g_logger.disable_timestamps();
g_logger.set_severity(level_falco_to_sinsp(falco_logger::level));
}
else
{
g_logger.remove_callback_log();
}
}


Expand Down
2 changes: 2 additions & 0 deletions userspace/falco/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class falco_logger
// Will throw exception if level is unknown.
static void set_level(string &level);

static void set_sinsp_logging(bool enable);

static void log(int priority, const string msg);

static int level;
Expand Down

0 comments on commit e188b78

Please sign in to comment.