#include <pthread.h>
#include "ysp_debug.h"
#include "database.h"
#include <stdarg.h>
#include <stdio.h>
#include "common.h" 


unsigned int debug_flags=/*PARA_TRACE_DEBUG|*/TRACE_DEBUG|ANALYSIS_TRACE_DEBUG|ANALYSIS_EEROR_DEBUG|ERROR_DEBUG|ACTION_DEBUG|COMUNICATION_TRACE_DEBUG|COMUNICATION_EEROR_DEBUG;

static pthread_mutex_t  mutex_log;
static int mutex_log_inited=0;

static char my_log_buf[1024];

void my_log_init(void)
{
if(!mutex_log_inited)
  {
   if(pthread_mutex_init(&mutex_log,NULL)!=0)
   {
     printf("init log mutex failed\r\n");
	 return;	
   } 
   all_table_init();     
   mutex_log_inited++;
  }	
}

void lock_log(void)
{
  if(!mutex_log_inited)
    my_log_init();
  pthread_mutex_lock(&mutex_log);	
}

void unlock_log(void)
{
 pthread_mutex_unlock(&mutex_log);		
}


void do_my_log(const char *fmt,...)/*__attribute__((format(printf,2,3)))*/
{
  int len;
  va_list args;
  lock_log();
  va_start(args, fmt );
  len=vsprintf(my_log_buf,fmt,args);
  va_end(args);	
  log_entry_add(my_log_buf,len);
  unlock_log();
}

void remove_old_log(unsigned int rsv_time)
{
  lock_log();
  data_entry_remove("log_tbl",(time_t)rsv_time);
  unlock_log();
}

int add_his_entry(ANALY_RESULT *str)
{
 int ret;
 lock_log();
 ret=his_entry_add(str);
 unlock_log();
 return ret;	
}

int read_his_entry(ANALY_RESULT *str)
{
 int ret;
 lock_log();
 ret=his_entry_read(str);
 unlock_log();
 return ret;	
}

