/* 
** 
** file name: logfunc.c
**
** The module performs log functions.
**
*/

#include 	"../include/includes.h"
#include    <stdarg.h>        /* ANSI C header file */
#include    <syslog.h>


static void    DebugDoIt(int, void *ptr, const char *, va_list ap);


/*
** Nonfatal error related to a system call.
** Print a message with the system's errno value and return.
*/

void
DebugRet(void *ptr, const char *fmt, ...)
{
    va_list        apa;

    va_start(apa, fmt);
    DebugDoIt(1, ptr, fmt, apa);
    va_end(apa);
    return;
}

/*
** Fatal error related to a system call.
** Print a message and terminate.
*/

void
DebugSys(void *ptr, const char *fmt, ...)
{
    va_list        apa;

    va_start(apa, fmt);
    DebugDoIt(1, ptr, fmt, apa);
    va_end(apa);
    exit(2);
}

/*
** Nonfatal error unrelated to a system call.
** Print a message and return.
*/

void
DebugMsg(void *ptr, const char *fmt, ...)
{

   //if(get_debug_log_flag() == 0)
   //	return ;
   
    va_list        apa;
    va_start(apa, fmt);
    DebugDoIt(0, ptr, fmt, apa);
    va_end(apa);
    return;
}

/*
** Fatal error unrelated to a system call.
** Print a message and terminate.
*/

void
DebugQuit(void *ptr, const char *fmt, ...)
{
    va_list        apa;

    va_start(apa, fmt);
    DebugDoIt(0, ptr, fmt, apa);
    va_end(apa);
    exit(2);
}

/*
** Print a message and return to caller.
** Caller specifies "errnoflag" and "priority".
*/

static void
DebugDoIt(int errnoflag, void *ptr, const char *fmt, va_list apa)
{
	int           errno_save;
	char        buf[8192];
	int           len, old_len;

	errno_save = errno;        /* value caller might want printed */
	vsprintf(buf, fmt, apa);
	
	if (errnoflag)
		sprintf(buf+strlen(buf), ": %s", strerror(errno_save));

	strcat(buf, "\r\n");
	len = strlen(buf);
	if(len >= 4096)
		return;
	
	old_len = strlen(ptr); 
	if((len+old_len) < 4096)
		strcat(ptr,buf);
	else
   		strcpy(ptr,buf);

	return;
}

/* Write a message to loop queue. */
BOOL 
WriteDebugMsg(void *dest, const void *src)
{
	_debug_info     *p;
    
    p = (_debug_info *) dest;
    memcpy(&p->infoDebug[p->subWrite], src, DEBUG_LINE_LEN);
    p->subWrite ++;

    /* 
    ** if write subscript more than max_num, 
    ** write subscript equal 0.
    */
    if (p->subWrite == DEBUG_BUFFER_LINE)
        p->subWrite = 0;

    /* 
    ** if message written successful,
    ** Read subscript is same as Write subscript, 
    ** then Read subsrcipt add 1.
    */
    if (p->subWrite == p->subRead) {
        (p->subRead == DEBUG_BUFFER_LINE-1) ? 
            (p->subRead = 0) : (p->subRead ++);
		return  FAILURE;
	}
	
	return SUCCESS;
}

/* Read a message from loop queue. */
BOOL
ReadDebugMsg(void *dest, const void *src)
{
	_debug_info        *p;
    
	p = (_debug_info *) src;
    /* 
    ** these are some messages not to be read 
    ** while subscript of read isn't equal to subscipt of write.
    */
    if (p->subRead == p->subWrite)
        return FAILURE;

    memcpy(dest, &p->infoDebug[p->subRead], DEBUG_LINE_LEN);
    /*
    ** increase READ SUBSCRIPT after got a message,
    ** set READ SUBSCRIPT to 0 while it equal to max_num.
    */
    p->subRead ++;
    if (p->subRead == DEBUG_BUFFER_LINE)
    {
		p->subRead = 0;
	}
		
	return SUCCESS;
}


