(core) Update logging in sandbox code, and log tracebacks as single log messages.

Summary:
- Replace logger module by the standard module 'logging'.
- When a log message from the sandbox includes newlines (e.g. for tracebacks),
  keep those lines together in the Node log message.

  Previously each line was a different message, making it difficult to view
  tracebacks, particularly in prod where each line becomes a separate message
  object.

- Fix assorted lint errors.

Test Plan: Added a test for the log-line splitting and escaping logic.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3956
This commit is contained in:
Dmitry S
2023-07-18 11:20:02 -04:00
parent 7fd48364df
commit 534615dd50
46 changed files with 154 additions and 249 deletions

View File

@@ -10,10 +10,13 @@ Usage:
"""
import os
import logging
import marshal
import sys
import traceback
log = logging.getLogger(__name__)
class CarefulReader(object):
"""
Wrap a pipe when reading from Pyodide, to work around marshaling
@@ -40,9 +43,6 @@ class CarefulReader(object):
def __getattr__(self, attr):
return getattr(self._file, attr)
def log(msg):
sys.stderr.write(str(msg) + "\n")
sys.stderr.flush()
class Sandbox(object):
"""
@@ -93,6 +93,7 @@ class Sandbox(object):
@classmethod
def use_pyodide(cls):
# pylint: disable=import-error,no-member
import js # Get pyodide object.
external_input = CarefulReader(sys.stdin.buffer)
external_output_method = lambda data: js.sendFromSandbox(data)
@@ -146,7 +147,7 @@ class Sandbox(object):
ret = self._functions[fname](*args)
self._send_to_js(Sandbox.DATA, ret)
except Exception as e:
traceback.print_exc()
log.warn("Call error in %s: %s", fname, traceback.format_exc())
self._send_to_js(Sandbox.EXC, "%s %s" % (type(e).__name__, e))
if break_on_response:
raise Exception("Sandbox disconnected unexpectedly")