Skip to content

Commit

Permalink
feat: 添加错误日志跟踪记录上传模块
Browse files Browse the repository at this point in the history
  • Loading branch information
fishros committed Sep 6, 2024
1 parent cd8f55f commit 2929a9e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 21 deletions.
52 changes: 44 additions & 8 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
# 将工具信息添加到相应类型的列表中
tool_categories[tool_type][tool_id]=tool_info


tracking = None
def main():
# download base
url_prefix = os.environ.get('FISHROS_URL','http://mirror.fishros.com/install')
os.system("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(base_url,base_url.replace(url_prefix,'')))

from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask,ChooseWithCategoriesTask
from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask,ChooseWithCategoriesTask,Tracking
from tools.base import encoding_utf8,osversion,osarch
from tools.base import run_tool_file,download_tools
from tools.base import config_helper,tr
Expand All @@ -66,6 +66,10 @@ def main():
CmdTask("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(translator_url,translator_url.replace(url_prefix,''))).run()
importlib.import_module("tools.translation.translator").Linguist()
from tools.base import tr
import copy

global tracing
tracing = copy.copy(Tracking)


# 使用量统计
Expand All @@ -87,7 +91,7 @@ def main():
print('Solutions: https://fishros.org.cn/forum/topic/24 ')
return False
PrintUtils.print_success(tr.tr("基础检查通过..."))

book = tr.tr("""
.-~~~~~~~~~-._ _.-~~~~~~~~~-.
__.' ~. .~ `.__
Expand All @@ -110,8 +114,6 @@ def main():
""")
PrintUtils.print_delay(tip,0.001)
PrintUtils.print_delay(book,0.001)


# download tools
code,result = ChooseWithCategoriesTask(tool_categories, tips=tr.tr("---众多工具,等君来用---"),categories=tools_type_map).run()
if code==0: PrintUtils().print_success(tr.tr("是觉得没有合胃口的菜吗?那快联系的小鱼增加菜单吧~"))
Expand All @@ -121,8 +123,42 @@ def main():
config_helper.gen_config_file()

PrintUtils.print_delay(tr.tr("欢迎加入机器人学习交流QQ群:438144612(入群口令:一键安装)"),0.1)
PrintUtils.print_success(tr.tr("鱼香小铺正式开业,最低499可入手一台能建图会导航的移动机器人,淘宝搜店:鱼香ROS 或打开链接查看:https://item.taobao.com/item.htm?id=696573635888"),0.001)
PrintUtils.print_success(tr.tr("如在使用过程中遇到问题,请打开:https://fishros.org.cn/forum 进行反馈"),0.001)
PrintUtils.print_delay(tr.tr("鱼香小铺正式开业,最低499可入手一台能建图会导航的移动机器人,淘宝搜店:鱼香ROS 或打开链接查看:https://item.taobao.com/item.htm?id=696573635888"),0.001)
PrintUtils.print_delay(tr.tr("如在使用过程中遇到问题,请打开:https://fishros.org.cn/forum 进行反馈"),0.001)

if __name__=='__main__':
main()
run_exc = []

try:
main()
except Exception as e:
import traceback
print('\r\n检测到程序发生异常退出,请打开:https://fishros.org.cn/forum 携带如下内容进行反馈\n\n')
print("标题:使用一键安装过程中遇到程序崩溃")
print("```")
traceback.print_exc()
run_exc.append(traceback.format_exc())
print("```")
print('本次运行详细日志文件已保存至 /tmp/fishros_install.log')

try:
with open("/tmp/fishros_install.log", "w", encoding="utf-8") as f:
for exec in run_exc:
print(exec, file=f) # 打印异常输出到文件中
for text,end in tracing.logs:
print(text, file=f,end=end) # 打印输出到文件中
for text in tracing.err_logs:
print(text, file=f) # 打印输出到文件中
if tracing.need_report:
print("")
input('检测到本次运行出现失败命令,直接退出按Ctrl+C,按任意键上传日志并退出\n')
ret = os.system("""curl -s -F "file=@/tmp/fishros_install.log" http://103.226.124.73:5000/upload > /tmp/fishros_upload 2>&1""")
if ret == 0:
with open("/tmp/fishros_upload","r") as f:
print("错误日志上传成功,反馈码:",f.read())
else:
print("日志上传失败,若还需反馈请手动发帖!")
except:
pass


58 changes: 45 additions & 13 deletions tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,32 +781,60 @@ def get_codename(self):

osversion = GetOsVersion()

class Tracking():
"""
日志跟踪模块
"""
logs = []
err_logs = []
need_report = False
def put_log(values,end=""):
Tracking.logs.append((values,end))

def put_cmd_result(code,out,err,command):
if code!=0:
Tracking.need_report = True
Tracking.err_logs.append("Execute Command: {} Error Code{}".format(command,code))
Tracking.err_logs.append('====================OUT====================')
for line in out:
Tracking.err_logs.append(line)
Tracking.err_logs.append('====================ERR====================')
for line in err:
Tracking.err_logs.append(line)


class PrintUtils():

@staticmethod
def print_delay(data,delay=0.03,end="\n"):
PrintUtils.print_text("\033[37m",end="")
for d in data:
d = d.encode("utf-8").decode("utf-8")
print("\033[37m{}".format(d),end="",flush=True)
PrintUtils.print_text("{}".format(d),end="",flush=True)
time.sleep(delay)
print(end=end)
PrintUtils.print_text(end=end)

@staticmethod
def print_error(data,end="\n"):
print("\033[31m{}\033[37m".format(data),end=end)
PrintUtils.print_text("\033[31m{}\033[37m".format(data),end=end)

@staticmethod
def print_info(data,end="\n"):
print("\033[37m{}".format(data))
PrintUtils.print_text("\033[37m{}".format(data),end=end)

@staticmethod
def print_success(data,end="\n"):
print("\033[32m{}\033[37m".format(data))
PrintUtils.print_text("\033[32m{}\033[37m".format(data),end=end)

@staticmethod
def print_warn(data,end="\n"):
print("\033[33m{}\033[37m".format(data))

PrintUtils.print_text("\033[33m{}\033[37m".format(data),end=end)

@staticmethod
def print_text(values: object="",end: str | None = "\n",flush= False):
print(values,end=end,flush=flush)
Tracking.put_log(values,end=end)

@staticmethod
def print_fish(timeout=1,scale=30):
return
Expand All @@ -820,6 +848,9 @@ def print_fish(timeout=1,scale=30):
time.sleep(timeout/scale)
print("\n")




class Task():
"""
- type: 任务类型
Expand Down Expand Up @@ -855,13 +886,13 @@ def __init__(self,timeout=10,scale=20) -> None:

def update(self,log=""):
if (self.i%4) == 0:
print('\r[/][{:.2f}s] {}'.format(self.dur,log),end="")
PrintUtils.print_text('\r[/][{:.2f}s] {}'.format(self.dur,log),end="")
elif(self.i%4) == 1:
print('\r[\\][{:.2f}s] {}'.format(self.dur,log),end="")
PrintUtils.print_text('\r[\\][{:.2f}s] {}'.format(self.dur,log),end="")
elif (self.i%4) == 2:
print('\r[|][{:.2f}s] {}'.format(self.dur,log),end="")
PrintUtils.print_text('\r[|][{:.2f}s] {}'.format(self.dur,log),end="")
elif (self.i%4) == 3:
print('\r[-][{:.2f}s] {}'.format(self.dur,log),end="")
PrintUtils.print_text('\r[-][{:.2f}s] {}'.format(self.dur,log),end="")
sys.stdout.flush()
self.i += 1
# update time
Expand All @@ -884,7 +915,7 @@ def update_time(self):

def finsh(self,log="",color='\033[32m'):
log = log+" "*(Progress.line_width-len(log)-15)
print('\r{}[-][{:.2f}s] {}'.format(color,self.dur, log), end="\r\n\r\n")
PrintUtils.print_text('\r{}[-][{:.2f}s] {}'.format(color,self.dur, log), end="\r\n\r\n")



Expand Down Expand Up @@ -981,6 +1012,7 @@ def run_command(self,executable='/bin/sh'):
self.bar.update_time()
time.sleep(0.1)

Tracking.put_cmd_result(self.ret_code,self.ret_out,self.ret_err,self.command)
return (self.ret_code,self.ret_out,self.ret_err)

def is_command_finish(self):
Expand Down Expand Up @@ -1034,7 +1066,7 @@ def __choose(data,tips,array):
while True:
if choose_item:
choose = str(choose_item['choose'])
print(tr.tr("为您从配置文件找到默认选项:"),choose_item)
PrintUtils.print_text(tr.tr("为您从配置文件找到默认选项:"),choose_item)
else:
choose = input(tr.tr("请输入[]内的数字以选择:"))
choose_item = None
Expand Down

0 comments on commit 2929a9e

Please sign in to comment.