#!/bin/bash source "bash-colors.sh" # Echo formatted time right now function _now { date +"%y/%m/%d %H:%M:%S" } # Write log to stdout # Usage: log # Usage with tag: log -t # Usage with pipe: log -p [] function log { if ! [ "${1}" ] || [ "${1}" = "-p" ]; then # Read from pipe PREFIX="PIPE" [ "${2}" = "" ] || PREFIX="${2}" while read -r LINE; do echo -e "${FG_YELLOW}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" done else # Read from arguments PREFIX="INFO" [ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}" shift 2 echo "${@}" | while read -r LINE; do echo "[ $(_now) :: ${PREFIX} ] ${LINE}" done fi } # Write log to stderr # Usage: logError # Usage with tag: logError -t # Usage with pipe: logError -p [] function logError { if ! [ "${1}" ] || [ "${1}" = "-p" ]; then # Read from pipe PREFIX="FAIL" [ "${2}" = "" ] || PREFIX="${2}" while read -r LINE; do echo -e "${BG_RED}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" >&2 done else # Read from arguments PREFIX="FAIL" [ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}" shift 2 echo "${@}" | while read -r LINE; do echo -e "${BG_RED}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" >&2 done fi } # Log tagged for sensor scripts, can be muted with the -qs arg on main.sh # Usage: logScript # Usage with tag: logScript -t # Usage with pipe: logScript -p [] function logScript { if ! [ "${SCRIPTS_QUIET}" = "yes" ]; then if ! [ "${1}" ] || [ "${1}" = "-p" ]; then # Read from pipe PREFIX="SENS" [ "${2}" = "" ] || PREFIX="${2}" while read -r LINE; do echo -e "[ $(_now) :: ${PREFIX} ] ${LINE}" done else # Read from arguments PREFIX="SENS" [ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}" shift 2 echo "${@}" | while read -r LINE; do echo "[ $(_now) :: ${PREFIX} ] ${LINE}" done fi fi }