Update nologs.py
Browse files
nologs.py
CHANGED
|
@@ -1,38 +1,37 @@
|
|
| 1 |
import subprocess
|
| 2 |
-
import sys
|
| 3 |
-
import logging
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
os.remove(log_file)
|
| 10 |
-
|
| 11 |
-
# 配置日志
|
| 12 |
-
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
-
|
| 14 |
-
# 创建一个将输出同时发送到控制台和日志文件的处理程序
|
| 15 |
-
console_handler = logging.StreamHandler(sys.stdout)
|
| 16 |
-
console_handler.setLevel(logging.INFO)
|
| 17 |
-
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
-
console_handler.setFormatter(formatter)
|
| 19 |
-
logging.getLogger().addHandler(console_handler)
|
| 20 |
-
|
| 21 |
-
# 重定向标准输出和标准错误到日志文件和控制台
|
| 22 |
-
sys.stdout = logging.getLogger().handlers[0].stream
|
| 23 |
-
sys.stderr = logging.getLogger().handlers[0].stream
|
| 24 |
-
|
| 25 |
-
try:
|
| 26 |
-
# 在try块中运行sd_clsa_webui_colab_ipynb_v2_3.py,并将输出重定向到日志文件
|
| 27 |
-
with open(log_file, 'a') as log_file_handle:
|
| 28 |
-
subprocess.run(['python', 'v2_3.py'], check=True, stdout=log_file_handle, stderr=subprocess.STDOUT)
|
| 29 |
-
except subprocess.CalledProcessError as e:
|
| 30 |
-
# 捕获异常并记录到日志文件
|
| 31 |
-
logging.exception(f"程序发生异常: {e}")
|
| 32 |
-
except Exception as e:
|
| 33 |
-
# 捕获其他异常并记录到日志文件
|
| 34 |
-
logging.exception(f"程序发生异常: {e}")
|
| 35 |
-
finally:
|
| 36 |
-
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
|
| 37 |
-
logging.getLogger().removeHandler(console_handler)
|
| 38 |
-
console_handler.close()
|
|
|
|
| 1 |
import subprocess
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
# 打开一个日志文件以追加方式写入
|
| 6 |
+
log_file = open("logs.txt", "a")
|
| 7 |
+
|
| 8 |
+
# 定义一个回调函数,用于处理每次输出
|
| 9 |
+
def process_output_line(line):
|
| 10 |
+
# 将输出写入日志文件
|
| 11 |
+
log_file.write(line)
|
| 12 |
+
log_file.flush() # 立即将数据刷新到文件,以确保实时记录
|
| 13 |
+
|
| 14 |
+
# 执行您的Python程序并将标准输出和标准错误重定向到回调函数
|
| 15 |
+
proc = subprocess.Popen(['python', 'v2_3.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
|
| 16 |
+
|
| 17 |
+
# 逐行处理程序的输出
|
| 18 |
+
for line in iter(proc.stdout.readline, ''):
|
| 19 |
+
# 打印输出到控制台,如果需要
|
| 20 |
+
sys.stdout.write(line)
|
| 21 |
+
sys.stdout.flush()
|
| 22 |
+
|
| 23 |
+
# 处理输出
|
| 24 |
+
process_output_line(line)
|
| 25 |
+
|
| 26 |
+
# 等待程序执行完成
|
| 27 |
+
proc.wait()
|
| 28 |
+
|
| 29 |
+
# 关闭日志文件
|
| 30 |
+
log_file.close()
|
| 31 |
+
|
| 32 |
+
# 获取程序的返回码
|
| 33 |
+
return_code = proc.returncode
|
| 34 |
|
| 35 |
+
# 如果程序返回非零退出码,打印错误信息
|
| 36 |
+
if return_code != 0:
|
| 37 |
+
print(f"程序返回非零退出码: {return_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|