#ifndef _DWC_LOG_C
#define _DWC_LOG_C

/*--------------------------------------------------------------------------
 *  Copyright (c) 2001-2004 Igor Daniloff. 
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at this directory.
 *------------------------------------------------------------------------*/
 
#ifndef lint
static const char rcsid[] = "$Id: dwc_log.c,v 1.32 2008/01/19 19:32:45 akorkov Exp $";
#endif

#include <stdio.h>
#include <stdarg.h>
#if defined(__UNIX_DW)
#include <syslog.h>
#endif /* UNIX */
#include "dwc_log.h"
#include "dwc_strings.h"

DWC_bool_t LogEnabled = FALSE;
DWC_bool_t LogSyslog = TRUE;
int        LogLevel = DW_LL_QUIET;
int        LogFacility;
int        LogPriority;

/* -------------------------------------------------- */
DWC_string_t findWhat( int what, DWC_msg_entry_t Table[], DWC_find_t how )
{
    DWC_msg_entry_t     *iterator;
    dwc_multistring_t    str = NULL;
    dwc_string_t         res = NULL;  
    dwc_bool_t           addsep = FALSE;

    if( 0 == what )
        return DWC_Strdup("success");

    str = dw_multistring_create(0);    
    if( !str )
        return DWC_Strdup("(null)");
            
    for( iterator = Table; iterator->name; ++iterator )
    {
        switch( how )
        {
         case FIND_FULL_MASK:
            if( dw_bm_include(what, iterator->code) )
            {
                if( addsep )
                    dw_multistring_add(str, ", ");
                else
                    addsep = TRUE;
                dw_multistring_add(str, iterator->name);
            }
            break;
         case FIND_MASK:
            if( dw_bm_contain(what, iterator->code) )
            {
                if( addsep )
                    dw_multistring_add(str, ", ");
                else
                    addsep = TRUE;
                dw_multistring_add(str, iterator->name);
            }
            break;
         default:
            if( dw_bm_equal(what, iterator->code) )
            {
                if( addsep )
                    dw_multistring_add(str, ", ");
                else
                    addsep = TRUE;
                dw_multistring_add(str, iterator->name);
            }
            break;
        }
    }
    res = dw_multistring_concat(str,DW_SD_FORWARD);
    dw_multistring_delete(str);
    return res;
}

/* -------------------------------------------------- */
void InternalPrint( const int level, const char* fmt, va_list args )
{
    if( level <= LogLevel )
    {
#if defined(__UNIX_DW)    
        if( LogSyslog )
        {
            vsyslog( LogFacility | LogPriority, fmt, args );
        }
        else
#endif /* UNIX */        
        {
            vfprintf( logoutput, fmt, args );
            fprintf( logoutput, "\n" );
            fflush( logoutput );
        }
    }
}

#endif /* _DWC_LOG_C */
