80 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
 | 
						|
 | 
						|
#include "if_rtt.h"
 | 
						|
#include "log.h"
 | 
						|
#include "stdio.h"
 | 
						|
#include "stdarg.h"
 | 
						|
#include "debug.h"
 | 
						|
#include "string.h"
 | 
						|
#ifdef RT_THREAD
 | 
						|
#include "rtthread.h"
 | 
						|
#define DBG_DEV       mylog()
 | 
						|
#else
 | 
						|
#define DBG_DEV     rtt()
 | 
						|
#endif
 | 
						|
 | 
						|
 | 
						|
#define CONSOLEBUF_SIZE 1024
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
typedef struct{
 | 
						|
  int inited;
 | 
						|
	#ifdef RT_THREAD
 | 
						|
  struct rt_mutex mutex;
 | 
						|
	#endif
 | 
						|
}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
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |