1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/lib/log.py Tue Feb 26 14:10:22 2008 +0000
1.3 @@ -0,0 +1,109 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +__author__ = "Artur Duque de Souza"
1.7 +__author_email__ = "artur.souza@indt.org.br"
1.8 +__license__ = "GPL"
1.9 +__version__ = "0.1"
1.10 +
1.11 +import os
1.12 +import logging
1.13 +
1.14 +__all__ = ("Log", "log_structure")
1.15 +
1.16 +class log_structure(object):
1.17 + """Structure to hold log info."""
1.18 +
1.19 + def __init__(self, log=None):
1.20 + self.log = log
1.21 + self.history = []
1.22 + # __init__()
1.23 +
1.24 +# log_structure()
1.25 +
1.26 +class Log(object):
1.27 + """This class implements a log where we can store status of
1.28 + all transcoders (even from those that are not running any more)."""
1.29 +
1.30 + ## key = tid
1.31 + ## item = ls
1.32 + logs = {}
1.33 +
1.34 + def insert(self, tid, name):
1.35 + """Insert a given tid on the log structure"""
1.36 + if not self.logs.has_key(tid):
1.37 + self.logs[tid] = log_structure(logging.getLogger(name))
1.38 + return True
1.39 + else:
1.40 + return False
1.41 + # insert()
1.42 +
1.43 + def remove(self, tid=None):
1.44 + """Cleans up all log stored for a
1.45 + given tid or for all transcodes."""
1.46 + if not tid:
1.47 + self.logs = {}
1.48 + else:
1.49 + del(self.logs[tid])
1.50 +
1.51 + return True
1.52 + # clean()
1.53 +
1.54 + def get_status(self, tid=None, all=False):
1.55 + """Get the status of all transcoders or
1.56 + of just one of them if it's given an tid."""
1.57 + if not tid:
1.58 + ret = {}
1.59 + for tids, logs in self.logs.items():
1.60 + if len(logs.history) > 0:
1.61 + if not all:
1.62 + ret[tids] = logs.history[-1]
1.63 + else:
1.64 + ret[tids] = logs.history
1.65 + return ret
1.66 + elif self.logs.has_key(tid) and len(self.logs[tid].history) > 0:
1.67 + if not all:
1.68 + return self.logs[tid].history[-1]
1.69 + else:
1.70 + return self.logs[tid].history
1.71 +
1.72 + return False
1.73 + # get_status()
1.74 +
1.75 + def _update_status(self, tid=None, msg=""):
1.76 + """Update the status of a given tid. Private method that
1.77 + is only called inside error/info/debug wrappers"""
1.78 + if msg != "":
1.79 + self.logs[tid].history.append(msg)
1.80 + return True
1.81 + else:
1.82 + return False
1.83 + # update_status()
1.84 +
1.85 + def error(self, tid, msg):
1.86 + """Python's Log.error wrapper"""
1.87 + if self.logs.has_key(tid):
1.88 + self.logs[tid].log.error("%s" % msg)
1.89 + return self._update_status(tid, msg)
1.90 + else:
1.91 + return False
1.92 + # error()
1.93 +
1.94 + def info(self, tid, msg):
1.95 + """Python's Log.info wrapper"""
1.96 + if self.logs.has_key(tid):
1.97 + self.logs[tid].log.info("%s" % msg)
1.98 + self._update_status(tid, msg)
1.99 + return True
1.100 + else:
1.101 + return False
1.102 + # info()
1.103 +
1.104 + def debug(self, tid, msg):
1.105 + """Python's Log.debug wrapper"""
1.106 + if self.logs.has_key(tid):
1.107 + self.logs[tid].log.debug("%s" % msg)
1.108 + self._update_status(tid, msg)
1.109 + return True
1.110 + else:
1.111 + return False
1.112 + # debug()