Performance tweaks

This commit is contained in:
Aaron Heise 2023-02-12 08:57:08 -06:00
parent 2e61b70996
commit 7d711cde6d
3 changed files with 27 additions and 9 deletions

View File

@ -192,6 +192,9 @@ class TTYRestorer(contextlib.AbstractContextManager):
""" """
Set raw mode on tty Set raw mode on tty
""" """
if not self._fd:
return
tty.setraw(self._fd, termios.TCSADRAIN) tty.setraw(self._fd, termios.TCSADRAIN)
def current_attr(self) -> [any]: def current_attr(self) -> [any]:
@ -199,14 +202,22 @@ class TTYRestorer(contextlib.AbstractContextManager):
Get the current termios attributes for the wrapped fd. Get the current termios attributes for the wrapped fd.
:return: attribute array :return: attribute array
""" """
return termios.tcgetattr(self._fd).copy() if not self._fd:
return None
return termios.tcgetattr(self._fd)
def set_attr(self, attr: [any], when: int = termios.TCSANOW): def set_attr(self, attr: [any], when: int = termios.TCSANOW):
""" """
Set termios attributes Set termios attributes
:param attr: attribute list to set :param attr: attribute list to set
:param when: when attributes should be applied (termios.TCSANOW, termios.TCSADRAIN, termios.TCSAFLUSH)
""" """
termios.tcsetattr(self._fd, when, attr) if not attr or not self._fd:
return
with contextlib.suppress(termios.error):
termios.tcsetattr(self._fd, when, attr)
def restore(self): def restore(self):
""" """
@ -216,8 +227,7 @@ class TTYRestorer(contextlib.AbstractContextManager):
def __exit__(self, __exc_type: typing.Type[BaseException], __exc_value: BaseException, def __exit__(self, __exc_type: typing.Type[BaseException], __exc_value: BaseException,
__traceback: types.TracebackType) -> bool: __traceback: types.TracebackType) -> bool:
with contextlib.suppress(termios.error): self.restore()
self.restore()
return __exc_type is not None and issubclass(__exc_type, termios.error) return __exc_type is not None and issubclass(__exc_type, termios.error)

View File

@ -115,7 +115,8 @@ async def _listen(configdir, command, identitypath=None, service_name="default",
log = _get_logger("_listen") log = _get_logger("_listen")
_cmd = command _cmd = command
targetloglevel = 3 + verbosity - quietness targetloglevel = RNS.LOG_INFO + verbosity - quietness
rnslogging.RnsHandler.set_global_log_level(__logging.INFO)
_reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel) _reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
_prepare_identity(identitypath) _prepare_identity(identitypath)
_destination = RNS.Destination(_identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, service_name) _destination = RNS.Destination(_identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, service_name)
@ -636,7 +637,8 @@ async def _execute(configdir, identitypath=None, verbosity=0, quietness=0, noid=
raise RemoteExecutionError("Invalid destination entered. Check your input.") raise RemoteExecutionError("Invalid destination entered. Check your input.")
if _reticulum is None: if _reticulum is None:
targetloglevel = 2 + verbosity - quietness targetloglevel = RNS.LOG_ERROR + verbosity - quietness
rnslogging.RnsHandler.set_global_log_level(__logging.ERROR)
_reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel) _reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
if _identity is None: if _identity is None:

View File

@ -60,6 +60,11 @@ class RnsHandler(Handler):
return RNS.LOG_DEBUG return RNS.LOG_DEBUG
return RNS.LOG_DEBUG return RNS.LOG_DEBUG
@classmethod
def set_global_log_level(cls, log_level: int):
logging.getLogger().setLevel(log_level)
RNS.loglevel = cls.get_rns_loglevel(log_level)
def emit(self, record): def emit(self, record):
""" """
Emit a record. Emit a record.
@ -109,16 +114,17 @@ def _rns_log(msg, level=3, _override_destination=False):
nonlocal msg, level, _override_destination nonlocal msg, level, _override_destination
try: try:
with process.TTYRestorer(sys.stdin.fileno()) as tr: with process.TTYRestorer(sys.stdin.fileno()) as tr:
attr = tr.current_attr() attr = tr.current_attr()
if attr:
attr[process.TTYRestorer.ATTR_IDX_OFLAG] = attr[process.TTYRestorer.ATTR_IDX_OFLAG] | \ attr[process.TTYRestorer.ATTR_IDX_OFLAG] = attr[process.TTYRestorer.ATTR_IDX_OFLAG] | \
termios.ONLRET | termios.ONLCR | termios.OPOST termios.ONLRET | termios.ONLCR | termios.OPOST
tr.set_attr(attr) tr.set_attr(attr)
_rns_log_orig(msg, level, _override_destination) _rns_log_orig(msg, level, _override_destination)
except ValueError: except ValueError:
_rns_log_orig(msg, level, _override_destination) _rns_log_orig(msg, level, _override_destination)
try: try:
if _loop: if _loop and _loop.is_running():
_loop.call_soon_threadsafe(_rns_log_inner) _loop.call_soon_threadsafe(_rns_log_inner)
else: else:
_rns_log_inner() _rns_log_inner()