Ejemplo n.º 1
0
void _log(fc_log_t *log, const char *file, int line, int level, const char *fmt, ...)
{
    int     len, size, errno_save;
    char    buf[FC_MAX_ERR_STR];
    va_list args;
    struct  timeval tv;
    struct  tm *tm;

    if (!log || log->fd < 0) {
        return;
    }

    gettimeofday(&tv, NULL);
    tm = localtime(&tv.tv_sec);

    errno_save = errno;
    len  = 0;
    size = FC_MAX_ERR_STR;
    len += fc_scnprintf(buf + len, size - len, "[%02d-%02d %02d:%02d:%02d.%ld] "
                                               "[%*s] [%-12s:%-4d] ",
                        tm->tm_mon,  tm->tm_mday,
                        tm->tm_hour, tm->tm_min, tm->tm_sec,
                        tv.tv_usec,
                        MAX_LEVEL_DESC_LEN,
                        loglevel_desc[level],
                        fc_basename(file), line);

    va_start(args, fmt);
    len += fc_vscnprintf(buf + len, size - len, fmt, args);
    va_end(args);

    // we do not need '\0'
    buf[len++] = '\n';

    if (write(log->fd, buf, len) < 0) {
        log->nerr ++;
    }
    errno = errno_save;
}
Ejemplo n.º 2
0
static void
req_process_num(struct context *ctx, struct conn *conn, struct msg *msg)
{
    rstatus_t status;
    uint8_t *key, nkey, cid;
    struct item *it;
    struct itemx *itx;
    uint64_t cnum, nnum;
    char numstr[FC_UINT64_MAXLEN];
    int n;

    key = msg->key_start;
    nkey = (uint8_t)(msg->key_end - msg->key_start);

    /* 1). look up existing itemx */
    itx = itemx_getx(msg->hash, msg->md);
    if (itx == NULL) {
        /* 2a). miss -> return NOT_FOUND */
        rsp_send_status(ctx, conn, msg, MSG_RSP_NOT_FOUND);
        return;
    }

    /* 2b). hit -> read existing item into it */
    it = slab_read_item(itx->sid, itx->offset);
    if (it == NULL) {
        rsp_send_error(ctx, conn, msg, MSG_RSP_SERVER_ERROR, errno);
        return;
    }
    if (item_expired(it)) {
        rsp_send_status(ctx, conn, msg, MSG_RSP_NOT_FOUND);
        return;
    }

    /* 3). sanity check item data to be a number */
    status = fc_atou64(item_data(it), it->ndata, &cnum);
    if (status != FC_OK) {
        rsp_send_error(ctx, conn, msg, MSG_RSP_CLIENT_ERROR, EINVAL);
        return;
    }

    /* 4). remove existing itemx of it */
    itemx_removex(msg->hash, msg->md);

    /* 5). compute the new incr/decr number nnum and numstr */
    if (msg->type == MSG_REQ_INCR) {
        nnum = cnum + msg->num;
    } else {
        if (cnum < msg->num) {
            nnum = 0;
        } else {
            nnum = cnum - msg->num;
        }
    }
    n = fc_scnprintf(numstr, sizeof(numstr), "%"PRIu64"", nnum);

    /* 6). alloc new item that can hold n worth of bytes */
    cid = item_slabcid(nkey, n);
    ASSERT(cid != SLABCLASS_INVALID_ID);

    it = item_get(key, nkey, cid, n, time_reltime(msg->expiry), msg->flags,
                   msg->md, msg->hash);
    if (it == NULL) {
        rsp_send_error(ctx, conn, msg, MSG_RSP_SERVER_ERROR, ENOMEM);
        return;
    }

    /* 7). copy numstr to it */
    fc_memcpy(item_data(it), numstr, n);

    rsp_send_num(ctx, conn, msg, it);
}