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