1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought logger package component>
#------------------------------------------------------------------------------
# Standard library imports.
from logging import Handler
# Enthought library imports.
from enthought.util.ring_buffer import RingBuffer
class LogQueueHandler(Handler):
""" Buffers up the log messages so that we can display them later.
This is important on startup when log messages are generated before
the ui has started. By putting them in this queue we can display
them once the ui is ready.
"""
# The view where updates will go
_view = None
def __init__(self, size=1000):
Handler.__init__(self)
# only buffer 1000 log records
self.size = size
self.ring = RingBuffer(self.size)
self.dirty = False
return
def emit(self, record):
""" Actually this is more like an enqueue than an emit()."""
self.ring.append(record)
if self._view is not None:
try:
self._view.update()
except Exception, e:
pass
self.dirty = True
return
def get(self):
self.dirty = False
try:
result = self.ring.get()
except Exception, msg:
# we did our best and it won't cause too much damage
# to just return a bogus message
result = []
return result
def has_new_records(self):
return self.dirty
def reset(self):
# start over with a new empty buffer
self.ring = RingBuffer(self.size)
if self._view is not None:
try:
self._view.update()
except Exception, e:
pass
self.dirty = True
return
## EOF ##################################################################
|