Skip to content

Commit 07bce64

Browse files
Write os::log messages to a file if possible
In order to allow for the output to `std{out,err}` to be as minimal as possible without losing fidelity on our logs, we will make more use of `os::log::debug` without setting `$OS_DEBUG`. Now, all logging functions also try to write to a log file under `$LOG_DIR`, so we will be able to inspect the log when looking for more verbose information. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent ee48a99 commit 07bce64

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

hack/lib/log/output.sh

+30-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
# Arguments:
99
# - all: message to write
1010
function os::log::info() {
11-
os::log::internal::prefix_lines "[INFO]" "$*"
11+
local message; message="$( os::log::internal::prefix_lines "[INFO]" "$*" )"
12+
os::log::internal::to_logfile "${message}"
13+
echo "${message}"
1214
}
1315
readonly -f os::log::info
1416

@@ -19,7 +21,9 @@ readonly -f os::log::info
1921
# Arguments:
2022
# - all: message to write
2123
function os::log::warn() {
22-
os::text::print_yellow "$( os::log::internal::prefix_lines "[WARNING]" "$*" )" 1>&2
24+
local message; message="$( os::log::internal::prefix_lines "[WARNING]" "$*" )"
25+
os::log::internal::to_logfile "${message}"
26+
os::text::print_yellow "${message}" 1>&2
2327
}
2428
readonly -f os::log::warn
2529

@@ -30,7 +34,9 @@ readonly -f os::log::warn
3034
# Arguments:
3135
# - all: message to write
3236
function os::log::error() {
33-
os::text::print_red "$( os::log::internal::prefix_lines "[ERROR]" "$*" )" 1>&2
37+
local message; message="$( os::log::internal::prefix_lines "[ERROR]" "$*" )"
38+
os::log::internal::to_logfile "${message}"
39+
os::text::print_red "${message}" 1>&2
3440
}
3541
readonly -f os::log::error
3642

@@ -42,23 +48,42 @@ readonly -f os::log::error
4248
# Arguments:
4349
# - all: message to write
4450
function os::log::fatal() {
45-
os::text::print_red "$( os::log::internal::prefix_lines "[FATAL]" "$*" )" 1>&2
51+
local message; message="$( os::log::internal::prefix_lines "[FATAL]" "$*" )"
52+
os::log::internal::to_logfile "${message}"
53+
os::text::print_red "${message}" 1>&2
4654
exit 1
4755
}
4856
readonly -f os::log::fatal
4957

5058
# os::log::debug writes the message to stderr if
5159
# the ${OS_DEBUG} variable is set.
5260
#
61+
# Globals:
62+
# - OS_DEBUG
5363
# Arguments:
5464
# - all: message to write
5565
function os::log::debug() {
66+
local message; message="$( os::log::internal::prefix_lines "[DEBUG]" "$*" )"
67+
os::log::internal::to_logfile "${message}"
5668
if [[ -n "${OS_DEBUG:-}" ]]; then
57-
os::text::print_blue "$( os::log::internal::prefix_lines "[DEBUG]" "$*" )" 1>&2
69+
os::text::print_blue "${message}" 1>&2
5870
fi
5971
}
6072
readonly -f os::log::debug
6173

74+
# os::log::internal::to_logfile makes a best-effort
75+
# attempt to write the message to the script logfile
76+
#
77+
# Globals:
78+
# - LOG_DIR
79+
# Arguments:
80+
# - all: message to write
81+
function os::log::internal::to_logfile() {
82+
if [[ -n "${LOG_DIR:-}" ]]; then
83+
echo "$*" >>"${LOG_DIR}/scripts.log"
84+
fi
85+
}
86+
6287
# os::log::internal::prefix_lines prints out the
6388
# original content with the given prefix at the
6489
# start of every line.

0 commit comments

Comments
 (0)