Files
falco/userspace/digwatch/formats.cpp
2016-04-22 16:01:00 -07:00

58 lines
1.3 KiB
C++

#include "formats.h"
#include "logger.h"
sinsp* digwatch_formats::s_inspector = NULL;
const static struct luaL_reg ll_digwatch [] =
{
{"formatter", &digwatch_formats::formatter},
{"format_event", &digwatch_formats::format_event},
{NULL,NULL}
};
void digwatch_formats::init(sinsp* inspector, lua_State *ls)
{
s_inspector = inspector;
luaL_openlib(ls, "digwatch", ll_digwatch, 0);
}
int digwatch_formats::formatter(lua_State *ls)
{
string format = luaL_checkstring(ls, 1);
sinsp_evt_formatter* formatter;
try
{
formatter = new sinsp_evt_formatter(s_inspector, format);
}
catch(sinsp_exception& e)
{
digwatch_logger::log(LOG_ERR, "Invalid output format '" + format + "'.\n");
throw sinsp_exception("set_formatter error");
}
lua_pushlightuserdata(ls, formatter);
return 1;
}
int digwatch_formats::format_event (lua_State *ls)
{
string line;
if (!lua_islightuserdata(ls, -1) || !lua_islightuserdata(ls, -2)) {
digwatch_logger::log(LOG_ERR, "Invalid arguments passed to format_event()\n");
throw sinsp_exception("format_event error");
}
sinsp_evt* evt = (sinsp_evt*)lua_topointer(ls, 1);
sinsp_evt_formatter* formatter = (sinsp_evt_formatter*)lua_topointer(ls, 2);
formatter->tostring(evt, &line);
lua_pushstring(ls, line.c_str());
return 1;
}