refactor: make log/observer nice

This commit is contained in:
Dobin
2024-03-29 12:32:24 +00:00
parent 32f89a1b20
commit a46e0e4f13
7 changed files with 132 additions and 139 deletions
+12 -28
View File
@@ -45,50 +45,34 @@ def run_process_checkret(args, check=True):
logger.warn("Caught KeyboardInterrupt, exiting gracefully...")
except subprocess.CalledProcessError as e:
logger.warn(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")
# Handle the error case
except Exception as e:
logger.warn(f"An error occurred: {e}")
# Handle other exceptions
logger.warn(f"An error occurred executing {e}")
# handle output
stdout_s = ""
stdout_b = b""
if ret.stdout != None:
stdout_b = ret.stdout
stdout_s = ret.stdout.decode('utf-8')
stderr_s = ""
stderr_b = b""
if ret.stderr != None:
stderr_b = ret.stderr
stderr_s = ret.stderr.decode('utf-8')
# write directly to logs/ dir
with open(f"{logs_dir}/cmdoutput.log", "ab") as f:
cmd = "------------------------------------\n"
cmd += "--- " + " ".join(args) + "\n"
f.write(cmd.encode('utf-8'))
observer.add_cmd_output(stdout_s)
f.write(stdout_b)
observer.add_cmd_output(stderr_s)
f.write(stderr_b)
# log it
observer.add_cmd_output(">>> {}\n".format(" ".join(args)))
for line in stdout_s.split("\n"):
observer.add_cmd_output(line)
for line in stderr_s.split("\n"):
observer.add_cmd_output(line)
# check return code (optional)
if ret.returncode != 0 and check:
logger.info("----! FAILED Command: {}".format(" ".join(args)))
if ret.stdout != None:
observer.add_cmd_output(stdout_s)
logger.info(stdout_s)
if ret.stderr != None:
observer.add_cmd_output(stderr_s)
logger.info(stderr_s)
raise Exception("Command failed: " + " ".join(args))
# debug: show command output
if config.ShowCommandOutput:
logger.info("> " + " ".join(args))
if ret.stdout != None:
logger.info(stdout_s)
if ret.stderr != None:
logger.info(stderr_s)
logger.info(">>> " + " ".join(args))
logger.info(stdout_s)
logger.info(stderr_s)
def try_start_shellcode(shc_file):