blob: b0ebc31a1adb175c2b9d5aaaa9b0e540bde1aeb7 (
plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#!/bin/bash
# vim:sw=8:noet
# SPDX-License-Identifier: GPL-2.0+
#
# Logging subsystem for alt-rootfs-installer.
#
# To initialize the subsystem use log_init() function. The subsystem works in
# two major modes "log" and "con".
# The first mode is "log", it outputs everything to the log file. It is
# still possible to output messages to stdout, but one should use function
# from this module to achieve that.
# The second mode is "con", it outputs everything to the terminal. In this
# mode it is also possible to redirect the output to a file. The module checks
# whether the file descriptor 1 is a terminal or not. That check was done to
# disable colors and column indentations to not clobber the output in the
# file.
. shell-error 2>/dev/null
. shell-terminfo 2>/dev/null
DEFAULT_LOG_FILE="$TMP/alt-rootfs-installer.log"
[ -n "$LOG_MODE" ] || LOG_MODE="log"
[ "$LOG_MODE" = "log" -a -z "$LOG_FILE" ] &&
LOG_FILE="$DEFAULT_LOG_FILE"
LOG_FD1=1
LOG_FD2=2
# Print a message. This function is intended to be used with print_done() or
# print_fail() functions.
#
# Arguments:
# $1 -- a message to output
print_message() {
[ "$LOG_MODE" == "con" ] || echo "${1-}"
if [ -t "$LOG_FD1" ]; then
1>&$LOG_FD1 color_text "* " magenta
1>&$LOG_FD1 echo "${1-}"
else
1>&$LOG_FD1 echo "* ${1-}"
fi
PRINT_TEXT_END=1
}
# Print text without a new line character. It is possible to change color
# and/or a column number to output.
#
# Arguments:
# $1 -- text to output
# $2 (optional) -- color ('-' to skip it)
# $3 (optional) -- a column number
printx() {
local col=
local color=
[ "$LOG_MODE" == "con" ] || echo -n "${1-}"
if [ -t "$LOG_FD1" ]; then
[ $# -gt 2 ] && col="$3"
[ $# -gt 1 ] && color="$2"
[ -n "$col" ] && 1>&$LOG_FD1 terminfo_col "$col"
[ "$color" == "-" ] && color=
1>&$LOG_FD1 color_text "${1-}" "$color"
else
1>&$LOG_FD1 echo -n "${1-}"
fi
}
# Print text with a new line character. It is possible to change color
# and/or a column number to output.
#
# Arguments:
# $1 -- text to output
# $2 (optional) -- color ('-' to skip it)
# $3 (optional) -- a column number
printn() {
local col=
local color=
[ "$LOG_MODE" == "con" ] || echo "${1-}"
if [ -t "$LOG_FD1" ]; then
[ $# -gt 2 ] && col="$3"
[ $# -gt 1 ] && color="$2"
[ -n "$col" ] && 1>&$LOG_FD1 terminfo_col "$col"
[ "$color" == "-" ] && color=
1>&$LOG_FD1 color_message "${1-}" "$color"
else
1>&$LOG_FD1 echo "${1-}"
fi
}
# Print [DONE] with a green color (in the case of terminal).
print_done() {
[ "$LOG_MODE" == "con" ] || echo "DONE"
if [ -t "$LOG_FD1" ]; then
1>&$LOG_FD1 color_message "[DONE]" green
else
1>&$LOG_FD1 echo "[DONE]"
fi
PRINT_TEXT_END=
}
# Print [FAIL] with a red color (in the case of terminal).
print_fail() {
[ "$LOG_MODE" == "con" ] || echo "FAIL"
if [ -t "$LOG_FD1" ]; then
1>&$LOG_FD1 color_message "[FAIL]" red
else
1>&$LOG_FD1 echo "[FAIL]"
fi
PRINT_TEXT_END=
}
# Print an error message and exit.
#
# Arguments:
# $1 -- an error message
fatal_error() {
[ -n "${PRINT_TEXT_END-}" ] && print_fail
[ "$LOG_MODE" == "con" ] || echo "Error: ${1-}"
if [ -t "$LOG_FD1" ]; then
1>&$LOG_FD1 color_text "Error: " red
else
1>&$LOG_FD1 echo -n "Error: "
fi
1>&$LOG_FD1 2>&$LOG_FD2 fatal "${1-}"
}
# Execute command and redirect stderr to /dev/tty
# $@ -- command and arguments to execute
log_errtty() {
2>/dev/tty $@
}
# Initialize logging subsystem.
log_init() {
# Initialize shell-terminfo from libshell
terminfo_init
if [ "$LOG_MODE" != "con" ]; then
# cleanup log file
if [ ! -d "${LOG_FILE%/*}" ]; then
fatal_error "can't locate directory ${LOG_FILE%/*} for log file"
fi
> "$LOG_FILE"
exec {LOG_FD1}>&1 {LOG_FD2}>&2 >"$LOG_FILE" 2>&1
fi
}
|