From 604a5d75749ddfb3ad91c26b7e99bc17e9805481 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 19 Sep 2023 20:15:32 +0900 Subject: [PATCH] improve: better logging --- __init__.py | 32 ++++++------ prestartup_script.py | 117 ++++++++++++++++++++++++++----------------- 2 files changed, 88 insertions(+), 61 deletions(-) diff --git a/__init__.py b/__init__.py index b641f31..0668f03 100644 --- a/__init__.py +++ b/__init__.py @@ -55,7 +55,7 @@ sys.path.append('../..') from torchvision.datasets.utils import download_url # ensure .js -print("### Loading: ComfyUI-Manager (V0.28.9)") +print("### Loading: ComfyUI-Manager (V0.29)") comfy_ui_required_revision = 1240 comfy_ui_revision = "Unknown" @@ -320,7 +320,7 @@ def git_repo_has_updates(path, do_fetch=False, do_update=False): return False except Exception as e: - print(f"Updating failed: {path}\n{e}") + print(f"\nUpdating failed: {path}\n{e}", file=sys.stderr) # Get commit hash of the remote branch remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha @@ -491,11 +491,11 @@ def check_a_custom_node_installed(item, do_fetch=False, do_update_check=True, do def check_custom_nodes_installed(json_obj, do_fetch=False, do_update_check=True, do_update=False): if do_fetch: - print("Start fetching...") + print("Start fetching...", end="") elif do_update: - print("Start updating...") + print("Start updating...", end="") elif do_update_check: - print("Start update check...") + print("Start update check...", end="") for item in json_obj['custom_nodes']: check_a_custom_node_installed(item, do_fetch, do_update_check, do_update) @@ -662,7 +662,7 @@ def unzip_install(files): os.remove(temp_filename) except Exception as e: - print(f"Install(unzip) error: {url} / {e}") + print(f"Install(unzip) error: {url} / {e}", file=sys.stderr) return False print("Installation was successful.") @@ -685,7 +685,7 @@ def download_url_with_agent(url, save_path): f.write(data) except Exception as e: - print(f"Download error: {url} / {e}") + print(f"Download error: {url} / {e}", file=sys.stderr) return False print("Installation was successful.") @@ -704,7 +704,7 @@ def copy_install(files, js_path_name=None): download_url(url, path) except Exception as e: - print(f"Install(copy) error: {url} / {e}") + print(f"Install(copy) error: {url} / {e}", file=sys.stderr) return False print("Installation was successful.") @@ -723,7 +723,7 @@ def copy_uninstall(files, js_path_name='.'): elif os.path.exists(file_path + ".disabled"): os.remove(file_path + ".disabled") except Exception as e: - print(f"Uninstall(copy) error: {url} / {e}") + print(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr) return False print("Uninstallation was successful.") @@ -752,7 +752,7 @@ def copy_set_active(files, is_disable, js_path_name='.'): os.rename(current_name, new_name) except Exception as e: - print(f"{action_name}(copy) error: {url} / {e}") + print(f"{action_name}(copy) error: {url} / {e}", file=sys.stderr) return False @@ -802,7 +802,7 @@ def gitclone_install(files): return False except Exception as e: - print(f"Install(git-clone) error: {url} / {e}") + print(f"Install(git-clone) error: {url} / {e}", file=sys.stderr) return False print("Installation was successful.") @@ -871,7 +871,7 @@ def gitclone_uninstall(files): elif os.path.exists(dir_path + ".disabled"): rmtree(dir_path + ".disabled") except Exception as e: - print(f"Uninstall(git-clone) error: {url} / {e}") + print(f"Uninstall(git-clone) error: {url} / {e}", file=sys.stderr) return False print("Uninstallation was successful.") @@ -916,7 +916,7 @@ def gitclone_set_active(files, is_disable): try_install_script(url, new_path, enable_script) except Exception as e: - print(f"{action_name}(git-clone) error: {url} / {e}") + print(f"{action_name}(git-clone) error: {url} / {e}", file=sys.stderr) return False print(f"{action_name} was successful.") @@ -937,7 +937,7 @@ def gitclone_update(files): return False except Exception as e: - print(f"Update(git-clone) error: {url} / {e}") + print(f"Update(git-clone) error: {url} / {e}", file=sys.stderr) return False print("Update was successful.") @@ -1054,7 +1054,7 @@ async def install_custom_node(request): else: return web.Response(status=200) except Exception as e: - print(f"ComfyUI update fail: {e}") + print(f"ComfyUI update fail: {e}", file=sys.stderr) pass return web.Response(status=400) @@ -1106,7 +1106,7 @@ async def install_model(request): if res: return web.json_response({}, content_type='application/json') except Exception as e: - print(f"[ERROR] {e}") + print(f"[ERROR] {e}", file=sys.stderr) pass return web.Response(status=400) diff --git a/prestartup_script.py b/prestartup_script.py index 11eb2e4..2098add 100644 --- a/prestartup_script.py +++ b/prestartup_script.py @@ -6,52 +6,79 @@ import atexit import threading import re -# Logger setup -if os.path.exists("comfyui.log"): - if os.path.exists("comfyui.prev.log"): - os.remove("comfyui.prev.log") - os.rename("comfyui.log", "comfyui.prev.log") +try: + # Logger setup + if os.path.exists("comfyui.log"): + if os.path.exists("comfyui.prev.log"): + if os.path.exists("comfyui.prev2.log"): + os.remove("comfyui.prev2.log") + os.rename("comfyui.prev.log", "comfyui.prev2.log") + os.rename("comfyui.log", "comfyui.prev.log") + + original_stdout = sys.stdout + original_stderr = sys.stderr + + tqdm = r'\d+%.*\[(.*?)\]' + + log_file = open("comfyui.log", "w", encoding="utf-8") + log_lock = threading.Lock() + + class Logger: + def __init__(self, is_stdout): + self.is_stdout = is_stdout + + def write(self, message): + if not self.is_stdout: + match = re.search(tqdm, message) + if match: + message = re.sub(r'([#|])\d', r'\1▌', message) + message = re.sub('#', '█', message) + if '100%' in message: + self.sync_write(message) + else: + original_stderr.write(message) + original_stderr.flush() + else: + self.sync_write(message) + else: + self.sync_write(message) + + def sync_write(self, message): + with log_lock: + log_file.write(message) + log_file.flush() + + if self.is_stdout: + original_stdout.write(message) + original_stdout.flush() + else: + original_stderr.write(message) + original_stderr.flush() + + def flush(self): + log_file.flush() + if self.is_stdout: + original_stdout.flush() + else: + original_stderr.flush() + + + def handle_stream(stream, prefix): + for line in stream: + print(prefix, line, end="") + + + def close_log(): + log_file.close() + + + sys.stdout = Logger(True) + sys.stderr = Logger(False) + + atexit.register(close_log) +except Exception as e: + print(f"[ComfyUI-Manager] Logging failed: {e}") -original_stdout = sys.stdout -original_stderr = sys.stderr - -tqdm = r'\d+%.*\[(.*?)\]' - -class Logger: - def __init__(self, filename): - self.file = open(filename, "w", encoding="utf-8") - - def write(self, message): - self.file.write(message) - self.file.flush() - - match = re.search(tqdm, message) - if match: - message = re.sub(r'([#|])\d', r'\1▌', message) - message = re.sub('#', '█', message) - original_stderr.write(message) - original_stderr.flush() - else: - original_stdout.write(message) - original_stdout.flush() - - def flush(self): - self.file.flush() - original_stdout.flush() - - def close_file(self): - self.file.close() - - -def handle_stream(stream, prefix): - for line in stream: - print(prefix, line, end="") - - -sys.stdout = Logger("comfyui.log") -sys.stderr = sys.stdout - -atexit.register(sys.stdout.close_file) print("** ComfyUI start up time:", datetime.datetime.now())