From 813a1ce6b0392e6d2ce4804951edfa617664c86c Mon Sep 17 00:00:00 2001 From: "dr.lt.data" Date: Mon, 29 Jan 2024 10:40:12 +0900 Subject: [PATCH] feat: optional file_logging fix: robust file_logging https://github.com/ltdrdata/ComfyUI-Manager/issues/375 --- README.md | 4 +++ __init__.py | 7 ++-- prestartup_script.py | 78 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 69 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 5190a1c8..fafeae1c 100644 --- a/README.md +++ b/README.md @@ -255,8 +255,12 @@ NODE_CLASS_MAPPINGS.update({ ## Additional Feature +* Logging to file feature + * This feature is enabled by default and can be disabled by setting `file_logging = False` in the `config.ini`. + * Fix node(recreate): When right-clicking on a node and selecting `Fix node (recreate)`, you can recreate the node. The widget's values are reset, while the connections maintain those with the same names. * It is used to correct errors in nodes of old workflows created before, which are incompatible with the version changes of custom nodes. + * Double-Click Node Title: You can set the double click behavior of nodes in the ComfyUI-Manager menu. * `Copy All Connections`, `Copy Input Connections`: Double-clicking a node copies the connections of the nearest node. * This action targets the nearest node within a straight-line distance of 1000 pixels from the center of the node. diff --git a/__init__.py b/__init__.py index ce7aa9dc..75add7ff 100644 --- a/__init__.py +++ b/__init__.py @@ -29,7 +29,7 @@ except: print(f"[WARN] ComfyUI-Manager: Your ComfyUI version is outdated. Please update to the latest version.") -version = [2, 5, 2] +version = [2, 6] version_str = f"V{version[0]}.{version[1]}" + (f'.{version[2]}' if len(version) > 2 else '') print(f"### Loading: ComfyUI-Manager ({version_str})") @@ -173,6 +173,7 @@ def write_config(): 'channel_url': get_config()['channel_url'], 'share_option': get_config()['share_option'], 'bypass_ssl': get_config()['bypass_ssl'], + "file_logging": get_config()['file_logging'], 'default_ui': get_config()['default_ui'], 'component_policy': get_config()['component_policy'], 'double_click_policy': get_config()['double_click_policy'], @@ -195,10 +196,11 @@ def read_config(): 'channel_url': default_conf['channel_url'] if 'channel_url' in default_conf else 'https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main', 'share_option': default_conf['share_option'] if 'share_option' in default_conf else 'all', 'bypass_ssl': default_conf['bypass_ssl'] if 'bypass_ssl' in default_conf else False, + 'file_logging': default_conf['file_logging'] if 'file_logging' in default_conf else True, 'default_ui': default_conf['default_ui'] if 'default_ui' in default_conf else 'none', 'component_policy': default_conf['component_policy'] if 'component_policy' in default_conf else 'workflow', 'double_click_policy': default_conf['double_click_policy'] if 'double_click_policy' in default_conf else 'copy-all', - "windows_selector_event_loop_policy": default_conf['windows_selector_event_loop_policy'] if 'windows_selector_event_loop_policy' in default_conf else False, + 'windows_selector_event_loop_policy': default_conf['windows_selector_event_loop_policy'] if 'windows_selector_event_loop_policy' in default_conf else False, } except Exception: @@ -209,6 +211,7 @@ def read_config(): 'channel_url': 'https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main', 'share_option': 'all', 'bypass_ssl': False, + 'file_logging': True, 'default_ui': 'none', 'component_policy': 'workflow', 'double_click_policy': 'copy-all', diff --git a/prestartup_script.py b/prestartup_script.py index 985da775..31c445e8 100644 --- a/prestartup_script.py +++ b/prestartup_script.py @@ -18,6 +18,7 @@ import cm_global message_collapses = [] import_failed_extensions = set() cm_global.variables['cm.on_revision_detected_handler'] = [] +enable_file_logging = True def register_message_collapse(f): @@ -30,6 +31,24 @@ def is_import_failed_extension(name): return name in import_failed_extensions +def check_file_logging(): + global enable_file_logging + try: + import configparser + config_path = os.path.join(os.path.dirname(__file__), "config.ini") + config = configparser.ConfigParser() + config.read(config_path) + default_conf = config['default'] + + if 'file_logging' in default_conf and default_conf['file_logging'].lower() == 'false': + enable_file_logging = False + except Exception: + pass + + +check_file_logging() + + sys.__comfyui_manager_register_message_collapse = register_message_collapse sys.__comfyui_manager_is_import_failed_extension = is_import_failed_extension cm_global.register_api('cm.register_message_collapse', register_message_collapse) @@ -118,16 +137,34 @@ try: postfix = "" # Logger setup - if os.path.exists(f"comfyui{postfix}.log"): - if os.path.exists(f"comfyui{postfix}.prev.log"): - if os.path.exists(f"comfyui{postfix}.prev2.log"): - os.remove(f"comfyui{postfix}.prev2.log") - os.rename(f"comfyui{postfix}.prev.log", f"comfyui{postfix}.prev2.log") - os.rename(f"comfyui{postfix}.log", f"comfyui{postfix}.prev.log") + if enable_file_logging: + if os.path.exists(f"comfyui{postfix}.log"): + if os.path.exists(f"comfyui{postfix}.prev.log"): + if os.path.exists(f"comfyui{postfix}.prev2.log"): + os.remove(f"comfyui{postfix}.prev2.log") + os.rename(f"comfyui{postfix}.prev.log", f"comfyui{postfix}.prev2.log") + os.rename(f"comfyui{postfix}.log", f"comfyui{postfix}.prev.log") + + log_file = open(f"comfyui{postfix}.log", "w", encoding="utf-8", errors="ignore") + + log_lock = threading.Lock() original_stdout = sys.stdout original_stderr = sys.stderr + if original_stdout.encoding.lower() == 'utf-8': + write_stdout = original_stdout.write + write_stderr = original_stderr.write + else: + def wrapper_stdout(msg): + original_stdout.write(msg.encode('utf-8').decode(original_stdout.encoding, errors="ignore")) + + def wrapper_stderr(msg): + original_stderr.write(msg.encode('utf-8').decode(original_stderr.encoding, errors="ignore")) + + write_stdout = wrapper_stdout + write_stderr = wrapper_stderr + pat_tqdm = r'\d+%.*\[(.*?)\]' pat_import_fail = r'seconds \(IMPORT FAILED\):' pat_custom_node = r'[/\\]custom_nodes[/\\](.*)$' @@ -135,9 +172,6 @@ try: is_start_mode = True is_import_fail_mode = False - log_file = open(f"comfyui{postfix}.log", "w", encoding="utf-8", errors="ignore") - log_lock = threading.Lock() - class ComfyUIManagerLogger: def __init__(self, is_stdout): self.is_stdout = is_stdout @@ -185,7 +219,7 @@ try: if '100%' in message: self.sync_write(message) else: - original_stderr.write(message) + write_stderr(message) original_stderr.flush() else: self.sync_write(message) @@ -204,11 +238,11 @@ try: with std_log_lock: if self.is_stdout: - original_stdout.write(message) + write_stdout(message) original_stdout.flush() terminal_hook.write_stderr(message) else: - original_stderr.write(message) + write_stderr(message) original_stderr.flush() terminal_hook.write_stdout(message) @@ -237,11 +271,16 @@ try: sys.stderr = original_stderr sys.stdout = original_stdout log_file.close() - - sys.stdout = ComfyUIManagerLogger(True) - sys.stderr = ComfyUIManagerLogger(False) - atexit.register(close_log) + + if enable_file_logging: + sys.stdout = ComfyUIManagerLogger(True) + sys.stderr = ComfyUIManagerLogger(False) + + atexit.register(close_log) + else: + sys.stdout.close_log = lambda: None + except Exception as e: print(f"[ComfyUI-Manager] Logging failed: {e}") @@ -250,7 +289,11 @@ print("** ComfyUI startup time:", datetime.datetime.now()) print("** Platform:", platform.system()) print("** Python version:", sys.version) print("** Python executable:", sys.executable) -print("** Log path:", os.path.abspath('comfyui.log')) + +if enable_file_logging: + print("** Log path:", os.path.abspath('comfyui.log')) +else: + print("** Log path: file logging is disabled") def check_bypass_ssl(): @@ -461,7 +504,6 @@ del pip_list def check_windows_event_loop_policy(): try: import configparser - import ssl config_path = os.path.join(os.path.dirname(__file__), "config.ini") config = configparser.ConfigParser() config.read(config_path)