diff --git a/userspace/engine/formats.cpp b/userspace/engine/formats.cpp index 008c4ee8..21779048 100644 --- a/userspace/engine/formats.cpp +++ b/userspace/engine/formats.cpp @@ -36,6 +36,7 @@ const static struct luaL_reg ll_falco [] = {"free_formatter", &falco_formats::free_formatter}, {"free_formatters", &falco_formats::free_formatters}, {"format_event", &falco_formats::format_event}, + {"resolve_tokens", &falco_formats::resolve_tokens}, {NULL,NULL} }; @@ -265,3 +266,34 @@ int falco_formats::format_event (lua_State *ls) return 1; } + + +int falco_formats::resolve_tokens(lua_State *ls) +{ + if (!lua_isstring(ls, -1) || + !lua_isstring(ls, -2)) { + lua_pushstring(ls, "Invalid arguments passed to resolve_tokens()"); + lua_error(ls); + } + gen_event* evt = (gen_event*)lua_topointer(ls, 1); + const char *source = (char *) lua_tostring(ls, 2); // TODO(fntlnz, leodido): do we need this one? + const char *format = (char *) lua_tostring(ls, 3); + string sformat = format; + + + map values; + + s_formatters->resolve_tokens((sinsp_evt *)evt, sformat, values); + + lua_newtable(ls); + int top = lua_gettop(ls); + for (std::map::iterator it = values.begin(); it != values.end(); ++it) { + const char* key = it->first.c_str(); + const char* value = it->second.c_str(); + lua_pushlstring(ls, key, it->first.size()); + lua_pushlstring(ls, value, it->second.size()); + lua_settable(ls, top); + } + + return 1; +} diff --git a/userspace/engine/formats.h b/userspace/engine/formats.h index 2e918a5f..955737ac 100644 --- a/userspace/engine/formats.h +++ b/userspace/engine/formats.h @@ -53,6 +53,8 @@ class falco_formats // formatted_string = falco.format_event(evt, formatter) static int format_event(lua_State *ls); + static int resolve_tokens(lua_State *ls); + static sinsp* s_inspector; static falco_engine *s_engine; static sinsp_evt_formatter_cache *s_formatters;