############################################################################
#                                                                          #
# Copyright (c)2008, Digi International (Digi). All Rights Reserved.       #
#                                                                          #
# Permission to use, copy, modify, and distribute this software and its    #
# documentation, without fee and without a signed licensing agreement, is  #
# hereby granted, provided that the software is used on Digi products only #
# and that the software contain this copyright notice, and the following   #
# two paragraphs appear in all copies, modifications, and distributions as #
# well. Contact Product Management, Digi International, Inc., 11001 Bren   #
# Road East, Minnetonka, MN, +1 952-912-3444, for commercial licensing     #
# opportunities for non-Digi products.                                     #
#                                                                          #
# DIGI SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED   #
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A          #
# PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, #
# PROVIDED HEREUNDER IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND. #
# DIGI HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,         #
# ENHANCEMENTS, OR MODIFICATIONS.                                          #
#                                                                          #
# IN NO EVENT SHALL DIGI BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,      #
# SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,   #
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF   #
# DIGI HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.                #
#                                                                          #
############################################################################

"""
Logging events are objects generated by the Dia's
:class:`~channels.channel_publisher.ChannelPublisher` and distributed
to the various :class:`loggers
<channels.logging.logger_base.LoggerBase>` instances by the
:class:`~channels.logging.logging_manager.LoggingManager` in order to
propagate loggable events to the logging subsystem.
"""

# imports
from channels.channel import PERM_GET
from common.helpers.format_channels import iso_date

# classes
class LoggingEventBase(object):
    """
    Logging event base class.

    Creates basic common shared members and provides a string
    representation for common debugging access to these events in the
    system.

    """
    __slots__ = ["channel"]
    def __init__(self, channel, record=None):
        self.channel = channel
        self.record = record
        
    def __repr__(self):

        if self.channel == None:
            return "<%s record=%s (no channel)>" % (
                self.__class__.__name__,
                repr(self.record))

        if getattr(self.channel, "perm_mask", None) == None:
            return "<%s record=%s channel=%s (no permissions mask)>" % (
                self.__class__.__name__,
                repr(self.record),
                self.channel.name()) 

        perm_denied = not (self.channel.perm_mask() & PERM_GET)
        if not perm_denied:
            try:
                return ("<%s record=%s channel=%s "
                        "sample.value=%s timestamp=%s>") % (
                     self.__class__.__name__,
                     repr(self.record),
                     self.channel.name(),
                     repr(self.channel.get().value),
                     iso_date(t=self.channel.get().timestamp)) 
            except:
                perm_denied = True
                
        if perm_denied:
            return "<%s record=%s channel=%s (get permission denied)>" % (
                self.__class__.__name__,
                repr(self.record),
                self.channel.name()) 
            

class LoggingEventNewSample(LoggingEventBase):
    """New data published to a channel"""
    pass

class LoggingEventChannelNew(LoggingEventBase):
    """
    A new channel created in the
    :class:`~channels.channel_database.ChannelDatabase`

    """
    pass

class LoggingEventChannelRemove(LoggingEventBase):
    """
    A channel removed from the
    :class:`~channels.channel_database.ChannelDatabase`

    """
    pass

class LoggingEventMeta(LoggingEventBase):
    """
    Generic event used internally

    """
    def __init__(self, record, description=""):
        self.description = description
        LoggingEventBase.__init__(self, channel=None, record=record)
