@@ -113,6 +113,11 @@ class Logger(logging.Logger):
113113 DEBUGWARNING = 15
114114 OFF = 100
115115
116+ #: The start position of a fragment of the log file as marked with
117+ #: L{markFragmentStart} for later retrieval using L{getFragment}.
118+ #: @type: C{long}
119+ fragmentStart = None
120+
116121 def _log (self , level , msg , args , exc_info = None , extra = None , codepath = None , activateLogViewer = False , stack_info = None ):
117122 if not extra :
118123 extra = {}
@@ -189,6 +194,65 @@ def exception(self, msg="", exc_info=True, **kwargs):
189194 return
190195 self ._log (level , msg , (), exc_info = exc_info , ** kwargs )
191196
197+ def markFragmentStart (self ):
198+ """Mark the current end of the log file as the start position of a
199+ fragment to be later retrieved by L{getFragment}.
200+ @returns: Whether a log file is in use and a position could be marked
201+ @rtype: bool
202+ """
203+ if (
204+ not globalVars .appArgs
205+ or not globalVars .appArgs .logFileName
206+ or not self .handlers
207+ or not isinstance (self .handlers [0 ], FileHandler )
208+ ):
209+ return False
210+ import codecs
211+ try :
212+ f = codecs .open (globalVars .appArgs .logFileName , "r" , encoding = "UTF-8" )
213+ # io.IOBase.seek: whence=2 -- end of stream
214+ f .seek (0 , whence = 2 )
215+ self .fragmentStart = f .tell ()
216+ return True
217+ except : # IOError is the most expected. Catch-all anyway.
218+ self .exception ()
219+ return False
220+ finally :
221+ try :
222+ f .close ()
223+ except :
224+ pass
225+
226+ def getFragment (self ):
227+ """Retrieve a fragment of the log starting from the position marked using
228+ L{markFragmentStart}.
229+ If L{fragmentStart} does not point to the current end of the log file, it
230+ is reset to C{None} after reading the fragment.
231+ @returns: The text of the fragment, or C{None} if L{fragmentStart} is None.
232+ @rtype: str
233+ """
234+ if (
235+ not globalVars .appArgs
236+ or globalVars .appArgs .secure
237+ or self .fragmentStart is None
238+ ):
239+ return None
240+ import codecs
241+ try :
242+ f = codecs .open (globalVars .appArgs .logFileName , "r" , encoding = "UTF-8" )
243+ f .seek (self .fragmentStart )
244+ fragment = f .read ()
245+ if fragment :
246+ self .fragmentStart = None
247+ return fragment
248+ except : # IOError is the most expected. Catch-all anyway.
249+ self .exception ()
250+ finally :
251+ try :
252+ f .close ()
253+ except :
254+ pass
255+
192256class RemoteHandler (logging .Handler ):
193257
194258 def __init__ (self ):
0 commit comments