From b7119231cf1b3d658e1462a5b006630eba13a77a Mon Sep 17 00:00:00 2001 From: Zak V Date: Mon, 7 Jun 2021 17:48:41 -0400 Subject: [PATCH 1/4] setup_logging.py is now more Jupyter-friendly. --- labscript_utils/setup_logging.py | 38 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/labscript_utils/setup_logging.py b/labscript_utils/setup_logging.py index df97448..001d0bd 100644 --- a/labscript_utils/setup_logging.py +++ b/labscript_utils/setup_logging.py @@ -11,6 +11,7 @@ # # ##################################################################### import sys, os +from io import UnsupportedOperation import logging, logging.handlers from labscript_utils.ls_zprocess import Handler, ensure_connected_to_zlog @@ -52,21 +53,26 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. handler.setFormatter(formatter) handler.setLevel(log_level) logger.addHandler(handler) - if sys.stdout is not None and sys.stdout.fileno() >= 0: - stdout_handler = logging.StreamHandler(sys.stdout) - stdout_handler.setFormatter(formatter) - stdout_handler.setLevel(terminal_level) - logger.addHandler(stdout_handler) - if sys.stderr is not None and sys.stderr.fileno() >= 0: - # Send warnings and greater to stderr instead of stdout: - stdout_handler.addFilter(LessThanFilter(logging.WARNING)) - sterr_handler = logging.StreamHandler(sys.stderr) - sterr_handler.setFormatter(formatter) - sterr_handler.setLevel(logging.WARNING) - logger.addHandler(sterr_handler) - else: - # Prevent bug on windows where writing to stdout without a command - # window causes a crash: - sys.stdout = sys.stderr = open(os.devnull, 'w') + try: + if sys.stdout is not None and sys.stdout.fileno() >= 0: + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setFormatter(formatter) + stdout_handler.setLevel(terminal_level) + logger.addHandler(stdout_handler) + if sys.stderr is not None and sys.stderr.fileno() >= 0: + # Send warnings and greater to stderr instead of stdout: + stdout_handler.addFilter(LessThanFilter(logging.WARNING)) + sterr_handler = logging.StreamHandler(sys.stderr) + sterr_handler.setFormatter(formatter) + sterr_handler.setLevel(logging.WARNING) + logger.addHandler(sterr_handler) + else: + # Prevent bug on windows where writing to stdout without a command + # window causes a crash: + sys.stdout = sys.stderr = open(os.devnull, 'w') + except UnsupportedOperation: + # Special handling for Jupyter notebook kernels where sys.stdout.fileno is not + # callable. + pass logger.setLevel(logging.DEBUG) return logger From 97e70c7d4faa83d0f0fb9462891fd16c3c8c1006 Mon Sep 17 00:00:00 2001 From: Zak V Date: Tue, 9 Nov 2021 19:56:07 -0500 Subject: [PATCH 2/4] Added warning message when setup_logging.setup_logging() cannot send output to stdout and stderr. --- labscript_utils/setup_logging.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/labscript_utils/setup_logging.py b/labscript_utils/setup_logging.py index 001d0bd..a579342 100644 --- a/labscript_utils/setup_logging.py +++ b/labscript_utils/setup_logging.py @@ -13,6 +13,7 @@ import sys, os from io import UnsupportedOperation import logging, logging.handlers +import warnings from labscript_utils.ls_zprocess import Handler, ensure_connected_to_zlog @@ -73,6 +74,8 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. except UnsupportedOperation: # Special handling for Jupyter notebook kernels where sys.stdout.fileno is not # callable. - pass + warnings.warn( + "Logging to stdout and stderr is disabled. See the log files for log messages." + ) logger.setLevel(logging.DEBUG) return logger From 2e381071508dca12f8b2e017f5c13d0cd7984a5e Mon Sep 17 00:00:00 2001 From: Zak V Date: Tue, 9 Nov 2021 20:04:37 -0500 Subject: [PATCH 3/4] Restructured try/except block in setup_logging.setup_logging() to avoid catching any UnsupportedOperation errors thrown by code other than sys.stdout.fileno(). --- labscript_utils/setup_logging.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/labscript_utils/setup_logging.py b/labscript_utils/setup_logging.py index a579342..8c37ee6 100644 --- a/labscript_utils/setup_logging.py +++ b/labscript_utils/setup_logging.py @@ -55,6 +55,16 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. handler.setLevel(log_level) logger.addHandler(handler) try: + # Check that sys.stdout.fileno is callable, which is needed below. It is NOT + # callable in Jupyter notebooks. + sys.stdout.fileno() + except UnsupportedOperation: + # In this case the code is likely being run from a Jupyter notebook, warn the + # user that log messages won't be printed to stdout or stderr. + warnings.warn( + "Logging to stdout and stderr is disabled. See the log files for log messages." + ) + else: if sys.stdout is not None and sys.stdout.fileno() >= 0: stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) @@ -71,11 +81,5 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. # Prevent bug on windows where writing to stdout without a command # window causes a crash: sys.stdout = sys.stderr = open(os.devnull, 'w') - except UnsupportedOperation: - # Special handling for Jupyter notebook kernels where sys.stdout.fileno is not - # callable. - warnings.warn( - "Logging to stdout and stderr is disabled. See the log files for log messages." - ) logger.setLevel(logging.DEBUG) return logger From c4dd2a83bdcf9b7318007a3a9970a599ba50f70c Mon Sep 17 00:00:00 2001 From: Zak V Date: Tue, 9 Nov 2021 20:12:19 -0500 Subject: [PATCH 4/4] Removed duplicate call to sys.stdout.fileno() in setup_logging.setup_logging(). Probably best practice to call it just once rather than calling it first to check if it raises an error then again later to actually use the value it returns. --- labscript_utils/setup_logging.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/labscript_utils/setup_logging.py b/labscript_utils/setup_logging.py index 8c37ee6..aee409d 100644 --- a/labscript_utils/setup_logging.py +++ b/labscript_utils/setup_logging.py @@ -57,7 +57,7 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. try: # Check that sys.stdout.fileno is callable, which is needed below. It is NOT # callable in Jupyter notebooks. - sys.stdout.fileno() + stdout_fileno = sys.stdout.fileno() except UnsupportedOperation: # In this case the code is likely being run from a Jupyter notebook, warn the # user that log messages won't be printed to stdout or stderr. @@ -65,7 +65,7 @@ def setup_logging(program_name, log_level=logging.DEBUG, terminal_level=logging. "Logging to stdout and stderr is disabled. See the log files for log messages." ) else: - if sys.stdout is not None and sys.stdout.fileno() >= 0: + if sys.stdout is not None and stdout_fileno >= 0: stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) stdout_handler.setLevel(terminal_level) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy