-
Notifications
You must be signed in to change notification settings - Fork 536
/
error
executable file
·40 lines (37 loc) · 1.04 KB
/
error
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
#!/usr/bin/env bash
# error -- Show error message
#
# $ error MSG [MSG_NEXT_LINE]...
# Displays the given messages to stderr and exits with 1.
#
# Colored output can be controled by a switch in environment:
# $ export DEEPDIVE_COLOR=auto # by default, uses color only for tty output
# $ export DEEPDIVE_COLOR=false # disables colored messages in tty
# $ export DEEPDIVE_COLOR=true # forces color coded output
# $ error ...
##
# Author: Jaeho Shin <netj@ropas.snu.ac.kr>
# Created: 2010-07-28
# use prefix and a log file when specified
: ${DEEPDIVE_ERROR_PREFIX:=} ${DEEPDIVE_LOGERROR:=}
# whether to use colored output
: ${DEEPDIVE_COLOR:=auto}
auto() { [[ -t 2 ]]; }
before= after=
if $DEEPDIVE_COLOR; then
# TODO check terminal's color support
before=a; a() { echo -ne '\033[31m'; }
after=b; b() { echo -ne '\033[0m'; }
fi
{
$before
for msg; do
{
[[ -z "$DEEPDIVE_LOGERROR" ]] ||
echo "${DEEPDIVE_ERROR_PREFIX}$msg" >>"$DEEPDIVE_LOGERROR"
} 2>/dev/null
echo "${DEEPDIVE_ERROR_PREFIX}$msg"
done
$after
} >&2
false