Central class to store all logs and files created during the build process
| 8 | |
| 9 | |
| 10 | class Observer(): |
| 11 | """Central class to store all logs and files created during the build process""" |
| 12 | |
| 13 | def __init__(self): |
| 14 | self.cmd_output = [] # output of external programs (cmdoutput.log) |
| 15 | self.logs: List[str] = [] # internal log messages (supermega.log) |
| 16 | self.files = [] # content of generated files |
| 17 | self.active = True |
| 18 | |
| 19 | |
| 20 | def reset(self): |
| 21 | self.cmd_output = [] |
| 22 | self.logs = [] |
| 23 | self.files = [] |
| 24 | self.idx = 0 |
| 25 | |
| 26 | |
| 27 | def add_cmd_output(self, cmd_output): |
| 28 | self.cmd_output.append(cmd_output) |
| 29 | |
| 30 | |
| 31 | def get_cmd_output(self): |
| 32 | return self.cmd_output |
| 33 | |
| 34 | |
| 35 | def add_log(self, log: str): |
| 36 | self.logs.append(log) |
| 37 | |
| 38 | |
| 39 | def get_logs(self): |
| 40 | return self.logs |
| 41 | |
| 42 | |
| 43 | def add_text_file(self, name, data): |
| 44 | self.files.append((name + ".txt", data)) |
| 45 | |
| 46 | |
| 47 | def add_code_file(self, name, data: bytes): |
| 48 | if not config.has_r2: |
| 49 | return |
| 50 | |
| 51 | ret = r2_disas(data) |
| 52 | self.files.append((name + ".disas.ascii", ret['color'])) |
| 53 | #self.write_to_file(name + ".disas.txt", ret['text']) |
| 54 | #self.write_to_file(name + ".disas.ascii", ret['color']) |
| 55 | #self.write_to_file(name + ".hex", ret['hexdump']) |
| 56 | #self.write_to_file_bin(name + ".bin", data) |
| 57 | |
| 58 | |
| 59 | def write_logs(self, working_dir: str): |
| 60 | # Our log output |
| 61 | with open(f"{working_dir}log-supermega.log", "w") as f: |
| 62 | for line in observer.get_logs(): |
| 63 | try: |
| 64 | f.write(line + "\n") |
| 65 | except Exception as e: |
| 66 | logger.warning("Error: {}".format(e)) |
| 67 |