1
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13 11
import logging
14

15

16 11
HISTORY_RECORDER = None
17 11
logger = logging.getLogger(__name__)
18

19

20 11
class BaseHistoryHandler(object):
21 11
    def emit(self, event_type, payload, source):
22
        raise NotImplementedError('emit()')
23

24

25 11
class HistoryRecorder(object):
26 11
    def __init__(self):
27 11
        self._enabled = False
28 11
        self._handlers = []
29

30 11
    def enable(self):
31 11
        self._enabled = True
32

33 11
    def disable(self):
34 11
        self._enabled = False
35

36 11
    def add_handler(self, handler):
37 11
        self._handlers.append(handler)
38

39 11
    def record(self, event_type, payload, source='BOTOCORE'):
40 11
        if self._enabled and self._handlers:
41 11
            for handler in self._handlers:
42 11
                try:
43 11
                    handler.emit(event_type, payload, source)
44 11
                except Exception:
45
                    # Never let the process die because we had a failure in
46
                    # a record collection handler.
47 11
                    logger.debug("Exception raised in %s.", handler,
48
                                 exc_info=True)
49

50

51 11
def get_global_history_recorder():
52
    global HISTORY_RECORDER
53 11
    if HISTORY_RECORDER is None:
54 11
        HISTORY_RECORDER = HistoryRecorder()
55 11
    return HISTORY_RECORDER

Read our documentation on viewing source code .

Loading