# Common debuging functions
#
# This file is sourced by can_messaging_functions and ad_dmu_functions. If you
# source either of these files, there is no need to source this one, however
# even if you do, it won't break things.
#
# To make use of the debugging functionality, prior to sourcing this file or
# any other file that sources this one, add the following (uncommented of
# course), depending on the "debug level" you wish to enable:
#
#   # Enable "function" level debugging
#   DEBUG_FUNC="yes"
#
#   # Enable "information" level debugging
#   DEBUG_INFO="yes"
#
# This will produce debugging information on the standard terminal console. To
# direct the output straight to a file instead, add the following just below
# the previous line:
#
#   OUTPUT_TO_FILE="yes"
#   OUTPUT_FILE="<filename>"
#
# replacing <filename> for the log file name you want to use. Note that the
# debug output will append to any existing, writable file, or create a new
# file if it does not exist and the directory is writable
#

# rudimentary "code guard"
HAS_DEBUGGING_FUNCTIONS="yes"

# @param $1 function name
function debug_function
{
    if [ "$DEBUG_FUNC" = "yes" ]
    then
        output "DEBUG FUNCTION: $1"
    fi
}

# @param $1 information
function debug_information
{
    if [ "$DEBUG_INFO" = "yes" ]
    then
        output "DEBUG INFORMATION: $1"
    fi
}

# @param $1 data to output
function output
{
    if [ "$OUTPUT_TO_FILE" = "yes" ]
    then
        output_to_file "$1"
    else
        output_to_console "$1"
    fi
}

# @param $1 data to output to file
function output_to_file
{
    if [ "x$OUTPUT_FILE" != "x" ]
    then 
        if [ -w $OUTPUT_FILE ]
        then
            TIMESTAMP=`date`
            echo "$TIMESTAMP $1" >> $OUTPUT_FILE
        else
            output_to_console $1
        fi
    else
        output_to_console $1
    fi
}

# @param $1 data to output to console
function output_to_console
{
    echo "$1"
}
