예제 #1
0
lsb_const_string read_message(lua_State *lua, int idx,
                              const lsb_heka_message *m)
{
  lsb_const_string ret = { NULL, 0 };
  size_t field_len;
  const char *field = luaL_checklstring(lua, idx, &field_len);
  int fi = (int)luaL_optinteger(lua, idx + 1, 0);
  luaL_argcheck(lua, fi >= 0, idx + 1, "field index must be >= 0");
  int ai = (int)luaL_optinteger(lua, idx + 2, 0);
  luaL_argcheck(lua, ai >= 0, idx + 2, "array index must be >= 0");

  if (strcmp(field, LSB_PAYLOAD) == 0) {
    if (m->payload.s) ret = m->payload;
  } else {
    if (field_len >= 8
        && memcmp(field, LSB_FIELDS "[", 7) == 0
        && field[field_len - 1] == ']') {
      lsb_read_value v;
      lsb_const_string f = { field + 7, field_len - 8 };
      lsb_read_heka_field(m, &f, fi, ai, &v);
      if (v.type == LSB_READ_STRING) ret = v.u.s;
    }
  }
  return ret;
}
예제 #2
0
int heka_read_message(lua_State *lua, lsb_heka_message *m)
{
  int n = lua_gettop(lua);
  if (n < 1 || n > 3) {
    return luaL_error(lua, "read_message() incorrect number of arguments");
  }
  size_t field_len;
  const char *field = luaL_checklstring(lua, 1, &field_len);
  int fi = (int)luaL_optinteger(lua, 2, 0);
  luaL_argcheck(lua, fi >= 0, 2, "field index must be >= 0");
  int ai = (int)luaL_optinteger(lua, 3, 0);
  luaL_argcheck(lua, ai >= 0, 3, "array index must be >= 0");

  if (strcmp(field, LSB_UUID) == 0) {
    if (m->uuid.s) {
      lua_pushlstring(lua, m->uuid.s, m->uuid.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, LSB_TIMESTAMP) == 0) {
    lua_pushnumber(lua, (lua_Number)m->timestamp);
  } else if (strcmp(field, LSB_TYPE) == 0) {
    if (m->type.s) {
      lua_pushlstring(lua, m->type.s, m->type.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, LSB_LOGGER) == 0) {
    if (m->logger.s) {
      lua_pushlstring(lua, m->logger.s, m->logger.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, LSB_SEVERITY) == 0) {
    lua_pushinteger(lua, m->severity);
  } else if (strcmp(field, LSB_PAYLOAD) == 0) {
    if (m->payload.s) {
      lua_pushlstring(lua, m->payload.s, m->payload.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, LSB_ENV_VERSION) == 0) {
    if (m->env_version.s) {
      lua_pushlstring(lua, m->env_version.s, m->env_version.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, LSB_PID) == 0) {
    lua_pushinteger(lua, m->pid);
  } else if (strcmp(field, LSB_HOSTNAME) == 0) {
    if (m->hostname.s) {
      lua_pushlstring(lua, m->hostname.s, m->hostname.len);
    } else {
      lua_pushnil(lua);
    }
  } else if (strcmp(field, "raw") == 0) {
    lua_pushlstring(lua, m->raw.s, m->raw.len);
  } else if (strcmp(field, "framed") == 0) {
    {
      char header[14] = "\x1e\x00\x08"; // up to 10 varint bytes and a \x1f
      int hlen = lsb_pb_output_varint(header + 3, m->raw.len) + 1;
      header[1] = (char)hlen;
      header[hlen + 2] = '\x1f';
      luaL_Buffer b;
      luaL_buffinit(lua, &b);
      luaL_addlstring(&b, header, hlen + 3);
      luaL_addlstring(&b, m->raw.s, m->raw.len);
      luaL_pushresult(&b);
    }
  } else if (strcmp(field, "size") == 0) {
    lua_pushnumber(lua, (lua_Number)m->raw.len);
  } else {
    if (field_len >= 8
        && memcmp(field, LSB_FIELDS "[", 7) == 0
        && field[field_len - 1] == ']') {
      lsb_read_value v;
      lsb_const_string f = { .s = field + 7, .len = field_len - 8 };
      lsb_read_heka_field(m, &f, fi, ai, &v);
      switch (v.type) {
      case LSB_READ_STRING:
        lua_pushlstring(lua, v.u.s.s, v.u.s.len);
        break;
      case LSB_READ_NUMERIC:
        lua_pushnumber(lua, v.u.d);
        break;
      case LSB_READ_BOOL:
        lua_pushboolean(lua, v.u.d ? 1 : 0);
        break;
      default:
        lua_pushnil(lua);
        break;
      }
    } else {
      lua_pushnil(lua);
    }
  }