Skip to content

Commit

Permalink
Logging cleanup (#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheng Lundquist authored May 23, 2024
1 parent a172ff1 commit b73e7fd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
4 changes: 3 additions & 1 deletion scripts/local_fuzz_bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def main(argv: Sequence[str] | None = None) -> None:
chain_port = 33333
db_port = 44444

local_chain_config = LocalChain.Config(chain_port=chain_port, db_port=db_port, block_timestamp_interval=12)
local_chain_config = LocalChain.Config(
chain_port=chain_port, db_port=db_port, block_timestamp_interval=12, log_level=logging.WARNING
)

while True:
# Build interactive local hyperdrive
Expand Down
58 changes: 48 additions & 10 deletions src/agent0/chainsync/analysis/calc_position_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
import os
from decimal import Decimal

import pandas as pd
Expand All @@ -12,6 +13,39 @@
from agent0.ethpy.hyperdrive.state import PoolState


# Define a context manager to suppress stdout and stderr.
# We keep this as camel case due to it being a context manager
# pylint: disable=invalid-name
class _suppress_stdout_stderr:
"""A context manager for doing a "deep suppression" of stdout and stderr in
Python, i.e. will suppress all print, even if the print originates in a
compiled C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed
to stderr just before a script exits, and after the context manager has
exited (at least, I think that is why it lets exceptions through).
"""

def __init__(self):
# Open a pair of null files
self.null_fds = [os.open(os.devnull, os.O_RDWR) for x in range(2)]
# Save the actual stdout (1) and stderr (2) file descriptors.
self.save_fds = [os.dup(1), os.dup(2)]

def __enter__(self):
# Assign the null pointers to stdout and stderr.
os.dup2(self.null_fds[0], 1)
os.dup2(self.null_fds[1], 2)

def __exit__(self, *_):
# Re-assign the real stdout/stderr back to (1) and (2)
os.dup2(self.save_fds[0], 1)
os.dup2(self.save_fds[1], 2)
# Close all file descriptors
for fd in self.null_fds + self.save_fds:
os.close(fd)


def calc_single_closeout(
position: pd.Series,
interface: HyperdriveReadInterface,
Expand Down Expand Up @@ -50,10 +84,12 @@ def calc_single_closeout(
out_value = Decimal("nan")
if token_type == "LONG":
try:
out_value = interface.calc_close_long(amount, maturity, hyperdrive_state)
# Suppress any errors coming from rust here, we already log it as info
with _suppress_stdout_stderr():
out_value = interface.calc_close_long(amount, maturity, hyperdrive_state)
# Rust Panic Exceptions are base exceptions, not Exceptions
except BaseException as exception: # pylint: disable=broad-except
logging.warning("Chainsync: Exception caught in calculating close long, ignoring: %s", exception)
logging.info("Chainsync: Exception caught in calculating close long, ignoring: %s", exception)
# FixedPoint to Decimal
out_value = Decimal(str(out_value))

Expand All @@ -79,16 +115,18 @@ def calc_single_closeout(
close_share_price = hyperdrive_state.pool_info.vault_share_price

try:
out_value = interface.calc_close_short(
amount,
open_vault_share_price=open_share_price,
close_vault_share_price=close_share_price,
maturity_time=maturity,
pool_state=hyperdrive_state,
)
# Suppress any errors coming from rust here, we already log it as info
with _suppress_stdout_stderr():
out_value = interface.calc_close_short(
amount,
open_vault_share_price=open_share_price,
close_vault_share_price=close_share_price,
maturity_time=maturity,
pool_state=hyperdrive_state,
)
# Rust Panic Exceptions are base exceptions, not Exceptions
except BaseException as exception: # pylint: disable=broad-except
logging.warning("Chainsync: Exception caught in calculating close short, ignoring: %s", exception)
logging.info("Chainsync: Exception caught in calculating close short, ignoring: %s", exception)
out_value = Decimal(str(out_value))

# For PNL, we assume all withdrawal shares are redeemable
Expand Down
10 changes: 9 additions & 1 deletion src/agent0/hypertypes/utilities/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,16 @@ def fixedpoint_to_checkpoint(
Checkpoint
A dataclass containing the checkpoint vault_share_price and exposure fields converted to integers.
"""
fixedpoint_keys = [
"weighted_spot_price",
"vault_share_price",
]

return Checkpoint(
**{snake_to_camel(key): value.scaled_value for key, value in asdict(fixedpoint_checkpoint).items()}
**{
snake_to_camel(key): value.scaled_value if key in fixedpoint_keys else value
for key, value in asdict(fixedpoint_checkpoint).items()
}
)


Expand Down

0 comments on commit b73e7fd

Please sign in to comment.