improve: better logging

This commit is contained in:
Dr.Lt.Data 2023-09-19 20:15:32 +09:00
parent b31931333e
commit 604a5d7574
2 changed files with 77 additions and 50 deletions

View File

@ -55,7 +55,7 @@ sys.path.append('../..')
from torchvision.datasets.utils import download_url from torchvision.datasets.utils import download_url
# ensure .js # ensure .js
print("### Loading: ComfyUI-Manager (V0.28.9)") print("### Loading: ComfyUI-Manager (V0.29)")
comfy_ui_required_revision = 1240 comfy_ui_required_revision = 1240
comfy_ui_revision = "Unknown" comfy_ui_revision = "Unknown"
@ -320,7 +320,7 @@ def git_repo_has_updates(path, do_fetch=False, do_update=False):
return False return False
except Exception as e: 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 # Get commit hash of the remote branch
remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha 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): def check_custom_nodes_installed(json_obj, do_fetch=False, do_update_check=True, do_update=False):
if do_fetch: if do_fetch:
print("Start fetching...") print("Start fetching...", end="")
elif do_update: elif do_update:
print("Start updating...") print("Start updating...", end="")
elif do_update_check: elif do_update_check:
print("Start update check...") print("Start update check...", end="")
for item in json_obj['custom_nodes']: for item in json_obj['custom_nodes']:
check_a_custom_node_installed(item, do_fetch, do_update_check, do_update) 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) os.remove(temp_filename)
except Exception as e: except Exception as e:
print(f"Install(unzip) error: {url} / {e}") print(f"Install(unzip) error: {url} / {e}", file=sys.stderr)
return False return False
print("Installation was successful.") print("Installation was successful.")
@ -685,7 +685,7 @@ def download_url_with_agent(url, save_path):
f.write(data) f.write(data)
except Exception as e: except Exception as e:
print(f"Download error: {url} / {e}") print(f"Download error: {url} / {e}", file=sys.stderr)
return False return False
print("Installation was successful.") print("Installation was successful.")
@ -704,7 +704,7 @@ def copy_install(files, js_path_name=None):
download_url(url, path) download_url(url, path)
except Exception as e: except Exception as e:
print(f"Install(copy) error: {url} / {e}") print(f"Install(copy) error: {url} / {e}", file=sys.stderr)
return False return False
print("Installation was successful.") print("Installation was successful.")
@ -723,7 +723,7 @@ def copy_uninstall(files, js_path_name='.'):
elif os.path.exists(file_path + ".disabled"): elif os.path.exists(file_path + ".disabled"):
os.remove(file_path + ".disabled") os.remove(file_path + ".disabled")
except Exception as e: except Exception as e:
print(f"Uninstall(copy) error: {url} / {e}") print(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr)
return False return False
print("Uninstallation was successful.") print("Uninstallation was successful.")
@ -752,7 +752,7 @@ def copy_set_active(files, is_disable, js_path_name='.'):
os.rename(current_name, new_name) os.rename(current_name, new_name)
except Exception as e: 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 return False
@ -802,7 +802,7 @@ def gitclone_install(files):
return False return False
except Exception as e: 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 return False
print("Installation was successful.") print("Installation was successful.")
@ -871,7 +871,7 @@ def gitclone_uninstall(files):
elif os.path.exists(dir_path + ".disabled"): elif os.path.exists(dir_path + ".disabled"):
rmtree(dir_path + ".disabled") rmtree(dir_path + ".disabled")
except Exception as e: 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 return False
print("Uninstallation was successful.") print("Uninstallation was successful.")
@ -916,7 +916,7 @@ def gitclone_set_active(files, is_disable):
try_install_script(url, new_path, enable_script) try_install_script(url, new_path, enable_script)
except Exception as e: 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 return False
print(f"{action_name} was successful.") print(f"{action_name} was successful.")
@ -937,7 +937,7 @@ def gitclone_update(files):
return False return False
except Exception as e: 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 return False
print("Update was successful.") print("Update was successful.")
@ -1054,7 +1054,7 @@ async def install_custom_node(request):
else: else:
return web.Response(status=200) return web.Response(status=200)
except Exception as e: except Exception as e:
print(f"ComfyUI update fail: {e}") print(f"ComfyUI update fail: {e}", file=sys.stderr)
pass pass
return web.Response(status=400) return web.Response(status=400)
@ -1106,7 +1106,7 @@ async def install_model(request):
if res: if res:
return web.json_response({}, content_type='application/json') return web.json_response({}, content_type='application/json')
except Exception as e: except Exception as e:
print(f"[ERROR] {e}") print(f"[ERROR] {e}", file=sys.stderr)
pass pass
return web.Response(status=400) return web.Response(status=400)

View File

@ -6,52 +6,79 @@ import atexit
import threading import threading
import re import re
# Logger setup try:
if os.path.exists("comfyui.log"): # Logger setup
if os.path.exists("comfyui.prev.log"): if os.path.exists("comfyui.log"):
os.remove("comfyui.prev.log") if os.path.exists("comfyui.prev.log"):
os.rename("comfyui.log", "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_stdout = sys.stdout
original_stderr = sys.stderr original_stderr = sys.stderr
tqdm = r'\d+%.*\[(.*?)\]' tqdm = r'\d+%.*\[(.*?)\]'
class Logger: log_file = open("comfyui.log", "w", encoding="utf-8")
def __init__(self, filename): log_lock = threading.Lock()
self.file = open(filename, "w", encoding="utf-8")
def write(self, message): class Logger:
self.file.write(message) def __init__(self, is_stdout):
self.file.flush() self.is_stdout = is_stdout
match = re.search(tqdm, message) def write(self, message):
if match: if not self.is_stdout:
message = re.sub(r'([#|])\d', r'\1▌', message) match = re.search(tqdm, message)
message = re.sub('#', '', message) if match:
original_stderr.write(message) message = re.sub(r'([#|])\d', r'\1▌', message)
original_stderr.flush() message = re.sub('#', '', message)
else: if '100%' in message:
original_stdout.write(message) self.sync_write(message)
original_stdout.flush() else:
original_stderr.write(message)
original_stderr.flush()
else:
self.sync_write(message)
else:
self.sync_write(message)
def flush(self): def sync_write(self, message):
self.file.flush() with log_lock:
original_stdout.flush() log_file.write(message)
log_file.flush()
def close_file(self): if self.is_stdout:
self.file.close() 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): def handle_stream(stream, prefix):
for line in stream: for line in stream:
print(prefix, line, end="") print(prefix, line, end="")
sys.stdout = Logger("comfyui.log") def close_log():
sys.stderr = sys.stdout 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}")
atexit.register(sys.stdout.close_file)
print("** ComfyUI start up time:", datetime.datetime.now()) print("** ComfyUI start up time:", datetime.datetime.now())