/*
 * log.c
 *
 *  Created on: Nov 7, 2022
 *      Author: tangm
 */
#include "log.h"
#include "usart.h"
#include "config.h"
#include <stdarg.h>
#include <stdio.h>

int fputc(int ch, FILE *f)
{
    usart_data_transmit(COM_232, (uint8_t)ch);
    while (RESET == usart_flag_get(COM_232, USART_FLAG_TBE));
    return ch;
}

void LogPrint(logLevel_t logLevel, const char *func, const int line, char * fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	char buf[LOG_LEN_MAX];
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	switch (logLevel)
	{
#ifdef _LOG_INFO
		case LOG_INFO:
			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "INFO", func, line, buf);
			break;
#endif
#ifdef _LOG_DEBUG
		case LOG_DEBUG:
			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "DEBUG", func, line, buf);
			break;
#endif
#ifdef _LOG_WARN
		case LOG_WARN:
			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "WARN", func, line, buf);
			break;
#endif
		case LOG_ERROR:
			printf("LOG[%-5s][%-20s:%4d] %s\r\n", "ERROR", func, line, buf);
			break;
		default:
			break;
	}
}
