From a2b293e3c950b40e5f5644031a715d9ccc128e77 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Fri, 3 Jan 2025 22:43:56 +0000 Subject: [PATCH 1/3] stdenv: introduce nixLog and nixLogWithLevel --- pkgs/stdenv/generic/setup.sh | 72 ++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 541689287970..19a836b80b05 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -56,60 +56,100 @@ getAllOutputNames() { fi } +# All provided arguments are joined with a space, then prefixed by the name of the function which invoked `nixLog` (or +# the hook name if the caller was an implicit hook), then directed to $NIX_LOG_FD, if it's set. +nixLog() { + # Return a value explicitly instead of the implicit return of the last command (result of the test). + [[ -z ${NIX_LOG_FD-} ]] && return 0 + + # Use the function name of the caller, unless it is _callImplicitHook, in which case use the name of the hook. + local callerName="${FUNCNAME[1]}" + if [[ $callerName == "_callImplicitHook" ]]; then + callerName="${hookName:?}" + fi + printf "%s: %s\n" "$callerName" "$*" >&"$NIX_LOG_FD" +} + +# Identical to nixLog, but additionally prefixed by the logLevel. +# NOTE: This function is only every meant to be called from the nix*Log family of functions. +nixLogWithLevel() { + # Return a value explicitly instead of the implicit return of the last command (result of the test). + [[ -z ${NIX_LOG_FD-} || ${NIX_DEBUG:-0} -lt ${1:?} ]] && return 0 + + local logLevel + case "${1:?}" in + 0) logLevel=ERROR ;; + 1) logLevel=WARN ;; + 2) logLevel=NOTICE ;; + 3) logLevel=INFO ;; + 4) logLevel=TALKATIVE ;; + 5) logLevel=CHATTY ;; + 6) logLevel=DEBUG ;; + 7) logLevel=VOMIT ;; + *) + echo "nixLogWithLevel: called with invalid log level: ${1:?}" >&"$NIX_LOG_FD" + return 1 + ;; + esac + + # Use the function name of the caller, unless it is _callImplicitHook, in which case use the name of the hook. + # NOTE: Our index into FUNCNAME is 2, not 1, because we are only ever to be called from the nix*Log family of + # functions, never directly. + local callerName="${FUNCNAME[2]}" + if [[ $callerName == "_callImplicitHook" ]]; then + callerName="${hookName:?}" + fi + + # Use the function name of the caller's caller, since we should only every be invoked by nix*Log functions. + printf "%s: %s: %s\n" "$logLevel" "$callerName" "${2:?}" >&"$NIX_LOG_FD" +} + # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlError` in the Nix source. nixErrorLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 0 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 0 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlWarn` in the Nix source. nixWarnLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 1 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 1 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlNotice` in the Nix source. nixNoticeLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 2 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 2 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlInfo` in the Nix source. nixInfoLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 3 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 3 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlTalkative` in the Nix source. nixTalkativeLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 4 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 4 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlChatty` in the Nix source. nixChattyLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 5 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 5 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlDebug` in the Nix source. nixDebugLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 6 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 6 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlVomit` in the Nix source. nixVomitLog() { - if [[ -z ${NIX_LOG_FD-} ]] || [[ ${NIX_DEBUG:-0} -lt 7 ]]; then return; fi - printf "%s\n" "$*" >&"$NIX_LOG_FD" + nixLogWithLevel 7 "$*" } # Log a hook, to be run before the hook is actually called. From 60749f9cdc9204dbecaa7ea56ff1fa584baee566 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 23 Jan 2025 04:11:44 +0000 Subject: [PATCH 2/3] stdenv: rename nixLogWithLevel to _nixLogWithLevel to indicate it is meant to be private --- pkgs/stdenv/generic/setup.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 19a836b80b05..3b99861009f6 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -72,7 +72,7 @@ nixLog() { # Identical to nixLog, but additionally prefixed by the logLevel. # NOTE: This function is only every meant to be called from the nix*Log family of functions. -nixLogWithLevel() { +_nixLogWithLevel() { # Return a value explicitly instead of the implicit return of the last command (result of the test). [[ -z ${NIX_LOG_FD-} || ${NIX_DEBUG:-0} -lt ${1:?} ]] && return 0 @@ -87,7 +87,7 @@ nixLogWithLevel() { 6) logLevel=DEBUG ;; 7) logLevel=VOMIT ;; *) - echo "nixLogWithLevel: called with invalid log level: ${1:?}" >&"$NIX_LOG_FD" + echo "_nixLogWithLevel: called with invalid log level: ${1:?}" >&"$NIX_LOG_FD" return 1 ;; esac @@ -107,49 +107,49 @@ nixLogWithLevel() { # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlError` in the Nix source. nixErrorLog() { - nixLogWithLevel 0 "$*" + _nixLogWithLevel 0 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlWarn` in the Nix source. nixWarnLog() { - nixLogWithLevel 1 "$*" + _nixLogWithLevel 1 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlNotice` in the Nix source. nixNoticeLog() { - nixLogWithLevel 2 "$*" + _nixLogWithLevel 2 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlInfo` in the Nix source. nixInfoLog() { - nixLogWithLevel 3 "$*" + _nixLogWithLevel 3 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlTalkative` in the Nix source. nixTalkativeLog() { - nixLogWithLevel 4 "$*" + _nixLogWithLevel 4 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlChatty` in the Nix source. nixChattyLog() { - nixLogWithLevel 5 "$*" + _nixLogWithLevel 5 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlDebug` in the Nix source. nixDebugLog() { - nixLogWithLevel 6 "$*" + _nixLogWithLevel 6 "$*" } # All provided arguments are joined with a space then directed to $NIX_LOG_FD, if it's set. # Corresponds to `Verbosity::lvlVomit` in the Nix source. nixVomitLog() { - nixLogWithLevel 7 "$*" + _nixLogWithLevel 7 "$*" } # Log a hook, to be run before the hook is actually called. From 8aa11323bb3b50e0cafba21511fd8d91158cffb3 Mon Sep 17 00:00:00 2001 From: Connor Baker Date: Thu, 23 Jan 2025 04:14:49 +0000 Subject: [PATCH 3/3] stdenv: add note in nix logging functions about not cluttering nix-shell --- pkgs/stdenv/generic/setup.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 3b99861009f6..4198f41aadde 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -60,6 +60,7 @@ getAllOutputNames() { # the hook name if the caller was an implicit hook), then directed to $NIX_LOG_FD, if it's set. nixLog() { # Return a value explicitly instead of the implicit return of the last command (result of the test). + # NOTE: By requiring NIX_LOG_FD be set, we avoid dumping logging inside of nix-shell. [[ -z ${NIX_LOG_FD-} ]] && return 0 # Use the function name of the caller, unless it is _callImplicitHook, in which case use the name of the hook. @@ -74,6 +75,7 @@ nixLog() { # NOTE: This function is only every meant to be called from the nix*Log family of functions. _nixLogWithLevel() { # Return a value explicitly instead of the implicit return of the last command (result of the test). + # NOTE: By requiring NIX_LOG_FD be set, we avoid dumping logging inside of nix-shell. [[ -z ${NIX_LOG_FD-} || ${NIX_DEBUG:-0} -lt ${1:?} ]] && return 0 local logLevel