You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.3 KiB
Bash

#!/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 <text>
# Usage with tag: log -t <tag> <text>
# Usage with pipe: log -p [<tag>]
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 <text>
# Usage with tag: logError -t <tag> <text>
# Usage with pipe: logError -p [<tag>]
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 <text>
# Usage with tag: logScript -t <tag> <text>
# Usage with pipe: logScript -p [<tag>]
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
}