Skip to content

Commit

Permalink
reckless: escape strings in log
Browse files Browse the repository at this point in the history
  • Loading branch information
endothermicdev committed Aug 6, 2024
1 parent d4fa4de commit 905c3cc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions tools/reckless
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ class Logger:
"log": []}
self.capture = capture

def str_esc(self, raw_string: str) -> str:
assert isinstance(raw_string, str)
return json.dumps(raw_string)[1:-1]

def debug(self, to_log: str):
assert isinstance(to_log, str) or hasattr(to_log, "__repr__")
if logging.root.level > logging.DEBUG:
return
if self.capture:
self.json_output['log'].append(f"DEBUG: {to_log}")
self.json_output['log'].append(self.str_esc(f"DEBUG: {to_log}"))
else:
logging.debug(to_log)

Expand All @@ -53,7 +57,7 @@ class Logger:
if logging.root.level > logging.INFO:
return
if self.capture:
self.json_output['log'].append(f"INFO: {to_log}")
self.json_output['log'].append(self.str_esc(f"INFO: {to_log}"))
else:
print(to_log)

Expand All @@ -62,7 +66,7 @@ class Logger:
if logging.root.level > logging.WARNING:
return
if self.capture:
self.json_output['log'].append(f"WARNING: {to_log}")
self.json_output['log'].append(self.str_esc(f"WARNING: {to_log}"))
else:
logging.warning(to_log)

Expand All @@ -71,7 +75,7 @@ class Logger:
if logging.root.level > logging.ERROR:
return
if self.capture:
self.json_output['log'].append(f"ERROR: {to_log}")
self.json_output['log'].append(self.str_esc(f"ERROR: {to_log}"))
else:
logging.error(to_log)

Expand Down

0 comments on commit 905c3cc

Please sign in to comment.