Tools.sh 1.98 KB
#!/bin/bash

#################################################
# File :    Tools.sh                            #
# Brief :   Tool functions                      #
# Author :  Jean-François Rey                   #
# Version : 1.0                                 #
# Date :    13/06/13                            #
#################################################

# Type
ERROR=2
WARN=1
INFO=0

# Global Verbose variable (have to be define in scripts)
# 0 : mute
# 1 : basic info (start,config,end)
# 2 : add process
# 3 : all
VERBOSE=0

# print_message( verbose_level, message)
# - message_type : ERROR, WARN, INFO
# - verbose_level : level to print message
# - message : the message to print
function print_message()
{
    if [ $# -eq 3 ]
    then
        # print error
        if [ $1 -eq $ERROR ]; then echo -ne "###ERROR : $3\n" > /dev/stderr; return 0; fi
        # check verbose level
        if [ $2 -le $VERBOSE ]
        then
            #for (( dec=1 ; $dec<$2 ; dec++ )); do echo -ne "\t"; done
            # print warn
            if [ $1 = $WARN ]; then echo -ne "##WARNING : $3\n"; return 0; fi 
            # default info
            echo -ne "#INFO : $3\n" 
        fi
    fi
}

# print_error(message)
# - message : error message to print
function print_error()
{
    print_message $ERROR 3 "$1"
}

# print_warn(message,[level])
# - message : message to print
# - level : optional option - verbose level to print
function print_warn()
{
    if [ $# -eq 2 ]
    then
       print_message $WARN $2 "$1"
    else
        print_message $WARN $VERBOSE "$1"
    fi
}

# print_info(message,[level])
# - message : message to print
# - level : optional option - verbose level to print
function print_info()
{
    if [ $# -eq 2 ]
    then
        print_message $INFO $2 "$1"
    else
        print_message $INFO $VERBOSE "$1"
    fi
}

# print_log_file(filename,message)
# - filename : full path to log file
# - message : message to log
print_log_file()
{
    if [ $# -eq 2 ]
    then
        echo -e "$2" >> "$1"
    fi
}