Package rrlog :: Package server :: Module printwriter
[hide private]
[frames] | no frames]

Source Code for Module rrlog.server.printwriter

  1  #Copyright (c) 2007 
  2  #        Ruben Reifenberg, Germany, 07381. 
  3  #    All rights reserved. 
  4  # 
  5  #Redistribution and use in source and binary forms, with or without 
  6  #modification, are permitted provided that the following conditions 
  7  #are met: 
  8  #1. Redistributions of source code must retain the above copyright 
  9  #   notice, this list of conditions and the following disclaimer as 
 10  #   the first lines of this file unmodified. 
 11  #2. Redistributions in binary form must reproduce the above copyright 
 12  #   notice, this list of conditions and the following disclaimer in the 
 13  #   documentation and/or other materials provided with the distribution. 
 14  # 
 15  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
 16  # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 17  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 18  # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
 19  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 20  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 21  # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 22  # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 23  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 24  # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 25  # SUCH DAMAGE. 
 26   
 27   
 28  """ 
 29  @summary: 
 30  Log to stdout. 
 31  @author: Ruben Reifenberg 
 32  """ 
 33   
 34  from rrlog import Log 
 35  from rrlog.server import LogServer 
 36  from rrlog.server.textwriter import TextlineLogWriter 
 37   
 38  import sys 
 39  print_ = sys.stdout.write # allow replacing print_ for tests 
 40   
41 -def createServer(writer=None, tsFormat=None, filters=None, observers=None, writeMsgid=False, format_line=None):
42 """ 43 @param tsFormat: timestamp format. See L{rrlog.server.LogServer.__init__} 44 @param filters: default = () See L{rrlog.server.LogServer.__init__} 45 @param observers: default = () See L{rrlog.server.LogServer.__init__} 46 @param format_line: See L{rrlog.server.textwriter.TextlineLogWriter.__init__} 47 """ 48 if writer is None: 49 writer = LOGWRITER_CLASS( 50 writeMsgid=writeMsgid, 51 format_line=format_line, 52 ) 53 54 return LogServer( 55 writer = writer, 56 filters = filters, 57 observers = observers, 58 tsFormat = tsFormat, 59 )
60
61 -def createLocalLog( 62 writer=None, 63 filters=None, 64 observers=None, 65 traceOffset=0, 66 tsFormat=None, 67 writeMsgid=False, 68 stackMax=1, 69 catsEnable=None, 70 catsDisable=None, 71 seFilesExclude=None, 72 format_line=None, 73 name=None, 74 extractStack=True, 75 ):
76 """ 77 @param catsEnable: see L{rrlog.Log.__init__} 78 @param catsDisable: see L{rrlog.Log.__init__} 79 @param seFilesExclude: see L{rrlog.Log.__init__} 80 @param filters: see L{rrlog.server.LogServer.__init__} 81 @param observers: see L{rrlog.server.LogServer.__init__} 82 @param tsFormat: timestamp format. See L{rrlog.server.LogServer.__init__} 83 Here, the default is None (shows no time stamps) 84 @param stackMax: see L{rrlog.Log.__init__}, default: 5 (==log 5 stack levels.) 85 @param format_line: See L{rrlog.server.textwriter.TextlineLogWriter.__init__} 86 @param name: The log can be identified by its optional name attribute (__repr__ method of the log will use it.) 87 @param extractStack: see L{rrlog.Log.__init__} 88 @return: a Log ready to use 89 """ 90 if writer is None: 91 writer = LOGWRITER_CLASS( 92 writeMsgid=writeMsgid, 93 format_line=format_line, 94 ) 95 return Log( 96 server = createServer( 97 writer=writer, 98 tsFormat=tsFormat, 99 filters=filters, 100 observers=observers, 101 ), 102 traceOffset=traceOffset, 103 stackMax=stackMax, 104 catsEnable=catsEnable, 105 catsDisable=catsDisable, 106 seFilesExclude=seFilesExclude, 107 name=name, 108 extractStack=extractStack, 109 )
110 111 112 113
114 -class PrintLogWriter(TextlineLogWriter):
115 """ 116 """
117 - def __init__(self, writeMsgid=False, format_line=None):
118 TextlineLogWriter.__init__(self, format_line) 119 self.writeMsgid = writeMsgid
120
121 - def _format_line(self, job):
122 """ 123 Default formatting method. 124 @rtype: str 125 @return: single log line 126 """ 127 def pre(): 128 if self.writeMsgid: return "[%s]"%(job.msgid) 129 else: return ""
130 return "%s%s %s %s %s\n"%( 131 pre(), 132 job.msg, 133 self.cfn_cln(job), 134 job.pathStr(1), 135 job.ts, 136 )
137 138
139 - def writeNow(self, job):
140 """ 141 Write without buffering, return when written 142 """ 143 print_(self._format_line[0](job))
144 145 146 LOGWRITER_CLASS = PrintLogWriter 147