home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / ZSI / wstools / logging.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  9.4 KB  |  244 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. ident = '$Id: logging.py 1395 2007-06-14 06:49:35Z boverhof $'
  5. import os
  6. import sys
  7. WARN = 1
  8. DEBUG = 2
  9.  
  10. class ILogger:
  11.     level = 0
  12.     
  13.     def __init__(self, msg):
  14.         pass
  15.  
  16.     
  17.     def warning(self, *args, **kw):
  18.         pass
  19.  
  20.     
  21.     def debug(self, *args, **kw):
  22.         pass
  23.  
  24.     
  25.     def error(self, *args, **kw):
  26.         pass
  27.  
  28.     
  29.     def setLevel(cls, level):
  30.         cls.level = level
  31.  
  32.     setLevel = classmethod(setLevel)
  33.     
  34.     debugOn = lambda self: self.level >= DEBUG
  35.     
  36.     warnOn = lambda self: self.level >= WARN
  37.  
  38.  
  39. class BasicLogger(ILogger):
  40.     last = ''
  41.     
  42.     def __init__(self, msg, out = sys.stdout):
  43.         self.msg = msg
  44.         self.out = out
  45.  
  46.     
  47.     def warning(self, msg, *args, **kw):
  48.         if self.warnOn() is False:
  49.             return None
  50.         print >>self, '    %s  ' % self.WARN,
  51.         print >>self, msg % args
  52.  
  53.     WARN = '[WARN]'
  54.     
  55.     def debug(self, msg, *args, **kw):
  56.         if self.debugOn() is False:
  57.             return None
  58.         print >>self, '    %s  ' % self.DEBUG,
  59.         print >>self, msg % args
  60.  
  61.     DEBUG = '[DEBUG]'
  62.     
  63.     def error(self, msg, *args, **kw):
  64.         if BasicLogger.last != self.msg:
  65.             BasicLogger.last = self.msg
  66.             print >>self, '---- ', self.msg, ' ----'
  67.         
  68.         print >>self, '    %s  ' % self.ERROR,
  69.         print >>self, msg % args
  70.  
  71.     ERROR = '[ERROR]'
  72.     
  73.     def write(self, *args):
  74.         for s in args:
  75.             self.out.write(s)
  76.         
  77.         event = ''.join(*args)
  78.  
  79.  
  80. _LoggerClass = BasicLogger
  81.  
  82. class GridLogger(ILogger):
  83.     
  84.     def debug(self, msg, *args, **kw):
  85.         kw['component'] = self.msg
  86.         gridLog(event = msg % args, level = 'DEBUG', **kw)
  87.  
  88.     
  89.     def warning(self, msg, *args, **kw):
  90.         kw['component'] = self.msg
  91.         gridLog(event = msg % args, level = 'WARNING', **kw)
  92.  
  93.     
  94.     def error(self, msg, *args, **kw):
  95.         kw['component'] = self.msg
  96.         gridLog(event = msg % args, level = 'ERROR', **kw)
  97.  
  98.  
  99. GLRegistry = { }
  100.  
  101. class GLRecord(dict):
  102.     reserved = ('ts', 'event', 'level', 'status', 'gid', 'prog')
  103.     omitname = ()
  104.     levels = dict(FATAL = 'Component cannot continue, or system is unusable.', ALERT = 'Action must be taken immediately.', CRITICAL = 'Critical conditions (on the system).', ERROR = 'Errors in the component; not errors from elsewhere.', WARNING = 'Problems that are recovered from, usually.', NOTICE = 'Normal but significant condition.', INFO = 'Informational messages that would be useful to a deployer or administrator.', DEBUG = 'Lower level information concerning program logic decisions, internal state, etc.', TRACE = 'Finest granularity, similar to "stepping through" the component or system.')
  105.     
  106.     def __init__(self, date = None, **kw):
  107.         if not date:
  108.             pass
  109.         kw['ts'] = self.GLDate()
  110.         if not kw.get('gid'):
  111.             pass
  112.         kw['gid'] = os.getpid()
  113.         dict.__init__(self, kw)
  114.  
  115.     
  116.     def __str__(self):
  117.         StringIO = StringIO
  118.         import cStringIO
  119.         s = StringIO()
  120.         n = ' '
  121.         reserved = self.reserved
  122.         omitname = self.omitname
  123.         levels = self.levels
  124.         for k in list((filter,)((lambda i: self.has_key(i)), reserved)) + list((filter,)((lambda i: i not in reserved), self.keys())):
  125.             v = self[k]
  126.             if k in omitname:
  127.                 s.write('%s ' % self.format[type(v)](v))
  128.                 continue
  129.             
  130.             if k == reserved[2] and v not in levels:
  131.                 pass
  132.             
  133.             s.write('%s=%s ' % (k, self.format[type(v)](v)))
  134.         
  135.         s.write('\n')
  136.         return s.getvalue()
  137.  
  138.     
  139.     class GLDate(str):
  140.         
  141.         def __new__(self, args = None):
  142.             import datetime
  143.             if not args:
  144.                 pass
  145.             args = datetime.datetime.utcnow()
  146.             if not args.tzinfo:
  147.                 pass
  148.             l = (args.year, args.month, args.day, args.hour, args.minute, args.second, args.microsecond, 'Z')
  149.             return str.__new__(self, '%04d-%02d-%02dT%02d:%02d:%02d.%06d%s' % l)
  150.  
  151.  
  152.     format = {
  153.         int: str,
  154.         float: (lambda x: '%lf' % x),
  155.         long: str,
  156.         str: (lambda x: x),
  157.         unicode: str,
  158.         GLDate: str }
  159.  
  160.  
  161. def gridLog(**kw):
  162.     import os
  163.     if not bool(int(os.environ.get('GRIDLOG_ON', 0))):
  164.         return None
  165.     url = os.environ.get('GRIDLOG_DEST')
  166.     if url is None:
  167.         return None
  168.     
  169.     try:
  170.         scheme = url[:url.find('://')]
  171.         send = GLRegistry[scheme]
  172.         send(url, str(GLRecord(**kw)))
  173.     except Exception:
  174.         url is None
  175.         ex = url is None
  176.         bool(int(os.environ.get('GRIDLOG_ON', 0)))
  177.         print >>sys.stderr, '*** gridLog failed -- %s' % str(kw)
  178.     except:
  179.         url is None
  180.  
  181.  
  182.  
  183. def sendUDP(url, outputStr):
  184.     socket = socket
  185.     AF_INET = AF_INET
  186.     SOCK_DGRAM = SOCK_DGRAM
  187.     import socket
  188.     idx1 = url.find('://') + 3
  189.     idx2 = url.find('/', idx1)
  190.     if idx2 < idx1:
  191.         idx2 = len(url)
  192.     
  193.     netloc = url[idx1:idx2]
  194.     (host, port) = netloc.split(':') + [
  195.         80][0:2]
  196.     socket(AF_INET, SOCK_DGRAM).sendto(outputStr, (host, int(port)))
  197.  
  198.  
  199. def writeToFile(url, outputStr):
  200.     print >>open(url.split('://')[1], 'a+'), outputStr
  201.  
  202. GLRegistry['gridlog-udp'] = sendUDP
  203. GLRegistry['file'] = writeToFile
  204.  
  205. def setBasicLogger():
  206.     setLoggerClass(BasicLogger)
  207.     BasicLogger.setLevel(0)
  208.  
  209.  
  210. def setGridLogger():
  211.     setLoggerClass(GridLogger)
  212.  
  213.  
  214. def setBasicLoggerWARN():
  215.     setLoggerClass(BasicLogger)
  216.     BasicLogger.setLevel(WARN)
  217.  
  218.  
  219. def setBasicLoggerDEBUG():
  220.     setLoggerClass(BasicLogger)
  221.     BasicLogger.setLevel(DEBUG)
  222.  
  223.  
  224. def setLoggerClass(loggingClass):
  225.     pass
  226.  
  227.  
  228. def setLoggerClass(loggingClass):
  229.     global _LoggerClass
  230.     _LoggerClass = loggingClass
  231.  
  232.  
  233. def setLevel(level = 0):
  234.     ILogger.level = level
  235.  
  236.  
  237. def getLevel():
  238.     return ILogger.level
  239.  
  240.  
  241. def getLogger(msg):
  242.     return _LoggerClass(msg)
  243.  
  244.