Back to index

moin  1.9.0~rc2
eventlog.py
Go to the documentation of this file.
00001 """
00002     MoinMoin event log class
00003 
00004     The global event-log is mainly used for statistics (e.g. EventStats).
00005 
00006     @copyright: 2007 MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import time
00011 
00012 from MoinMoin.logfile import LogFile
00013 from MoinMoin import wikiutil
00014 
00015 class EventLog(LogFile):
00016     """ The global event-log is mainly used for statistics (e.g. EventStats) """
00017     def __init__(self, request, filename=None, buffer_size=65536, **kw):
00018         if filename is None:
00019             rootpagename = kw.get('rootpagename', None)
00020             if rootpagename:
00021                 from MoinMoin.Page import Page
00022                 filename = Page(request, rootpagename).getPagePath('event-log', isfile=1)
00023             else:
00024                 filename = request.rootpage.getPagePath('event-log', isfile=1)
00025         LogFile.__init__(self, filename, buffer_size)
00026 
00027     def add(self, request, eventtype, values=None, add_http_info=1,
00028             mtime_usecs=None):
00029         """ Write an event of type `eventtype, with optional key/value
00030             pairs appended (i.e. you have to pass a dict).
00031         """
00032         if request.isSpiderAgent:
00033             return
00034 
00035         if mtime_usecs is None:
00036             mtime_usecs = wikiutil.timestamp2version(time.time())
00037 
00038         if values is None:
00039             values = {}
00040         if request.cfg.log_remote_addr and add_http_info:
00041             # if cfg.log_remote_addr is False (usually for privacy reasons),
00042             # we likely do not want to log user agent and http referer either.
00043             for key in ['remote_addr', 'http_user_agent', 'http_referer']:
00044                 value = getattr(request, key, '')
00045                 if value:
00046                     # Save those http headers in UPPERcase
00047                     values[key.upper()] = value
00048         # Encode values in a query string TODO: use more readable format
00049         values = wikiutil.makeQueryString(values)
00050         self._add(u"%d\t%s\t%s\n" % (mtime_usecs, eventtype, values))
00051 
00052     def parser(self, line):
00053         """ parse a event-log line into its components """
00054         try:
00055             time_usecs, eventtype, kvpairs = line.rstrip().split('\t')
00056         except ValueError:
00057             # badly formatted line in file, skip it
00058             return None
00059         return long(time_usecs), eventtype, wikiutil.parseQueryString(kvpairs)
00060 
00061     def set_filter(self, event_types=None):
00062         """ optionally filter log for specific event types """
00063         if event_types is None:
00064             self.filter = None
00065         else:
00066             self.filter = lambda line: (line[1] in event_types)
00067 
00068