mirror of
https://github.com/dobin/SuperMega
synced 2026-06-02 17:27:10 +00:00
refactor: make log/observer nice
This commit is contained in:
+47
-40
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import pprint
|
||||
from capstone import Cs, CS_ARCH_X86, CS_MODE_64
|
||||
from typing import List, Dict
|
||||
|
||||
from pe.r2helper import r2_disas
|
||||
from utils import delete_all_files_in_directory
|
||||
@@ -8,68 +9,74 @@ from model.defs import *
|
||||
|
||||
|
||||
class Observer():
|
||||
"""Central class to store all logs and files created during the build process"""
|
||||
|
||||
def __init__(self):
|
||||
self.cmd_output = []
|
||||
self.logs = []
|
||||
self.idx = 0
|
||||
self.cmd_output = [] # output of external programs (cmdoutput.log)
|
||||
self.logs: List[str] = [] # internal log messages (supermega.log)
|
||||
self.files = [] # content of generated files
|
||||
self.active = True
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.cmd_output = []
|
||||
self.logs = []
|
||||
self.idx = 0
|
||||
|
||||
|
||||
def add_cmd_output(self, cmd_output):
|
||||
self.cmd_output.append(cmd_output)
|
||||
|
||||
def add_log(self, log):
|
||||
|
||||
def get_cmd_output(self):
|
||||
return self.cmd_output
|
||||
|
||||
|
||||
def add_log(self, log: str):
|
||||
self.logs.append(log)
|
||||
|
||||
|
||||
def get_logs(self):
|
||||
return self.logs
|
||||
|
||||
def writelog(self):
|
||||
# write log to file
|
||||
with open(f"{logs_dir}/supermega.log", "w") as f:
|
||||
for line in self.logs:
|
||||
f.write(line + "\n")
|
||||
|
||||
def add_text(self, name, data):
|
||||
self.write_to_file(name + ".txt", data)
|
||||
self.idx += 1
|
||||
def add_text_file(self, name, data):
|
||||
self.files.append((name + ".txt", data))
|
||||
|
||||
def add_code(self, name, data: bytes):
|
||||
|
||||
def add_code_file(self, name, data: bytes):
|
||||
ret = r2_disas(data)
|
||||
self.write_to_file(name + ".disas.txt", ret['text'])
|
||||
self.write_to_file(name + ".disas.ascii", ret['color'])
|
||||
self.write_to_file(name + ".hex", ret['hexdump'])
|
||||
self.write_to_file_bin(name + ".bin", data)
|
||||
self.idx += 1
|
||||
self.files.append((name + ".disas.ascii", ret['color']))
|
||||
#self.write_to_file(name + ".disas.txt", ret['text'])
|
||||
#self.write_to_file(name + ".disas.ascii", ret['color'])
|
||||
#self.write_to_file(name + ".hex", ret['hexdump'])
|
||||
#self.write_to_file_bin(name + ".bin", data)
|
||||
#self.idx += 1
|
||||
|
||||
def add_json(self, name, data):
|
||||
self.write_to_file(name, pprint.pformat(data, indent=4))
|
||||
self.idx += 1
|
||||
|
||||
def write_to_file(self, filename, data):
|
||||
if not self.active:
|
||||
return
|
||||
with open("{}/{}-{}".format(logs_dir, self.idx, filename), "w") as f:
|
||||
f.write(data)
|
||||
def write_to_file_bin(self, filename, data):
|
||||
if not self.active:
|
||||
return
|
||||
with open("{}/{}-{}".format(logs_dir, self.idx, filename), "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
def clean_files(self):
|
||||
delete_all_files_in_directory(f"{logs_dir}/")
|
||||
self.idx = 0
|
||||
self.logs = []
|
||||
|
||||
#def write_to_file(self, filename, data):
|
||||
# if not self.active:
|
||||
# return
|
||||
# with open("{}/{}-{}".format(logs_dir, self.idx, filename), "w") as f:
|
||||
# f.write(data)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
s = "<todo>"
|
||||
return s
|
||||
#def write_to_file_bin(self, filename, data):
|
||||
# if not self.active:
|
||||
# return
|
||||
# with open("{}/{}-{}".format(logs_dir, self.idx, filename), "wb") as f:
|
||||
# f.write(data)
|
||||
|
||||
|
||||
#def clean_files(self):
|
||||
# delete_all_files_in_directory(f"{logs_dir}/")
|
||||
# self.idx = 0
|
||||
# self.logs = []
|
||||
|
||||
|
||||
#def __str__(self):
|
||||
# s = "<todo>"
|
||||
# return s
|
||||
|
||||
|
||||
observer = Observer()
|
||||
Reference in New Issue
Block a user