1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# $Id$
#
# Copyright (C) 2013--2014 Dragon Research Labs ("DRL")
# Portions copyright (C) 2009--2012 Internet Systems Consortium ("ISC")
# Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL, ISC, AND ARIN DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DRL,
# ISC, OR ARIN BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""
Logging facilities for RPKI libraries.
"""
import os
import sys
import time
import logging
import logging.handlers
import argparse
import traceback as tb
try:
have_setproctitle = False
if os.getenv("DISABLE_SETPROCTITLE") is None:
import setproctitle # pylint: disable=F0401
have_setproctitle = True
except ImportError:
pass
logger = logging.getLogger(__name__)
## @var show_python_ids
# Whether __repr__() methods should show Python id numbers
show_python_ids = False
## @var enable_tracebacks
# Whether tracebacks are enabled globally. Individual classes and
# modules may choose to override this.
enable_tracebacks = True
## @var use_setproctitle
# Whether to use setproctitle (if available) to change name shown for
# this process in ps listings (etc).
use_setproctitle = True
## @var proctitle_extra
# Extra text to include in proctitle display. By default this is the
# tail of the current directory name, as this is often useful, but you
# can set it to something else if you like. If None or the empty
# string, the extra information field will be omitted from the proctitle.
proctitle_extra = os.path.basename(os.getcwd())
class Formatter(object):
"""
Reimplementation (easier than subclassing in this case) of
logging.Formatter.
It turns out that the logging code only cares about this class's
.format(record) method, everything else is internal; so long as
.format() converts a record into a properly formatted string, the
logging code is happy.
So, rather than mess around with dynamically constructing and
deconstructing and tweaking format strings and ten zillion options
we don't use, we just provide our own implementation that supports
what we do need.
"""
converter = time.gmtime
def __init__(self, ident, handler):
self.ident = ident
self.is_syslog = isinstance(handler, logging.handlers.SysLogHandler)
def format(self, record):
return "".join(self.coformat(record)).rstrip("\n")
def coformat(self, record):
try:
if not self.is_syslog:
yield time.strftime("%Y-%m-%d %H:%M:%S ", time.gmtime(record.created))
except:
yield "[$!$Time format failed]"
try:
yield "%s[%d]: " % (self.ident, record.process)
except:
yield "[$!$ident format failed]"
try:
if isinstance(record.context, (str, unicode)):
yield record.context + " "
else:
yield repr(record.context) + " "
except AttributeError:
pass
except:
yield "[$!$context format failed]"
try:
yield record.getMessage()
except:
yield "[$!$record.getMessage() failed]"
try:
if record.exc_info:
if self.is_syslog or not enable_tracebacks:
lines = tb.format_exception_only(record.exc_info[0], record.exc_info[1])
lines.insert(0, ": ")
else:
lines = tb.format_exception(record.exc_info[0], record.exc_info[1], record.exc_info[2])
lines.insert(0, "\n")
for line in lines:
yield line
except:
yield "[$!$exception formatting failed]"
def argparse_setup(parser, default_thunk = None):
"""
Set up argparse stuff for functionality in this module.
Default logging destination is syslog, but you can change this
by setting default_thunk to a callable which takes no arguments
and which returns a instance of a logging.Handler subclass.
Also see rpki.log.init().
"""
class LogLevelAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
setattr(namespace, self.dest, getattr(logging, values.upper()))
parser.add_argument("--log-level", default = logging.WARNING, action = LogLevelAction,
choices = ("debug", "info", "warning", "error", "critical"),
help = "how verbosely to log")
group = parser.add_mutually_exclusive_group()
syslog_address = "/dev/log" if os.path.exists("/dev/log") else ("localhost", logging.handlers.SYSLOG_UDP_PORT)
class SyslogAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
namespace.log_handler = lambda: logging.handlers.SysLogHandler(address = syslog_address, facility = values)
group.add_argument("--log-syslog", nargs = "?", const = "daemon", action = SyslogAction,
choices = sorted(logging.handlers.SysLogHandler.facility_names.keys()),
help = "send logging to syslog")
class StreamAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
namespace.log_handler = lambda: logging.StreamHandler(stream = self.const)
group.add_argument("--log-stderr", nargs = 0, action = StreamAction, const = sys.stderr,
help = "send logging to standard error")
group.add_argument("--log-stdout", nargs = 0, action = StreamAction, const = sys.stdout,
help = "send logging to standard output")
class WatchedFileAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
namespace.log_handler = lambda: logging.handlers.WatchedFileHandler(filename = values)
group.add_argument("--log-file", action = WatchedFileAction,
help = "send logging to a file, reopening if rotated away")
class RotatingFileAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
namespace.log_handler = lambda: logging.handlers.RotatingFileHandler(
filename = values[0],
maxBytes = int(values[1]) * 1024,
backupCount = int(values[2]))
group.add_argument("--log-rotating-file", action = RotatingFileAction,
nargs = 3, metavar = ("FILENAME", "KBYTES", "COUNT"),
help = "send logging to rotating file")
class TimedRotatingFileAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
namespace.log_handler = lambda: logging.handlers.TimedRotatingFileHandler(
filename = values[0],
interval = int(values[1]),
backupCount = int(values[2]),
when = "H",
utc = True)
group.add_argument("--log-timed-rotating-file", action = TimedRotatingFileAction,
nargs = 3, metavar = ("FILENAME", "HOURS", "COUNT"),
help = "send logging to timed rotating file")
if default_thunk is None:
default_thunk = lambda: logging.handlers.SysLogHandler(address = syslog_address, facility = "daemon")
parser.set_defaults(log_handler = default_thunk)
def init(ident = None, args = None):
"""
Initialize logging system.
Default logging destination is stderr if "args" is not specified.
"""
if ident is None:
ident = os.path.basename(sys.argv[0])
if args is None:
args = argparse.Namespace(log_level = logging.WARNING,
log_handler = logging.StreamHandler)
handler = args.log_handler() # pylint: disable=E1101
handler.setFormatter(Formatter(ident, handler))
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(args.log_level) # pylint: disable=E1101
if ident and have_setproctitle and use_setproctitle:
if proctitle_extra:
setproctitle.setproctitle("%s (%s)" % (ident, proctitle_extra))
else:
setproctitle.setproctitle(ident)
def class_logger(module_logger, attribute = "logger"):
"""
Class decorator to add a class-level Logger object as a class
attribute. This allows control of debugging messages at the class
level rather than just the module level.
This decorator takes the module logger as an argument.
"""
def decorator(cls):
setattr(cls, attribute, module_logger.getChild(cls.__name__))
return cls
return decorator
def log_repr(obj, *tokens):
"""
Constructor for __repr__() strings, handles suppression of Python
IDs as needed, includes tenant_handle when available.
"""
words = ["%s.%s" % (obj.__class__.__module__, obj.__class__.__name__)]
try:
words.append("{%s}" % obj.tenant.tenant_handle)
except:
pass
for token in tokens:
if token is not None:
try:
s = str(token)
except:
s = "???"
logger.exception("Failed to generate repr() string for object of type %r", type(token))
if s:
words.append(s)
if show_python_ids:
words.append(" at %#x" % id(obj))
return "<" + " ".join(words) + ">"
def show_stack(stack_logger = None):
"""
Log a stack trace.
"""
if stack_logger is None:
stack_logger = logger
for frame in tb.format_stack():
for line in frame.split("\n"):
if line:
stack_logger.debug("%s", line.rstrip())
|