Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to log to stdout #1473

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions printrun/pronterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,39 @@ def format_length(mm, fractional=2):
return '%%.%df' % fractional % units + suffix

class ConsoleOutputHandler:
"""Handle console output. All messages go through the logging submodule. We setup a logging handler to get logged messages and write them to both stdout (unless a log file path is specified, in which case we add another logging handler to write to this file) and the log panel.
We also redirect stdout and stderr to ourself to catch print messages and al."""
"""Handle console output. All messages go through the logging submodule. We
setup a logging handler to get logged messages and write them to both
stdout (unless a log file path is specified, in which case we add another
logging handler to write to this file) and the log panel. We also redirect
stdout and stderr to ourselves to catch print messages et al."""

def __init__(self, target, log_path):
def __init__(self, target, log_path, log_stdout):
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = self
sys.stderr = self
self.print_on_stdout = not log_path
if log_path:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was worried about removing this check, but in my tests I could not make it fail due to this removal. So I'm happy with it. But if you could double check I would appreciate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was redundant: there is already the same check in setup_logging()

setup_logging(self, log_path, reset_handlers = True)
else:
setup_logging(sys.stdout, reset_handlers = True)

self.print_on_stdout = log_stdout or not log_path
self.target = target

setup_logging(self, log_path, reset_handlers=True)

def __del__(self):
sys.stdout = self.stdout
sys.stderr = self.stderr

def write(self, data):
try:
self.target(data)
except:
pass
except Exception as e:
# This shouldn't generally happen, unless there is something very wrong with the code
self.stderr.write(_("An exception occurred during an attempt to log a message: {}").format(e))

if self.print_on_stdout:
self.stdout.write(data)

def flush(self):
if self.stdout:
if self.print_on_stdout:
self.stdout.flush()

class PronterWindow(MainWindow, pronsole.pronsole):
Expand Down Expand Up @@ -223,7 +227,7 @@ def __init__(self, app, filename = None, size = winsize):
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText(_("Not connected to printer."))

self.t = ConsoleOutputHandler(self.catchprint, self.settings.log_path)
self.t = ConsoleOutputHandler(self.catchprint, self.settings.log_path, self.settings.log_stdout)
self.stdout = sys.stdout
self.slicing = False
self.loading_gcode = False
Expand Down
4 changes: 3 additions & 1 deletion printrun/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ def __init__(self, root):
self._add(StringSetting("start_command", "", _("Start Command:"), _("Executable to run when the print is started"), "External"))
self._add(StringSetting("final_command", "", _("Final Command:"), _("Executable to run when the print is finished"), "External"))
self._add(StringSetting("error_command", "", _("Error Command:"), _("Executable to run when an error occurs"), "External"))
self._add(DirSetting("log_path", str(Path.home()), _("Log Path:"), _("Path to the log file. An empty path will log to the console."), "UI"))
self._add(DirSetting("log_path", str(Path.home()), _("Log Path:"),
_("Path to the log file. If the path is a directory the file will be named 'printrun.log'"), "UI"))
self._add(BooleanSetting("log_stdout", False, _("Log to console:"), _("Duplicate log messages to stdout"), "UI"))

self._add(HiddenSetting("project_offset_x", 0.0))
self._add(HiddenSetting("project_offset_y", 0.0))
Expand Down
Loading