12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /*
- * log.c
- *
- * Created on: Nov 7, 2022
- * Author: tangm
- */
- #include "log.h"
- #include <stdarg.h>
- #include <stdio.h>
- #include "usart.h"
- ///重定向c库函数printf到串口,重定向后可使用printf函数
- int fputc(int ch, FILE *f)
- {
- /* 发送一个字节数据到串口 */
- USART_SendData(USART_232, (uint8_t) ch);
-
- /* 等待发送完毕 */
- while (USART_GetFlagStatus(USART_232, USART_FLAG_TXE) == RESET);
- return (ch);
- }
- ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
- int fgetc(FILE *f)
- {
- /* 等待串口输入数据 */
- while (USART_GetFlagStatus(USART_232, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(USART_232);
- }
- 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;
- }
- }
|