Ejemplo n.º 1
0
static void debugformatted(char *fmt, va_list va) {
    char buffer[VARG_BUF_LEN] = {0};
    strcpy(buffer, SAVE_CURSOR "\033[0;");
    uitoa(BOTTOM_HALF - 1, 10, &buffer[strlen(buffer)]);
    strcat(buffer, "r" "\033[");
    uitoa(BOTTOM_HALF - 1, 10, &buffer[strlen(buffer)]);
    strcat(buffer, ";0H\r\n");
    strcat(buffer, fmt);
    strcat(buffer, "\033[");
    uitoa(TERM_BOTTOM, 10, &buffer[strlen(buffer)]);
    strcat(buffer, ";");
    uitoa(TERMINAL_HEIGHT, 10, &buffer[strlen(buffer)]);
    strcat(buffer, "r" RESTORE_CURSOR);

    printformatted(IO, buffer, va);
}
Ejemplo n.º 2
0
/* take one json_t and output an ncsa like line */
int jsonncsa(json_t * jsonevent, char *ncsaline, int line_size)
{
    int idx, szwrite = 0;
    long long tmp;
    char *ptr;
    const char *value;
    json_t *jsondata;
    char intbuffer[INTMAXSZ];

    ptr = ncsaline;
    /* if we receive a rejected json, it will not have to be formatted, but directly sent */
    jsondata = json_object_get(jsonevent, REJECTED_JSON);
    if (jsondata != NULL) {
        value = json_string_value(jsondata);
        if (value != NULL) {
            szwrite = snprintf(ptr, line_size, "%s", value);
            return szwrite < 0;
        }
        return -1;
    }
    /* iterate through the fields list contained in entities struct, in order */
    idx = 0;
    do {
        jsondata = json_object_get(jsonevent, entities[idx].name);
        if (jsondata != NULL) {
            json_incref(jsondata);
            value = json_string_value(jsondata);
            if (value != NULL) {
                szwrite = printformatted(ptr, line_size - (ptr - ncsaline),
                                         entities[idx].attribut, value);
            } else {
                /* it is possible that it is instead, an integer from json, so we get it */
                tmp = json_integer_value(jsondata);
                /* json_integer_value will return 0 in case it fails, but then, what should i do?
                   0 value could be a good value in some case in our logs, so there is no fail */
                snprintf(intbuffer, INTMAXSZ, "%lld", tmp);
                szwrite = printformatted(ptr, line_size - (ptr - ncsaline),
                                         entities[idx].attribut,
                                         intbuffer);
            }
            json_decref(jsondata);
        } else {
            szwrite = printformatted(ptr, line_size - (ptr - ncsaline),
                                     entities[idx].attribut, UNDEF_VAL);
        }
        if (szwrite < 0) {
            /* something want really bad, snprintf erro */
            fprintf(stderr, "something went wrong with snprintf\n");
            break;
        }
        /* there we have a possible b0f, should ensure we really got that written, and not all
           we wanna (cf. 'Return value' of snprintf manpage), verify that */
        if (ptr >= (ncsaline + line_size)) {
            fprintf(stderr, "received a too long json\n");
            break;
        }
        ptr += szwrite;
    }
    while (entities[++idx].name != NULL);

    return 0;
}