#!/bin/sh
LOG_FILE=/opt/wlan/var/log/np1.log
LOGSLAVE_TAG="nplus1_Slave"
LOGMASTER_TAG="nplus1_Master"
LOG_PRI="local5"
LOGHOST_PRI="local1"
LOG_CMD="logger"

if [ -f /opt/wlan/np1/config/np1_log.def ]; then
    source /opt/wlan/np1/config/np1_log.def
else
    NP1_LOG_GEN=0
    NP1_LOG_SCRIPT=0
    NP1_LOG_XFER=0
fi


# Busybox does not support -f <LOG_FILE>, so removing it


# *****************************************************************************

# $1 is tag, slave or master
# $2 is pri, i.e., local1 or local5
# $3 is the message
function np1_log_info_fn
{
    tag=$1
    shift
    log_pri=$1
    shift
    msg="$*"
    
    if [ "${tag}" = "master" ]; then
        log_tag=${LOGMASTER_TAG}
    else
        log_tag=${LOGSLAVE_TAG}
    fi
    ${LOG_CMD} -p ${log_pri}.info -t ${log_tag} "INFO: ${msg}"
}


# *****************************************************************************

# $1 is tag, slave or master
# $2 is pri, i.e., local1 or local5
# $3 is the message
function np1_log_warn_fn
{
    tag=$1
    shift
    log_pri=$1
    shift
    msg="$*"
    
    if [ "${tag}" = "master" ]; then
        log_tag=${LOGMASTER_TAG}
    else
        log_tag=${LOGSLAVE_TAG}
    fi

    ${LOG_CMD} -p ${log_pri}.warn -t ${log_tag} "WARN: ${msg}"
}

# *****************************************************************************

# $1 is tag, slave or master
# $2 is pri, i.e., local1 or local5
# $3 is the message
function np1_log_err_fn
{
    tag=$1
    shift
    log_pri=$1
    shift
    msg="$*"
    
    if [ "${tag}" = "master" ]; then
        log_tag=${LOGMASTER_TAG}
    else
        log_tag=${LOGSLAVE_TAG}
    fi

    ${LOG_CMD} -p ${log_pri}.err -t ${log_tag} "ERROR: ${msg}"
}

# *****************************************************************************

# $1 is the module
# $2 is the level
# $3 is the tag, i.e., slave or master
# $4 is the log_pri
# $5 is the message
function np1_log_dbg_fn
{
    log_module=$1
    shift

    log_lvl=$1
    shift

    tag=$1
    shift

    log_pri=$1
    shift

    log_msg="$*"

    if [ "${tag}" = "master" ]; then
        log_tag=${LOGMASTER_TAG}
    else
        log_tag=${LOGSLAVE_TAG}
    fi

    if [ "${log_module}" = "SCRIPT" ]; then
        user_lvl=${NP1_LOG_SCRIPT}
    elif [ "${log_module}" = "" ]; then
        user_lvl=${NP1_LOG_XFER}
    else
        user_lvl=${NP1_LOG_GEN}
    fi

    if [ ${log_lvl} -le ${user_lvl} ]; then
        ${LOG_CMD} -p ${log_pri}.debug -t ${log_tag} "Mod(${log_module}) Lvl(${log_lvl}) ${log_msg}"
    fi
}


# *****************************************************************************

# $1 is message
function np1_log_info_slave
{
    msg="$*"
    np1_log_info_fn slave local5 ${msg}
}

# *****************************************************************************
# $1 is message
function np1_log_warn_slave
{
    msg="$*"
    np1_log_warn_fn slave local5 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_err_slave
{
    msg="$*"
    np1_log_err_fn slave local5 ${msg}
}

# *****************************************************************************


# $1 is message
function np1_loghost_info_slave
{
    msg="$*"
    np1_log_info_fn slave local1 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_loghost_warn_slave
{
    msg="$*"
    np1_log_warn_fn slave local1 ${msg}
}

# ****************************************************************************

# $1 is message
function np1_loghost_err_slave
{
    msg="$*"
    np1_log_err_fn slave local1 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_log_dbg_slave
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"
    np1_log_dbg_fn ${module} ${lvl} slave local5 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_loghost_dbg_slave
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"
    np1_log_dbg_fn ${module} ${lvl} slave local1 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_info_master
{
    msg="$*"
    np1_log_info_fn master local5 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_warn_master
{
    msg="$*"
    np1_log_warn_fn master local5 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_err_master
{
    msg="$*"
    np1_log_err_fn master local5 ${msg}
}

# *****************************************************************************


# $1 is message
function np1_loghost_info_master
{
    msg="$*"
    np1_log_info_fn master local1 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_loghost_warn_master
{
    msg="$*"
    np1_log_warn_fn master local1 ${msg}
}

# *****************************************************************************


# $1 is message
function np1_loghost_err_master
{
    msg="$*"
    np1_log_err_fn master local1 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_log_dbg_master
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"
    np1_log_dbg_fn ${module} ${lvl} master local5 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_loghost_dbg_master
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"
    np1_log_dbg_fn ${module} ${lvl} master local1 ${msg}
}
# *****************************************************************************
# $1 is message
function np1_log_info
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_info_fn  ${np1type} local5 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_warn
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_warn_fn  ${np1type} local5 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_log_err
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_err_fn   ${np1type} local5 ${msg}
}

# *****************************************************************************


# $1 is message
function np1_loghost_info
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_info_fn  ${np1type} local1 ${msg}
}

# *****************************************************************************

# $1 is message
function np1_loghost_warn
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_warn_fn  ${np1type} local1 ${msg}
}

# *****************************************************************************


# $1 is message
function np1_loghost_err
{
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_err_fn  ${np1type} local1 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_log_dbg
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi


    np1_log_dbg_fn ${module} ${lvl}  ${np1type} local5 ${msg}
}

# *****************************************************************************

# $1 is module
# $2 is level
# $3 is message
function np1_loghost_dbg
{
    module=$1
    shift
    lvl=$1
    shift
    msg="$*"

    if [ -f /opt/wlan/np1/config/slave.conf ]; then
        np1type=slave
    else
        np1type=master
    fi

    np1_log_dbg_fn ${module} ${lvl} ${np1type} local1 ${msg}
}
