Exemplo n.º 1
0
int heka_encode_message(lua_State *lua)
{
  int n = lua_gettop(lua);
  bool framed = false;

  switch (n) {
  case 2:
    luaL_checktype(lua, 2, LUA_TBOOLEAN);
    framed = lua_toboolean(lua, 2);
    // fall thru
  case 1:
    luaL_checktype(lua, 1, LUA_TTABLE);
    break;
  default:
    return luaL_argerror(lua, n, "incorrect number of arguments");
  }

  lsb_lua_sandbox *lsb = lua_touserdata(lua, lua_upvalueindex(1));
  if (!lsb) {
    return luaL_error(lua, "encode_message() invalid upvalueindex");
  }

  lsb_heka_sandbox *hsb = lsb_get_parent(lsb);
  set_missing_headers(lua, 1, hsb);

  lsb->output.pos = 0;
  lsb_err_value ret = heka_encode_message_table(lsb, 1);
  if (ret) {
    const char *err = lsb_get_error(lsb);
    if (strlen(err) == 0) err = ret;
    return luaL_error(lua, "encode_message() failed: %s", err);
  }

  size_t len = 0;
  const char *output = lsb_get_output(lsb, &len);
  lsb->usage[LSB_UT_OUTPUT][LSB_US_CURRENT] = len;

  if (framed) {
    char header[14] = "\x1e\x00\x08"; // up to 10 varint bytes and a \x1f
    int hlen = lsb_pb_output_varint(header + 3, len) + 1;
    lsb->usage[LSB_UT_OUTPUT][LSB_US_CURRENT] = len + hlen + LSB_HDR_FRAME_SIZE;
    header[1] = (char)hlen;
    header[hlen + 2] = '\x1f';
    luaL_Buffer b;
    luaL_buffinit(lua, &b);
    luaL_addlstring(&b, header, hlen + LSB_HDR_FRAME_SIZE);
    luaL_addlstring(&b, output, len);
    luaL_pushresult(&b);
  } else {
    lua_pushlstring(lua, output, len);
  }

  if (lsb->usage[LSB_UT_OUTPUT][LSB_US_CURRENT]
      > lsb->usage[LSB_UT_OUTPUT][LSB_US_MAXIMUM]) {
    lsb->usage[LSB_UT_OUTPUT][LSB_US_MAXIMUM] =
        lsb->usage[LSB_UT_OUTPUT][LSB_US_CURRENT];
  }
  return 1;
}
Exemplo n.º 2
0
static void output_heka(lsb_heka_message *msg)
{
  static char header[14] = "\x1e\x00\x08"; // up to 10 varint bytes and a \x1f
  int hlen = lsb_pb_output_varint(header + 3, msg->raw.len) + 1;
  header[1] = (char)hlen;
  header[hlen + 2] = '\x1f';
  if (fwrite(header, hlen + 3, 1, stdout) != 1) {
    log_cb(NULL, NULL, 0, "error outputting header");
    exit(1);
  }
  if (fwrite(msg->raw.s, msg->raw.len, 1, stdout) != 1) {
    log_cb(NULL, NULL, 0, "error outputting message");
    exit(1);
  }
  return;
}
Exemplo n.º 3
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);
    }
  }