#include "SEGGER_RTT.h"

#include <stdarg.h>
#include <stdio.h>

int SEGGER_RTT_vprintf(unsigned BufferIndex, const char *sFormat, va_list *pParamList)
{
    char buffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
    int len = vsnprintf(buffer, sizeof(buffer), sFormat, *pParamList);

    if (len < 0) {
        return len;
    }
    if ((unsigned)len > sizeof(buffer)) {
        len = (int)sizeof(buffer);
    }

    return (int)SEGGER_RTT_Write(BufferIndex, buffer, (unsigned)len);
}

int SEGGER_RTT_printf(unsigned BufferIndex, const char *sFormat, ...)
{
    int result;
    va_list args;

    va_start(args, sFormat);
    result = SEGGER_RTT_vprintf(BufferIndex, sFormat, &args);
    va_end(args);
    return result;
}
