Skip to content

New function to redirect the $LOG_FD fd to a changed STDOUT#178

Merged
frenck merged 2 commits intohassio-addons:mainfrom
lmagyar:pr-reinitialize-log-output
Apr 10, 2026
Merged

New function to redirect the $LOG_FD fd to a changed STDOUT#178
frenck merged 2 commits intohassio-addons:mainfrom
lmagyar:pr-reinitialize-log-output

Conversation

@lmagyar
Copy link
Copy Markdown
Contributor

@lmagyar lmagyar commented Nov 8, 2025

Proposed Changes

Based on my comment in #174 (#174 (comment))

This is useful in scripts that are not called by S6 and have their STDOUT and STDERR not initialized (ie. redirected to /dev/null) and they have to redirect their STDOUT and STDERR to eg. /proc/1/fd/1. This is valid for eg. Docker healthcheck scripts, some daemons started by S6 services (eg. in case of NetworkManager dispatcher only STDERR has valid value, but crond scripts are started with valid STDOUT and STDERR).

Possible enhancements:

  1. Maybe we can extend it with adding exec &>/proc/1/fd/1 before/outside the if? Add-on scripts can log/write only here. In this case this would be a general reinitialization for scripts not started by S6. This would be a useful addition.

  2. Or we can hide this function completely: if the shebang references bashio, bashio checks that STDOUT and STDERR has valid, non /dev/null values, and if any of them is /dev/null, redirects them to /proc/1/fd/1 automatically? This can be a breaking change for some corner cases.

Related Issues

Summary by CodeRabbit

  • New Features
    • Added the ability to reinitialize logging output so log messages can be redirected to the current standard output. This ensures logs remain visible and correctly routed after external changes to output streams, improving reliability of logging across scripts and runtime adjustments.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 8, 2025

Walkthrough

A new public function bashio::log.reinitialize_output() was added to lib/log.sh. It validates that LOG_FD is numeric and, if so, rebinds that file descriptor to the current STDOUT using an exec redirect (errors are suppressed).

Changes

Cohort / File(s) Change Summary
Log Output Reinitialization
lib/log.sh
Added public function bashio::log.reinitialize_output() that checks whether LOG_FD is a numeric file descriptor and rebinds it to the current STDOUT using an eval-based exec redirect, with error suppression.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller
    participant LogLib as bashio::log.reinitialize_output()
    participant Shell as Shell FD Table

    Caller->>LogLib: call reinitialize_output()
    LogLib->>LogLib: is LOG_FD numeric?
    alt LOG_FD is numeric
        LogLib->>Shell: exec {LOG_FD}>&1  (rebind to current STDOUT)
        Shell-->>LogLib: result (success / failure suppressed)
        LogLib-->>Caller: return (no error output)
    else not numeric
        LogLib-->>Caller: no-op (returns)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Single new function in one file; straightforward conditional and exec usage.
  • Review focus:
    • Correctness of numeric validation for LOG_FD.
    • Safety of eval-based exec and proper error suppression.

Possibly related PRs

Suggested reviewers

  • frenck

Poem

🐰 I nudged a FD back to the sunlit stream,
Where stdout dances and loglines gleam.
A quiet exec, a gentle rebound,
Now echoes of output are easily found. ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: a new function that redirects the LOG_FD file descriptor to a changed STDOUT, which aligns with the added public function bashio::log.reinitialize_output().
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f4a682 and b3804d8.

📒 Files selected for processing (1)
  • lib/log.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/log.sh

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
lib/log.sh (1)

27-31: Consider adding fd writeability validation for consistency and robustness.

The function safely validates that LOG_FD is numeric before using it in eval, which is correct. However, the validation is less strict than the LOG_FD initialization at line 11, which also checks that the fd is actually writable using { : >&"${LOG_FD-2}"; }. If LOG_FD is set to a numeric value for a closed or invalid file descriptor, the exec will silently fail without indication.

While this may be acceptable if callers are expected to verify state, consider matching the validation rigor of line 11 for robustness:

function bashio::log.reinitialize_output() {
    if [[ "${LOG_FD-}" =~ ^[0-9]+$ ]] && { : >&"${LOG_FD}"; } 2>/dev/null; then
        eval "exec ${LOG_FD}>&1"
    fi
}

This ensures the fd exists and is writable before attempting redirection.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f788a0b and 6f4a682.

📒 Files selected for processing (1)
  • lib/log.sh (1 hunks)

@lmagyar

This comment was marked as resolved.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 8, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Nov 8, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 8, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 9, 2025

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Dec 9, 2025
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Dec 9, 2025

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Dec 10, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 9, 2026

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Jan 9, 2026
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Jan 9, 2026

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Jan 10, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 9, 2026

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Feb 9, 2026
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Feb 9, 2026

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Feb 10, 2026
@github-actions
Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Mar 13, 2026
@lmagyar
Copy link
Copy Markdown
Contributor Author

lmagyar commented Mar 13, 2026

not stale

@github-actions github-actions bot removed the stale There has not been activity on this issue or PR for quite some time. label Mar 14, 2026
@frenck frenck added the new-feature New features or options. label Apr 7, 2026
@frenck frenck requested a review from Copilot April 7, 2026 13:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a helper to realign Bashio logging output when a script changes its STDOUT after LOG_FD has already been established, so subsequent bashio::log.* calls follow the new STDOUT.

Changes:

  • Introduces bashio::log.reinitialize_output() to redirect the existing LOG_FD to the current STDOUT.
  • Adds documentation/comments describing when add-on developers should call this function.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @lmagyar 👍

../Frenck

                       

Blogging my personal ramblings at frenck.dev

@frenck frenck merged commit ac92cda into hassio-addons:main Apr 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New features or options.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants