|
1
|
|
|
import os |
|
2
|
|
|
import sys |
|
3
|
|
|
import time |
|
4
|
|
|
import logging |
|
5
|
|
|
|
|
6
|
|
|
from exabgp.logger.handler import getLogger |
|
7
|
|
|
from exabgp.logger.format import formater |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def echo(_): |
|
11
|
|
|
return _ |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class option(object): |
|
15
|
|
|
logger = None |
|
16
|
|
|
formater = echo |
|
17
|
|
|
|
|
18
|
|
|
short = False |
|
19
|
|
|
level = 'WARNING' |
|
20
|
|
|
logit = {} |
|
21
|
|
|
|
|
22
|
|
|
cwd = '' |
|
23
|
|
|
|
|
24
|
|
|
# where the log should go, stdout, stderr, file, syslog, ... |
|
25
|
|
|
destination = '' |
|
26
|
|
|
|
|
27
|
|
|
enabled = { |
|
28
|
|
|
'pdb': False, |
|
29
|
|
|
'reactor': False, |
|
30
|
|
|
'daemon': False, |
|
31
|
|
|
'processes': False, |
|
32
|
|
|
'configuration': False, |
|
33
|
|
|
'network': False, |
|
34
|
|
|
'wire': False, |
|
35
|
|
|
'message': False, |
|
36
|
|
|
'rib': False, |
|
37
|
|
|
'timer': False, |
|
38
|
|
|
'routes': False, |
|
39
|
|
|
'parser': False, |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
@classmethod |
|
43
|
|
|
def _set_level(cls, level): |
|
44
|
|
|
cls.level = level |
|
45
|
|
|
|
|
46
|
|
|
levels = 'FATAL CRITICAL ERROR WARNING INFO DEBUG NOTSET' |
|
47
|
|
|
index = levels.index(level) |
|
48
|
|
|
for level in levels.split(): |
|
49
|
|
|
cls.logit[level] = levels.index(level) <= index |
|
50
|
|
|
|
|
51
|
|
|
@classmethod |
|
52
|
|
|
def log_enabled(cls, source, level): |
|
53
|
|
|
return cls.enabled.get(source, True) and cls.logit.get(level, False) |
|
54
|
|
|
|
|
55
|
|
|
@classmethod |
|
56
|
|
|
def load(cls, env): |
|
57
|
|
|
cls.pid = os.getpid() |
|
58
|
|
|
cls.cwd = os.getcwd() |
|
59
|
|
|
|
|
60
|
|
|
cls.short = env.log.short |
|
61
|
|
|
|
|
62
|
|
|
cls._set_level(env.log.level) |
|
63
|
|
|
|
|
64
|
|
|
cls.option = { |
|
65
|
|
|
'pdb': env.debug.pdb, |
|
66
|
|
|
'reactor': env.log.enable and (env.log.all or env.log.reactor), |
|
67
|
|
|
'daemon': env.log.enable and (env.log.all or env.log.daemon), |
|
68
|
|
|
'processes': env.log.enable and (env.log.all or env.log.processes), |
|
69
|
|
|
'configuration': env.log.enable and (env.log.all or env.log.configuration), |
|
70
|
|
|
'network': env.log.enable and (env.log.all or env.log.network), |
|
71
|
|
|
'wire': env.log.enable and (env.log.all or env.log.packets), |
|
72
|
|
|
'message': env.log.enable and (env.log.all or env.log.message), |
|
73
|
|
|
'rib': env.log.enable and (env.log.all or env.log.rib), |
|
74
|
|
|
'timer': env.log.enable and (env.log.all or env.log.timers), |
|
75
|
|
|
'routes': env.log.enable and (env.log.all or env.log.routes), |
|
76
|
|
|
'parser': env.log.enable and (env.log.all or env.log.parser), |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
destination = env.log.destination |
|
80
|
|
|
|
|
81
|
|
|
if destination in ('stdout', 'stderr', 'syslog'): |
|
82
|
|
|
cls.destination = destination |
|
83
|
|
|
elif destination.startwith('file:'): |
|
84
|
|
|
cls.destination = destination[5:] |
|
85
|
|
|
else: |
|
86
|
|
|
cls.destination = 'stdout' |
|
87
|
|
|
|
|
88
|
|
|
@classmethod |
|
89
|
|
|
def setup(cls, env): |
|
90
|
|
|
cls.load(env) |
|
91
|
|
|
|
|
92
|
|
|
# the time is used as we will need to re-init the logger once |
|
93
|
|
|
# we have dropped root privileges so that any permission issues |
|
94
|
|
|
# can be noticed at start time (and not once we try to rotate file for example) |
|
95
|
|
|
now = str(time.time()) |
|
96
|
|
|
|
|
97
|
|
|
if cls.destination == 'stdout': |
|
98
|
|
|
cls.logger = getLogger(f'ExaBGP stdout {now}', format='%(message)s', stream=sys.stderr, level=cls.level,) |
|
99
|
|
|
cls.formater = formater(env.log.short, 'stdout') |
|
100
|
|
|
return |
|
101
|
|
|
|
|
102
|
|
|
if cls.destination == 'stdout': |
|
103
|
|
|
cls.logger = getLogger(f'ExaBGP stderr {now}', format='%(message)s', stream=sys.stderr, level=cls.level,) |
|
104
|
|
|
cls.formater = formater(env.log.short, 'stderr') |
|
105
|
|
|
return |
|
106
|
|
|
|
|
107
|
|
|
# if cls.destination == 'file': |
|
108
|
|
|
# os.path.realpath(os.path.normpath(os.path.join(cls._cwd, destination))) |
|
109
|
|
|
# _logger = getLogger('ExaBGP file', filename='') |
|
110
|
|
|
# _format = formater(cls.enabled, 'stderr') |
|
111
|
|
|
|
|
112
|
|
|
if cls.destination == 'syslog': |
|
113
|
|
|
cls.logger = getLogger( |
|
114
|
|
|
f'ExaBGP syslog {now}', |
|
115
|
|
|
format='%(message)s', |
|
116
|
|
|
address='/var/run/syslog' if sys.platform == 'darwin' else '/dev/log', |
|
117
|
|
|
level=cls.level, |
|
118
|
|
|
) |
|
119
|
|
|
cls.formater = formater(env.log.short, 'syslog') |
|
120
|
|
|
|
|
121
|
|
|
# need to re-add remote syslog |
|
122
|
|
|
|