1
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
2
# vi: set ft=python sts=4 ts=4 sw=4 noet :
4
# This file is part of Fail2Ban.
6
# Fail2Ban is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# Fail2Ban is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Fail2Ban; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
__author__ = "Yaroslav Halchenko"
22
__copyright__ = "Copyright (c) 2013 Yaroslav Halchenko"
25
import logging, os, re, traceback
26
from os.path import basename, dirname
29
# Following "traceback" functions are adopted from PyMVPA distributed
30
# under MIT/Expat and copyright by PyMVPA developers (i.e. me and
31
# Michael). Hereby I re-license derivative work on these pieces under GPL
32
# to stay in line with the main Fail2Ban license
35
"""Custom function to include directory name if filename is too common
37
Also strip .py at the end
40
if base.endswith('.py'):
42
if base in set(['base', '__init__']):
43
base = basename(dirname(s)) + '.' + base
46
class TraceBack(object):
47
"""Customized traceback to be included in debug messages
50
def __init__(self, compress=False):
51
"""Initialize TrackBack metric
56
if True then prefix common with previous invocation gets
60
self.__compress = compress
63
ftb = traceback.extract_stack(limit=100)[:-2]
64
entries = [[mbasename(x[0]), dirname(x[0]), str(x[1])] for x in ftb]
65
entries = [ [e[0], e[2]] for e in entries
66
if not (e[0] in ['unittest', 'logging.__init__']
67
or e[1].endswith('/unittest'))]
69
# lets make it more concise
70
entries_out = [entries[0]]
71
for entry in entries[1:]:
72
if entry[0] == entries_out[-1][0]:
73
entries_out[-1][1] += ',%s' % entry[1]
75
entries_out.append(entry)
76
sftb = '>'.join(['%s:%s' % (mbasename(x[0]),
77
x[1]) for x in entries_out])
79
# lets remove part which is common with previous invocation
81
common_prefix = os.path.commonprefix((self.__prev, sftb))
82
common_prefix2 = re.sub('>[^>]*$', '', common_prefix)
84
if common_prefix2 != "":
85
sftb = '...' + sftb[len(common_prefix2):]
86
self.__prev = prev_next
90
class FormatterWithTraceBack(logging.Formatter):
91
"""Custom formatter which expands %(tb) and %(tbc) with tracebacks
93
TODO: might need locking in case of compressed tracebacks
95
def __init__(self, fmt, *args, **kwargs):
96
logging.Formatter.__init__(self, fmt=fmt, *args, **kwargs)
97
compress = '%(tbc)s' in fmt
98
self._tb = TraceBack(compress=compress)
100
def format(self, record):
101
record.tbc = record.tb = self._tb()
102
return logging.Formatter.format(self, record)