/*
 $License:
    Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
    Copyright (C) 2012 MBARI
 $
 */
/******************************************************************************
 * $Id: log_at32.c $ 
 ******************************************************************************/
 
/**
 * @defgroup MPL_LOG
 * @brief Logging facility for the MPL
 *
 * @{
 *      @file     log_at32.c
 *      @brief    Core logging facility functions.
 *
 *
**/

#include <beds.h>
#include <stdio.h>
#include <string.h>

#include "mltypes.h"

#include "log.h"
#include <syslog.h>
#include <cfxpico.h>

#define LOG_BUFFER_SIZE 1024
static char new_fmt[LOG_BUFFER_SIZE];

/* Enabled log types, in order of the MPL_LOG_xxx #defines in log.h */
static char logEnabled[MPL_LOG_SILENT+1] = 
    {0, 0, 0, 0, 0, 1, 1, 0};

/* Function added 7/19/2012, rah, MBARI	*/
void _MPL_ENABLE_LOG(int logType, bool enabled)
{
    if ((logType >= 0) && (logType <= MPL_LOG_SILENT))
	logEnabled[logType] = (enabled ? 1 : 0);
}

int _MLPrintLog (int priority, const char* tag, const char* fmt, ...)
{
    va_list ap;
    int result;

    va_start(ap, fmt);
    result = _MLPrintVaLog(priority,tag,fmt,ap);
    va_end(ap);

    return result;
}

#if 1
int _MLPrintVaLog(int priority, const char* tag, const char* fmt, va_list args)
{
    int result;
    char priority_char;

    if (NULL == fmt) {
        fmt = "";
    }
    if ((priority < 0) || (priority > MPL_LOG_SILENT))
	return(INV_ERROR_INVALID_PARAMETER);

    if (logEnabled[priority] == 0)
	return(INV_SUCCESS);

    switch (priority) {
    case MPL_LOG_UNKNOWN:
        priority_char = 'U';
        break;
    case MPL_LOG_VERBOSE:
        priority_char = 'V';
        break;
    case MPL_LOG_DEBUG:
        priority_char = 'D';
        break;
    case MPL_LOG_INFO:
        priority_char = 'I';
        break;
    case MPL_LOG_WARN:
        priority_char = 'W';
        break;
    case MPL_LOG_ERROR:
        priority_char = 'E';
        break;
    case MPL_LOG_SILENT:
        priority_char = 'S';
        break;
    case MPL_LOG_DEFAULT:
    default:
        priority_char = 'D';
        break;
    };

    result = snprintf(new_fmt, sizeof(new_fmt), "%c/%s:%s", 
                       priority_char, tag, fmt);
    if (result <= 0) {
        return INV_ERROR_LOG_MEMORY_ERROR;
    }
#if 0
    result = vprintf(new_fmt, args);
#else
    result = vSysLogPrintf(LOG_MPU, new_fmt, args);
#endif
    return((result <= 0) ? INV_ERROR_LOG_OUTPUT_ERROR : INV_SUCCESS);
}

#else

int _MLPrintVaLog(int priority, const char* tag, const char* fmt, va_list args)
{
    int	result;

    printf("%ld/%s: ", priority, tag);
    result = vprintf(fmt, args);
    return((result <= 0) ? INV_ERROR_LOG_OUTPUT_ERROR : INV_SUCCESS);
}
#endif

int _MPL_LOGV(char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(_MLPrintVaLog(MPL_LOG_VERBOSE, MPL_LOG_TAG, fmt, ap)); 
}

int _MPL_LOGD(char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(_MLPrintVaLog(MPL_LOG_DEBUG, MPL_LOG_TAG, fmt, ap)); 
}

int _MPL_LOGI(char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(_MLPrintVaLog(MPL_LOG_INFO, MPL_LOG_TAG, fmt, ap)); 
}

int _MPL_LOGW(char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(_MLPrintVaLog(MPL_LOG_WARN, MPL_LOG_TAG, fmt, ap)); 
}

int _MPL_LOGE(char *fmt, ...)
{
    va_list	ap;
 
    va_start(ap, fmt);
    return(_MLPrintVaLog(MPL_LOG_ERROR, MPL_LOG_TAG, fmt, ap)); 
}

/**
 * @}
**/


