|
| 1 | +import sys |
| 2 | +import copy |
| 3 | +import linecache |
| 4 | +from threading import local |
| 5 | +from contextlib import contextmanager |
| 6 | + |
| 7 | +from ._compat import with_metaclass |
| 8 | +from .scope import Scope |
| 9 | +from .utils import exceptions_from_error_tuple, create_event, \ |
| 10 | + skip_internal_frames |
| 11 | + |
| 12 | + |
| 13 | +_local = local() |
| 14 | + |
| 15 | + |
| 16 | +class HubMeta(type): |
| 17 | + |
| 18 | + @property |
| 19 | + def current(self): |
| 20 | + try: |
| 21 | + rv = _local.hub |
| 22 | + except AttributeError: |
| 23 | + _local.hub = rv = Hub(GLOBAL_HUB) |
| 24 | + return rv |
| 25 | + |
| 26 | + @property |
| 27 | + def main(self): |
| 28 | + return GLOBAL_HUB |
| 29 | + |
| 30 | + |
| 31 | +class _HubManager(object): |
| 32 | + |
| 33 | + def __init__(self, hub): |
| 34 | + self._old = Hub.current |
| 35 | + _local.hub = hub |
| 36 | + |
| 37 | + def __exit__(self, exc_type, exc_value, tb): |
| 38 | + _local.hub = self._old |
| 39 | + |
| 40 | + |
| 41 | +class _ScopeManager(object): |
| 42 | + |
| 43 | + def __init__(self, hub): |
| 44 | + self._hub = hub |
| 45 | + |
| 46 | + def __exit__(self, exc_type, exc_value, tb): |
| 47 | + self._hub.stack.pop() |
| 48 | + |
| 49 | + |
| 50 | +class Hub(with_metaclass(HubMeta)): |
| 51 | + |
| 52 | + def __init__(self, client_or_hub=None, scope=None): |
| 53 | + if isinstance(client_or_hub, Hub): |
| 54 | + hub = client_or_hub |
| 55 | + client, other_scope = hub._stack[-1] |
| 56 | + if scope is None: |
| 57 | + hub._flush_event_processors() |
| 58 | + scope = copy.copy(other_scope) |
| 59 | + else: |
| 60 | + client = client_or_hub |
| 61 | + if scope is None: |
| 62 | + scope = Scope() |
| 63 | + self._stack = [(client, scope)] |
| 64 | + self._pending_processors = [] |
| 65 | + |
| 66 | + def __enter__(self): |
| 67 | + return _HubManager(self) |
| 68 | + |
| 69 | + def run(self, callback): |
| 70 | + """Runs a callback in the context of the hub. Alternatively the |
| 71 | + with statement can be used on the hub directly. |
| 72 | + """ |
| 73 | + with self: |
| 74 | + callback() |
| 75 | + |
| 76 | + @property |
| 77 | + def client(self): |
| 78 | + """Returns the current client on the hub.""" |
| 79 | + return self._stack[-1][0] |
| 80 | + |
| 81 | + def bind_client(self, new): |
| 82 | + """Binds a new client to the hub.""" |
| 83 | + top = self._stack[-1] |
| 84 | + self._stack[-1] = (new, top[1]) |
| 85 | + |
| 86 | + def capture_event(self, event): |
| 87 | + """Captures an event.""" |
| 88 | + self._flush_event_processors() |
| 89 | + client, scope = self._stack[-1] |
| 90 | + if client is not None: |
| 91 | + client.capture_event(event, scope) |
| 92 | + return event.get('event_id') |
| 93 | + |
| 94 | + def capture_message(self, message, level=None): |
| 95 | + """Captures a message.""" |
| 96 | + if self.client is None: |
| 97 | + return |
| 98 | + if level is None: |
| 99 | + level = 'info' |
| 100 | + event = create_event() |
| 101 | + event['message'] = message |
| 102 | + if level is not None: |
| 103 | + event['level'] = level |
| 104 | + return self.capture_event(event) |
| 105 | + |
| 106 | + def capture_exception(self, error=None): |
| 107 | + """Captures an exception.""" |
| 108 | + client = self.client |
| 109 | + if client is None: |
| 110 | + return |
| 111 | + if error is None: |
| 112 | + exc_type, exc_value, tb = sys.exc_info() |
| 113 | + else: |
| 114 | + tb = getattr(error, '__traceback__', None) |
| 115 | + if tb is not None: |
| 116 | + exc_type = type(error) |
| 117 | + exc_value = error |
| 118 | + else: |
| 119 | + exc_type, exc_value, tb = sys.exc_info() |
| 120 | + tb = skip_internal_frames(tb) |
| 121 | + if exc_value is not error: |
| 122 | + tb = None |
| 123 | + exc_value = error |
| 124 | + exc_type = type(error) |
| 125 | + |
| 126 | + event = create_event() |
| 127 | + event['exception'] = { |
| 128 | + 'values': exceptions_from_error_tuple( |
| 129 | + exc_type, exc_value, tb, client.options['with_locals']) |
| 130 | + } |
| 131 | + return self.capture_event(event) |
| 132 | + |
| 133 | + def add_breadcrumb(self, crumb): |
| 134 | + """Adds a breadcrumb.""" |
| 135 | + client, scope = self._stack[-1] |
| 136 | + if client is None: |
| 137 | + return |
| 138 | + if callable(crumb): |
| 139 | + crumb = crumb() |
| 140 | + if crumb is not None: |
| 141 | + scope.breadcrumbs.append(crumb) |
| 142 | + while len(scope.breadcrumbs) >= client.options['max_breadcrumbs']: |
| 143 | + scope.breadcrumbs.popleft() |
| 144 | + |
| 145 | + def add_event_processor(self, factory): |
| 146 | + """Registers a new event processor with the top scope.""" |
| 147 | + self._pending_processors.append(factory) |
| 148 | + |
| 149 | + def push_scope(self): |
| 150 | + """Pushes a new layer on the scope stack.""" |
| 151 | + client, scope = self._stack[-1] |
| 152 | + self._stack.append((client, copy.copy(scope))) |
| 153 | + return _ScopeManager(self) |
| 154 | + |
| 155 | + @contextmanager |
| 156 | + def configure_scope(self): |
| 157 | + """Reconfigures the scope.""" |
| 158 | + yield self._stack[-1][1] |
| 159 | + |
| 160 | + def _flush_event_processors(self): |
| 161 | + rv = self._pending_processors |
| 162 | + self._pending_processors = [] |
| 163 | + top = self._stack[-1][1] |
| 164 | + for factory in rv: |
| 165 | + top._event_processors.append(factory()) |
| 166 | + |
| 167 | + |
| 168 | +GLOBAL_HUB = Hub() |
0 commit comments