Skip to content

Commit

Permalink
Fix journal redirect on systems without journal
Browse files Browse the repository at this point in the history
We redirect all the error messages from stderr to journal in the main
Anaconda process. This is mainly covering errors and warnings from
GTK UI.

However, this code returns FileNotFound on systems which don't have
journal enabled.

To fix this move this code under our existing logic for logging to
journal which is enabled only on HW installations.
  • Loading branch information
jkonecny12 committed Oct 14, 2024
1 parent 7c96bd8 commit c9f931f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
10 changes: 0 additions & 10 deletions anaconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@

from pyanaconda.modules.common.structures.rescue import RescueData

# Redirect Anaconda main process stderr to Journal,
# as otherwise this could end up writing all over
# the TUI on TTY1.

# create an appropriately named Journal writing stream
from systemd import journal
anaconda_stderr_stream = journal.stream("anaconda", priority=journal.LOG_ERR)
# redirect stderr of this process to the stream
os.dup2(anaconda_stderr_stream.fileno(), sys.stderr.fileno())


def exitHandler(rebootData):
# Clear the list of watched PIDs.
Expand Down
24 changes: 19 additions & 5 deletions pyanaconda/anaconda_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import logging
from logging.handlers import SysLogHandler, SocketHandler
from systemd.journal import JournalHandler
from systemd import journal
import os
import sys
import warnings
Expand Down Expand Up @@ -94,22 +94,22 @@ def __setattr__(self, name, value):
self._stream = WriteProxy() # pylint: disable=attribute-defined-outside-init


class AnacondaJournalHandler(_AnacondaLogFixer, JournalHandler):
class AnacondaJournalHandler(_AnacondaLogFixer, journal.JournalHandler):
def __init__(self, tag='', facility=ANACONDA_SYSLOG_FACILITY,
identifier=ANACONDA_SYSLOG_IDENTIFIER):
self.tag = tag
JournalHandler.__init__(self,
journal.JournalHandler.__init__(self,
SYSLOG_FACILITY=facility,
SYSLOG_IDENTIFIER=identifier)

def emit(self, record):
if self.tag:
original_msg = record.msg
record.msg = '%s: %s' % (self.tag, original_msg)
JournalHandler.emit(self, record)
journal.JournalHandler.emit(self, record)
record.msg = original_msg
else:
JournalHandler.emit(self, record)
journal.JournalHandler.emit(self, record)


class AnacondaSocketHandler(_AnacondaLogFixer, SocketHandler):
Expand Down Expand Up @@ -187,6 +187,9 @@ def __init__(self, write_to_journal=False):
self.addFileHandler(MAIN_LOG_FILE, simpleline_logger)
self.forwardToJournal(simpleline_logger)

# Redirect all stderr messages from process to journal
self.stderrToJournal()

# Create a second logger for just the stuff we want to dup on
# stdout. Anything written here will also get passed up to the
# parent loggers for processing and possibly be written to the
Expand Down Expand Up @@ -232,6 +235,17 @@ def forwardToJournal(self, logr, log_formatter=None, log_filter=None):
journal_handler.setFormatter(log_formatter)
logr.addHandler(journal_handler)

def stderrToJournal(self):
"""Print all stderr messages from Anaconda to journal instead.
Redirect Anaconda main process stderr to Journal, as otherwise this could end up writing
all over the TUI on TTY1.
"""
# create an appropriately named Journal writing stream
anaconda_stderr_stream = journal.stream("anaconda", priority=journal.LOG_ERR)
# redirect stderr of this process to the stream
os.dup2(anaconda_stderr_stream.fileno(), sys.stderr.fileno())

# pylint: disable=redefined-builtin
def showwarning(self, message, category, filename, lineno,
file=sys.stderr, line=None):
Expand Down

0 comments on commit c9f931f

Please sign in to comment.