Skip to content

Commit

Permalink
Merge branch 'staging' into feat/roman/substrate-definition-before-in…
Browse files Browse the repository at this point in the history
…itialization
  • Loading branch information
roman-opentensor committed Oct 1, 2024
2 parents 51c03bd + 27bf3db commit e294815
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 25 deletions.
21 changes: 0 additions & 21 deletions bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@
from .utils.deprecated import *


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def __getattr__(name):
if name == "version_split":
warnings.warn(
Expand Down
4 changes: 2 additions & 2 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def __init__(
logging.info(
f"You are connecting to {self.network} network with endpoint {self.chain_endpoint}."
)
logging.warning(
logging.debug(
"We strongly encourage running a local subtensor node whenever possible. "
"This increases decentralization and resilience of the network."
)
logging.warning(
logging.debug(
"In a future release, local subtensor will become the default endpoint. "
"To get ahead of this change, please run a local subtensor node and point to it."
)
Expand Down
65 changes: 63 additions & 2 deletions bittensor/utils/btlogging/loggingmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,48 @@ class LoggingMachine(StateMachine, Logger):
Debug = State()
Trace = State()
Disabled = State()
Warning = State()

enable_default = (
Debug.to(Default)
| Trace.to(Default)
| Disabled.to(Default)
| Default.to(Default)
| Warning.to(Default)
)

enable_info = enable_default

enable_trace = (
Default.to(Trace) | Debug.to(Trace) | Disabled.to(Trace) | Trace.to(Trace)
Default.to(Trace)
| Debug.to(Trace)
| Disabled.to(Trace)
| Trace.to(Trace)
| Warning.to(Trace)
)

enable_debug = (
Default.to(Debug) | Trace.to(Debug) | Disabled.to(Debug) | Debug.to(Debug)
Default.to(Debug)
| Trace.to(Debug)
| Disabled.to(Debug)
| Debug.to(Debug)
| Warning.to(Debug)
)

enable_warning = (
Default.to(Warning)
| Trace.to(Warning)
| Disabled.to(Warning)
| Debug.to(Warning)
| Warning.to(Warning)
)

disable_trace = Trace.to(Default)

disable_debug = Debug.to(Default)

disable_warning = Warning.to(Default)

disable_logging = (
Trace.to(Disabled)
| Debug.to(Disabled)
Expand Down Expand Up @@ -308,9 +330,29 @@ def before_enable_default(self):
continue
logger.setLevel(stdlogging.CRITICAL)

def before_enable_info(self):
"""Logs status before enable Default."""
self._logger.info(f"Enabling default logging.")
self._logger.setLevel(stdlogging.INFO)
for logger in all_loggers():
if logger.name in self._primary_loggers:
continue
logger.setLevel(stdlogging.CRITICAL)

def after_enable_default(self):
pass

def before_enable_warning(self):
"""Logs status before enable Warning."""
self._logger.info("Enabling warning.")
self._stream_formatter.set_trace(True)
for logger in all_loggers():
logger.setLevel(stdlogging.WARNING)

def after_enable_warning(self):
"""Logs status after enable Warning."""
self._logger.info("Warning enabled.")

# Trace
def before_enable_trace(self):
"""Logs status before enable Trace."""
Expand Down Expand Up @@ -449,6 +491,25 @@ def set_trace(self, on: bool = True):
if self.current_state_value == "Trace":
self.disable_trace()

def set_warning(self, on: bool = True):
"""Sets Warning state."""
if on and not self.current_state_value == "Warning":
self.enable_warning()
elif not on:
if self.current_state_value == "Warning":
self.disable_warning()

def set_default(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_default()

# as an option to be more obvious. `bittensor.logging.set_info()` is the same `bittensor.logging.set_default()`
def set_info(self):
"""Sets Default state."""
if not self.current_state_value == "Default":
self.enable_info()

def get_level(self) -> int:
"""Returns Logging level."""
return self._logger.level
Expand Down
36 changes: 36 additions & 0 deletions bittensor/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
get_hash,
)
from bittensor.utils.balance import Balance as Balance # noqa: F401
from bittensor.utils.btlogging import logging
from bittensor.utils.mock.subtensor_mock import MockSubtensor as MockSubtensor # noqa: F401
from bittensor.utils.subnets import SubnetsAPI # noqa: F401

Expand Down Expand Up @@ -148,3 +149,38 @@
# Makes the `bittensor.core.extrinsics` subpackage available as `bittensor.extrinsics` for backwards compatibility.
extrinsics_subpackage = importlib.import_module("bittensor.core.extrinsics")
sys.modules["bittensor.extrinsics"] = extrinsics_subpackage


# Logging helpers.
def trace(on: bool = True):
"""
Enables or disables trace logging.
Args:
on (bool): If True, enables trace logging. If False, disables trace logging.
"""
logging.set_trace(on)


def debug(on: bool = True):
"""
Enables or disables debug logging.
Args:
on (bool): If True, enables debug logging. If False, disables debug logging.
"""
logging.set_debug(on)


def warning(on: bool = True):
"""
Enables or disables warning logging.
Args:
on (bool): If True, enables warning logging. If False, disables warning logging and sets default (INFO) level.
"""
logging.set_warning(on)


# set Warning logging level for bittensor SDK
warning()

0 comments on commit e294815

Please sign in to comment.