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

Source Code for Module rrlog.server.textwriter

  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  @summary: 
 29  Abstract base for text line logging. 
 30  @author: Ruben Reifenberg 
 31  """ 
 32   
 33   
34 -def default_format_line(job, lineCount=""):
35 """ 36 Format a text line for writing from given job data. 37 This format is not intended for parsers, 38 since it does not provide parser friendly delimiters between the items, and 39 it is likely to change with later versions. 40 Currently, provide a custom line formatting (see format_line parameter) if you need a stable and parseable format. 41 @param lineCount: int or str, intended to be used for adding a line number. Default == "" (not visible) 42 (Generally, writer classes are free to use it in their custom way (e.g.starting form 0 or from 1, globaly written lines in file or stdout/lines in current file only), or to ignore it.) 43 @return: line to be written 44 @rtype: str 45 """ 46 def cfunc(): 47 if job.cfunc is not "": return ":::%s"%(job.cfunc) 48 else: return ""
49 def ts(): 50 if job.ts is not "": return "@%s"%(job.ts) 51 else: return "" 52 return "%1s %s.[%s:%s%s] %s %s %s\n"%( 53 job.cat, 54 lineCount, 55 job.pid, 56 job.msgid, 57 ts(), 58 job.msg, 59 #self.cfn_cln(job), # path-str includes the cfn/cln now 60 cfunc(), 61 job.pathStr(0), 62 ) 63 64
65 -class TextlineLogWriter(object):
66 """ 67 Abstract base class for text line writers 68 """
69 - def __init__(self, format_line=None):
70 """ 71 @param format_line: Callable, takes a job and returns a single message line as str. 72 See L{MsgJob} for job attributes available. 73 See L{textwriter.default_format_line} for an example implementation. 74 """ 75 self._lineCount = 0 76 if format_line is not None: 77 self._format_line=(format_line,) 78 else: 79 self._format_line=(self._format_line,)
80 81
82 - def estimateLineCount(self):
83 """ 84 For performance reasons, it is allowed to estimate instead count exactly. 85 (Intended for implementations which need to read access written data to count; 86 but not required here, we are simply counting exactly.) 87 @return: count of already written log lines 88 """ 89 return self._lineCount
90
91 - def cfn_cln(self, job):
92 """ 93 Convenient string for callers file name / callers line number 94 @rtype: str 95 @return: "*<cfn>(<cln>)", "" if not given 96 """ 97 cln = job.cln() 98 if cln != -1: 99 return "*%s(%s)"%(job.cfn(),cln) 100 else: 101 return ""
102 103
104 - def _format_line(self, job):
105 """ 106 Default formatting method. 107 @rtype: str 108 @return: single log line 109 """ 110 return default_format_line(job, lineCount = self._lineCount)
111