102 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| 
 | |
| #include "board.h"
 | |
| #include "if_rtt.h"
 | |
| #include "stdio.h"
 | |
| #include "stdarg.h"
 | |
| #include "debug.h"
 | |
| #include "string.h"
 | |
| #ifdef RT_THREAD
 | |
| #include "rtthread.h"
 | |
| #endif
 | |
| 
 | |
| 
 | |
| #if 1
 | |
| 
 | |
| 
 | |
| #define DBG_DEV_INIT()     rtt()->init()
 | |
| #define DBG_DEV_WRITE(d,len)    rtt()->write(d,len)
 | |
| 
 | |
| #else
 | |
| 
 | |
| #define DBG_DEV_INIT()\
 | |
|   {\
 | |
|     g_data.uart=dev_get("uart1");\
 | |
|     if(g_data.uart){\
 | |
|       g_data.uart->init(g_data.uart,0);\
 | |
|     }\
 | |
|   }
 | |
| #define DBG_DEV_WRITE(d,len)\
 | |
|   {\
 | |
|     if(g_data.uart){\
 | |
|       g_data.uart->write(g_data.uart,d,len);\
 | |
|     }\
 | |
|   }
 | |
| 
 | |
| #endif
 | |
| 
 | |
| #define CONSOLEBUF_SIZE 1024
 | |
| 
 | |
| 
 | |
| 
 | |
| #ifdef DEBUG
 | |
| 
 | |
| typedef struct{
 | |
|   int inited;
 | |
| 	#ifdef RT_THREAD
 | |
|   struct rt_mutex mutex;
 | |
| 	#endif
 | |
|   uart_def *uart;
 | |
| }self_def;
 | |
| 
 | |
| static self_def g_data;
 | |
| 
 | |
| int debug_init(void)
 | |
| {
 | |
|   if(g_data.inited==0)
 | |
|   {
 | |
| 		#ifdef RT_THREAD
 | |
|     rt_mutex_init(&g_data.mutex,"debug_mutex",RT_IPC_FLAG_FIFO);
 | |
| 		#endif
 | |
|     DBG_DEV_INIT();
 | |
|     DBG_DEV_WRITE((const uint8_t *)"\r\n",2);
 | |
|     g_data.inited=1;
 | |
|     DBG_LOG("debug inited.\r\n");
 | |
|   }
 | |
|   return 0;
 | |
| }
 | |
| 
 | |
| 
 | |
| void debug_log(const char *file,const char *fun,int line,int level,const char *fmt, ...)
 | |
| {
 | |
|   if(g_data.inited==0) return;
 | |
|   va_list args;
 | |
|   size_t length;
 | |
|   static char log_buf[CONSOLEBUF_SIZE];
 | |
|   static const char *level_str[]={"[info] ","[log] ","[warn] ","[err] "};
 | |
|   static int level_str_len[]={7,6,7,6};
 | |
|   if(level<DBG_LEVEL_INFO||level>DBG_LEVEL_ERR) return;
 | |
| 	#ifdef RT_THREAD
 | |
|   rt_mutex_take(&g_data.mutex,RT_WAITING_FOREVER);
 | |
| 	#endif
 | |
|   memcpy(log_buf,level_str[level],level_str_len[level]);
 | |
|   length=level_str_len[level];
 | |
|   length+=sprintf(log_buf + length,"%s|%s|%d| ",file,fun,line);
 | |
|   
 | |
|   va_start(args, fmt);
 | |
|   length += vsnprintf(log_buf + length, sizeof(log_buf) - length - 1, fmt, args);
 | |
|   if (length > CONSOLEBUF_SIZE - 1)
 | |
|     length = CONSOLEBUF_SIZE - 1;
 | |
|   va_end(args);
 | |
|   memcpy(&log_buf[length],"\r\n",2);
 | |
|   length+=2;
 | |
|   DBG_DEV_WRITE((const uint8_t *)log_buf,length);
 | |
| 	#ifdef RT_THREAD
 | |
|   rt_mutex_release(&g_data.mutex);
 | |
| 	#endif
 | |
| }
 | |
| 
 | |
| #endif
 | |
| 
 | |
| 
 | |
| 
 | 
