morphbr@718: #!/usr/bin/env python morphbr@718: morphbr@718: __author__ = "Artur Duque de Souza" morphbr@718: __author_email__ = "artur.souza@indt.org.br" morphbr@718: __license__ = "GPL" morphbr@718: __version__ = "0.1" morphbr@718: morphbr@718: import os morphbr@718: import logging morphbr@718: morphbr@718: __all__ = ("Log", "log_structure") morphbr@718: morphbr@718: class log_structure(object): morphbr@718: """Structure to hold log info.""" morphbr@718: morphbr@718: def __init__(self, log=None): morphbr@718: self.log = log morphbr@718: self.history = [] morphbr@718: # __init__() morphbr@718: morphbr@718: # log_structure() morphbr@718: morphbr@718: class Log(object): morphbr@718: """This class implements a log where we can store status of morphbr@718: all transcoders (even from those that are not running any more).""" morphbr@718: morphbr@718: ## key = tid morphbr@718: ## item = ls morphbr@718: logs = {} morphbr@718: morphbr@718: def insert(self, tid, name): morphbr@718: """Insert a given tid on the log structure""" morphbr@718: if not self.logs.has_key(tid): morphbr@718: self.logs[tid] = log_structure(logging.getLogger(name)) morphbr@718: return True morphbr@718: else: morphbr@718: return False morphbr@718: # insert() morphbr@718: morphbr@718: def remove(self, tid=None): morphbr@718: """Cleans up all log stored for a morphbr@718: given tid or for all transcodes.""" morphbr@718: if not tid: morphbr@718: self.logs = {} morphbr@718: else: morphbr@718: del(self.logs[tid]) morphbr@718: morphbr@718: return True morphbr@718: # clean() morphbr@718: morphbr@718: def get_status(self, tid=None, all=False): morphbr@718: """Get the status of all transcoders or morphbr@718: of just one of them if it's given an tid.""" morphbr@718: if not tid: morphbr@718: ret = {} morphbr@718: for tids, logs in self.logs.items(): morphbr@718: if len(logs.history) > 0: morphbr@718: if not all: morphbr@718: ret[tids] = logs.history[-1] morphbr@718: else: morphbr@718: ret[tids] = logs.history morphbr@718: return ret morphbr@718: elif self.logs.has_key(tid) and len(self.logs[tid].history) > 0: morphbr@718: if not all: morphbr@718: return self.logs[tid].history[-1] morphbr@718: else: morphbr@718: return self.logs[tid].history morphbr@718: morphbr@718: return False morphbr@718: # get_status() morphbr@718: morphbr@718: def _update_status(self, tid=None, msg=""): morphbr@718: """Update the status of a given tid. Private method that morphbr@718: is only called inside error/info/debug wrappers""" morphbr@718: if msg != "": morphbr@718: self.logs[tid].history.append(msg) morphbr@718: return True morphbr@718: else: morphbr@718: return False morphbr@718: # update_status() morphbr@718: morphbr@718: def error(self, tid, msg): morphbr@718: """Python's Log.error wrapper""" morphbr@718: if self.logs.has_key(tid): morphbr@718: self.logs[tid].log.error("%s" % msg) morphbr@718: return self._update_status(tid, msg) morphbr@718: else: morphbr@718: return False morphbr@718: # error() morphbr@718: morphbr@718: def info(self, tid, msg): morphbr@718: """Python's Log.info wrapper""" morphbr@718: if self.logs.has_key(tid): morphbr@718: self.logs[tid].log.info("%s" % msg) morphbr@718: self._update_status(tid, msg) morphbr@718: return True morphbr@718: else: morphbr@718: return False morphbr@718: # info() morphbr@718: morphbr@718: def debug(self, tid, msg): morphbr@718: """Python's Log.debug wrapper""" morphbr@718: if self.logs.has_key(tid): morphbr@718: self.logs[tid].log.debug("%s" % msg) morphbr@718: self._update_status(tid, msg) morphbr@718: return True morphbr@718: else: morphbr@718: return False morphbr@718: # debug()