gmyth-stream/server/lib/log.py
author melunko
Thu Feb 21 17:44:16 2008 +0000 (2008-02-21)
branchtrunk
changeset 917 be87d28e8371
parent 718 3fbcd3d9b2d1
permissions -rw-r--r--
[svn r926] Added gst-gpac
morphbr@718
     1
#!/usr/bin/env python
morphbr@718
     2
morphbr@718
     3
__author__ = "Artur Duque de Souza"
morphbr@718
     4
__author_email__ = "artur.souza@indt.org.br"
morphbr@718
     5
__license__ = "GPL"
morphbr@718
     6
__version__ = "0.1"
morphbr@718
     7
morphbr@718
     8
import os
morphbr@718
     9
import logging
morphbr@718
    10
morphbr@718
    11
__all__ = ("Log", "log_structure")
morphbr@718
    12
morphbr@718
    13
class log_structure(object):
morphbr@718
    14
    """Structure to hold log info."""
morphbr@718
    15
morphbr@718
    16
    def __init__(self, log=None):
morphbr@718
    17
        self.log = log
morphbr@718
    18
        self.history = []
morphbr@718
    19
    # __init__()
morphbr@718
    20
morphbr@718
    21
# log_structure()
morphbr@718
    22
morphbr@718
    23
class Log(object):
morphbr@718
    24
    """This class implements a log where we can store status of
morphbr@718
    25
    all transcoders (even from those that are not running any more)."""
morphbr@718
    26
morphbr@718
    27
    ## key = tid
morphbr@718
    28
    ## item = ls
morphbr@718
    29
    logs = {}
morphbr@718
    30
morphbr@718
    31
    def insert(self, tid, name):
morphbr@718
    32
        """Insert a given tid on the log structure"""
morphbr@718
    33
        if not self.logs.has_key(tid):
morphbr@718
    34
            self.logs[tid] = log_structure(logging.getLogger(name))
morphbr@718
    35
            return True
morphbr@718
    36
        else:
morphbr@718
    37
            return False
morphbr@718
    38
    # insert()
morphbr@718
    39
morphbr@718
    40
    def remove(self, tid=None):
morphbr@718
    41
        """Cleans up all log stored for a
morphbr@718
    42
        given tid or for all transcodes."""
morphbr@718
    43
        if not tid:
morphbr@718
    44
            self.logs = {}
morphbr@718
    45
        else:
morphbr@718
    46
            del(self.logs[tid])
morphbr@718
    47
morphbr@718
    48
        return True
morphbr@718
    49
    # clean()
morphbr@718
    50
morphbr@718
    51
    def get_status(self, tid=None, all=False):
morphbr@718
    52
        """Get the status of all transcoders or
morphbr@718
    53
        of just one of them if it's given an tid."""
morphbr@718
    54
        if not tid:
morphbr@718
    55
            ret = {}
morphbr@718
    56
            for tids, logs in self.logs.items():
morphbr@718
    57
                if len(logs.history) > 0:
morphbr@718
    58
                    if not all:
morphbr@718
    59
                        ret[tids] = logs.history[-1]
morphbr@718
    60
                    else:
morphbr@718
    61
                        ret[tids] = logs.history
morphbr@718
    62
            return ret
morphbr@718
    63
        elif self.logs.has_key(tid) and len(self.logs[tid].history) > 0:
morphbr@718
    64
            if not all:
morphbr@718
    65
                return self.logs[tid].history[-1]
morphbr@718
    66
            else:
morphbr@718
    67
                return self.logs[tid].history
morphbr@718
    68
morphbr@718
    69
        return False
morphbr@718
    70
    # get_status()
morphbr@718
    71
morphbr@718
    72
    def _update_status(self, tid=None, msg=""):
morphbr@718
    73
        """Update the status of a given tid. Private method that
morphbr@718
    74
        is only called inside error/info/debug wrappers"""
morphbr@718
    75
        if msg != "":
morphbr@718
    76
            self.logs[tid].history.append(msg)
morphbr@718
    77
            return True
morphbr@718
    78
        else:
morphbr@718
    79
            return False
morphbr@718
    80
    # update_status()
morphbr@718
    81
morphbr@718
    82
    def error(self, tid, msg):
morphbr@718
    83
        """Python's Log.error wrapper"""
morphbr@718
    84
        if self.logs.has_key(tid):
morphbr@718
    85
            self.logs[tid].log.error("%s" % msg)
morphbr@718
    86
            return self._update_status(tid, msg)
morphbr@718
    87
        else:
morphbr@718
    88
            return False
morphbr@718
    89
    # error()
morphbr@718
    90
morphbr@718
    91
    def info(self, tid, msg):
morphbr@718
    92
        """Python's Log.info wrapper"""
morphbr@718
    93
        if self.logs.has_key(tid):
morphbr@718
    94
            self.logs[tid].log.info("%s" % msg)
morphbr@718
    95
            self._update_status(tid, msg)
morphbr@718
    96
            return True
morphbr@718
    97
        else:
morphbr@718
    98
            return False
morphbr@718
    99
    # info()
morphbr@718
   100
morphbr@718
   101
    def debug(self, tid, msg):
morphbr@718
   102
        """Python's Log.debug wrapper"""
morphbr@718
   103
        if self.logs.has_key(tid):
morphbr@718
   104
            self.logs[tid].log.debug("%s" % msg)
morphbr@718
   105
            self._update_status(tid, msg)
morphbr@718
   106
            return True
morphbr@718
   107
        else:
morphbr@718
   108
            return False
morphbr@718
   109
    # debug()