# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/sentryUtil.ipynb (unless otherwise specified).

__all__ = ['logSentry', 'autologSentry']

# Cell
from sentry_sdk import add_breadcrumb, capture_exception, capture_message
from .exception import traceback
from typing import Any
from copy import deepcopy
from .exception import errorString
import functools, sentry_sdk, inspect

# Cell
def logSentry(message:str, data:Any = (lambda :{})(), level:str = 'info', section:str='main'):
  '''
    just add docs for ease of logging to sentry
    Input:
      message ::str:: required :: message to send to sentry
      data ::dict:: optional :: and object to send to sentry (default is an empty dict)
      level ::str::optional:: log level (default:info)
      section ::str::optional:: section name or function name (default: main)
    Response:
      Bool:: true means logged properly, false for error, print error message to console
  '''
  try:
    add_breadcrumb(
      category=section,
      data={'data':deepcopy(data)},
      level=level,
      message=message
    )
    return True
  except Exception as e:
    print(message, data, level, section)
    print(f'error is {e}, {traceback()}')
    return False

# Cell
def autologSentry(verbose=False):
  def outer(f):
    @functools.wraps(f)
    def inner(*args,**kwargs):
      try:
        r = f(*args,**kwargs)
        message = str(f.__name__)+str(inspect.signature(f))
        sentry_sdk.add_breadcrumb(category=f.__name__, message=message, data={
          'args': args, 'kwargs': kwargs, 'output':r }, level='info')
        if verbose: print(f.__name__, message, { 'args': args, 'kwargs': kwargs, 'output':r })
        return r
      except Exception as e:
        sentry_sdk.add_breadcrumb(category=f.__name__, message=errorString(), data={
          'args': args, 'kwargs': kwargs, 'error': str(e) }, level='critical')
        if verbose: print(f.__name__, errorString(), {'args': args, 'kwargs': kwargs, 'error':str(e)} )
        raise e
    return inner
  return outer