-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger
executable file
·62 lines (54 loc) · 1.37 KB
/
logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
set -eu
#######################################
# Function:
# Parameters:
# $1: message type
# $2: message
# Output:
# Colored string
#######################################
function print_usage() {
echo 'Usage:'
echo ' logger -p [type] [message...]'
echo " -p prefixed output"
echo ' [type] = info / warn / ok / error / tip / debug'
echo ''
echo "Examples:"
echo " logger info 'hello world!'"
}
#######################################
# flags
declare add_prefix='false'
declare option
while getopts ph option; do
case $option in
p) add_prefix='true' ;;
h) print_usage; exit 0; ;;
*) print_usage; exit 1; ;;
esac
done
shift $((OPTIND - 1))
#######################################
utils_dir="$(cd "$(dirname "$0")"; pwd)"
declare -r utils_dir
if echo "$PATH" | grep -v "$utils_dir" &> /dev/null; then
export PATH="$utils_dir:$PATH"
fi
if [[ $# == 0 ]] || [[ $# == 1 && ! -p /dev/stdin ]]; then
color_print -np 'error' 'Incorrect number of parameters.'
print_usage
exit 1;
fi
if [[ -p /dev/stdin ]]; then # <-- make pipe work
str=$(cat -) # <-- make pipe work
else
str="${*:2}"
fi
declare -r str
color_print "$1" "[$(date "+%Y/%m/%d %H:%M:%S")] "
if [[ $add_prefix == 'true' ]]; then
color_print -np "$1" "$str"
else
color_print -n "$1" "$str"
fi