/* * log.c * * Created on: Nov 7, 2022 * Author: tangm */ #include "log.h" #include "usart.h" #include #include #include "ucos_ii.h" #include "main.h" #include "udp_send.h" #include "stdlib.h" #include "malloc.h" static OS_EVENT *logMutex; uint8_t perr; void LogPrint(logLevel_t logLevel,const char *file, const char *func, const int line, char * fmt, ...) { OSMutexPend(logMutex, 5000, &perr); if(perr == OS_ERR_NONE) { udp_log_start(); va_list args; va_start(args, fmt); char buf[LOG_LEN_MAX]; vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); char *p=mymalloc(SRAMEX, 1024); switch (logLevel) { #ifdef _LOG_INFO case LOG_INFO: sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "INFO", file, func, line, buf); break; #endif #ifdef _LOG_DEBUG case LOG_DEBUG: sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "DEBUG",file, func, line, buf); break; #endif #ifdef _LOG_WARN case LOG_WARN: sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "WARN", file, func, line, buf); break; #endif case LOG_ERROR: sprintf(p,"[%s][%s][%s:%4d] %s\r\n", "ERROR",file, func, line, buf); break; default: break; } udp_send_printf(p); myfree(SRAMEX, p); udp_log_close(); perr = OSMutexPost(logMutex); } } void log_init() { logMutex = OSMutexCreate (0, &perr);//创建一个log互斥量 OSTimeDly(100); }