Exemple #1
0
int DEBUG_printf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    #if defined(MICROPY_DEBUG_STDERR) && MICROPY_DEBUG_STDERR
    // Printing debug to stderr may give a chance tests which
    // check stdout to pass, etc.
    extern const mp_print_t mp_stderr_print;
    int ret = mp_vprintf(&mp_stderr_print, fmt, ap);
    #else
    int ret = mp_vprintf(&mp_plat_print, fmt, ap);
    #endif
    va_end(ap);
    return ret;
}
Exemple #2
0
int DEBUG_printf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int ret = mp_vprintf(&mp_plat_print, fmt, ap);
    va_end(ap);
    return ret;
}
Exemple #3
0
int mp_printf(const mp_print_t *print, const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int ret = mp_vprintf(print, fmt, ap);
    va_end(ap);
    return ret;
}
Exemple #4
0
int DEBUG_printf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int ret = mp_vprintf(MICROPY_DEBUG_PRINTER, fmt, ap);
    va_end(ap);
    return ret;
}
Exemple #5
0
void mp_warning(const char *msg, ...) {
    va_list args;
    va_start(args, msg);
    mp_print_str(MICROPY_ERROR_PRINTER, "Warning: ");
    mp_vprintf(MICROPY_ERROR_PRINTER, msg, args);
    mp_print_str(MICROPY_ERROR_PRINTER, "\n");
    va_end(args);
}
Exemple #6
0
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...) {
    assert(fmt != NULL);

    // Check that the given type is an exception type
    assert(exc_type->make_new == mp_obj_exception_make_new);

    // Try to allocate memory for the message
    mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
    size_t o_str_alloc = strlen(fmt) + 1;
    byte *o_str_buf = m_new_maybe(byte, o_str_alloc);

    bool used_emg_buf = false;
    #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
    // If memory allocation failed and there is an emergency buffer then try to use
    // that buffer to store the string object and its data (at least 16 bytes for
    // the string data), reserving room at the start for the traceback and 1-tuple.
    if ((o_str == NULL || o_str_buf == NULL)
        && mp_emergency_exception_buf_size >= EMG_TRACEBACK_ALLOC * sizeof(size_t)
            + sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t) + sizeof(mp_obj_str_t) + 16) {
        used_emg_buf = true;
        o_str = (mp_obj_str_t*)((uint8_t*)MP_STATE_VM(mp_emergency_exception_buf)
            + EMG_TRACEBACK_ALLOC * sizeof(size_t) + sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t));
        o_str_buf = (byte*)&o_str[1];
        o_str_alloc = (uint8_t*)MP_STATE_VM(mp_emergency_exception_buf)
            + mp_emergency_exception_buf_size - o_str_buf;
    }
    #endif

    if (o_str == NULL) {
        // No memory for the string object so create the exception with no args
        return mp_obj_exception_make_new(exc_type, 0, 0, NULL);
    }

    if (o_str_buf == NULL) {
        // No memory for the string buffer: assume that the fmt string is in ROM
        // and use that data as the data of the string
        o_str->len = o_str_alloc - 1; // will be equal to strlen(fmt)
        o_str->data = (const byte*)fmt;
    } else {
        // We have some memory to format the string
        struct _exc_printer_t exc_pr = {!used_emg_buf, o_str_alloc, 0, o_str_buf};
        mp_print_t print = {&exc_pr, exc_add_strn};
        va_list ap;
        va_start(ap, fmt);
        mp_vprintf(&print, fmt, ap);
        va_end(ap);
        exc_pr.buf[exc_pr.len] = '\0';
        o_str->len = exc_pr.len;
        o_str->data = exc_pr.buf;
    }

    // Create the string object and call mp_obj_exception_make_new to create the exception
    o_str->base.type = &mp_type_str;
    o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
    mp_obj_t arg = MP_OBJ_FROM_PTR(o_str);
    return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
}
Exemple #7
0
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
    strn_print_env_t strn_print_env = {str, size};
    mp_print_t print = {&strn_print_env, strn_print_strn};
    int len = mp_vprintf(&print, fmt, ap);
    // add terminating null byte
    if (size > 0) {
        if (strn_print_env.remain == 0) {
            strn_print_env.cur[-1] = 0;
        } else {
            strn_print_env.cur[0] = 0;
        }
    }
    return len;
}
Exemple #8
0
int vprintf(const char *fmt, va_list ap) {
    return mp_vprintf(&mp_plat_print, fmt, ap);
}