24
24
import threading
25
25
import multiprocessing
26
26
import socket
27
+ import logging
27
28
28
29
import stem
29
30
from stem import StreamStatus
30
31
from stem import CircStatus
31
32
32
33
import command
33
34
import util
34
- import log
35
35
36
- logger = log . get_logger ( )
36
+ log = logging . getLogger ( __name__ )
37
37
38
38
39
39
def get_relay_desc (controller , fpr ):
@@ -45,11 +45,11 @@ def get_relay_desc(controller, fpr):
45
45
try :
46
46
desc = controller .get_server_descriptor (relay = fpr )
47
47
except stem .DescriptorUnavailable as err :
48
- logger .warning ("Descriptor for %s not available: %s" % (fpr , err ))
48
+ log .warning ("Descriptor for %s not available: %s" % (fpr , err ))
49
49
except stem .ControllerError as err :
50
- logger .warning ("Unable to query for %d: %s" % (fpr , err ))
50
+ log .warning ("Unable to query for %d: %s" % (fpr , err ))
51
51
except ValueError :
52
- logger .warning ("%s is malformed. Is it a relay fingerprint?" % fpr )
52
+ log .warning ("%s is malformed. Is it a relay fingerprint?" % fpr )
53
53
54
54
return desc
55
55
@@ -105,20 +105,20 @@ def prepare(self, port, circuit_id=None, stream_id=None):
105
105
stream_id = stream_id )
106
106
self .unattached [port ] = partially_attached
107
107
108
- logger .debug ("Pending attachers: %d." % len (self .unattached ))
108
+ log .debug ("Pending attachers: %d." % len (self .unattached ))
109
109
110
110
def _attach (self , stream_id = None , circuit_id = None ):
111
111
"""
112
112
Attach a stream to a circuit.
113
113
"""
114
114
115
- logger .debug ("Attempting to attach stream %s to circuit %s." %
116
- (stream_id , circuit_id ))
115
+ log .debug ("Attempting to attach stream %s to circuit %s." %
116
+ (stream_id , circuit_id ))
117
117
118
118
try :
119
119
self .controller .attach_stream (stream_id , circuit_id )
120
120
except stem .OperationFailed as err :
121
- logger .warning ("Failed to attach stream because: %s" % err )
121
+ log .warning ("Failed to attach stream because: %s" % err )
122
122
123
123
124
124
def module_closure (queue , module , circ_id , * module_args , ** module_kwargs ):
@@ -137,7 +137,7 @@ def func():
137
137
try :
138
138
module (* module_args , ** module_kwargs )
139
139
140
- logger .debug ("Informing event handler that module finished." )
140
+ log .debug ("Informing event handler that module finished." )
141
141
queue .put ((circ_id , None ))
142
142
except KeyboardInterrupt :
143
143
pass
@@ -180,32 +180,31 @@ def queue_reader(self):
180
180
circuits.
181
181
"""
182
182
183
- logger .debug ("Starting thread to read from IPC queue." )
183
+ log .debug ("Starting thread to read from IPC queue." )
184
184
185
185
while True :
186
186
try :
187
187
circ_id , sockname = self .queue .get ()
188
188
except EOFError :
189
- logger .debug ("IPC queue terminated." )
189
+ log .debug ("IPC queue terminated." )
190
190
break
191
191
192
192
# Over the queue, a module can either signal that it finished
193
193
# execution (by sending (circ_id,None)) or that it is ready to have
194
194
# its stream attached to a circuit (by sending (circ_id,sockname)).
195
195
196
196
if sockname is None :
197
- logger .debug ("Closing finished circuit %s." % circ_id )
197
+ log .debug ("Closing finished circuit %s." % circ_id )
198
198
try :
199
199
self .controller .close_circuit (circ_id )
200
200
except stem .InvalidArguments as err :
201
- logger .debug ("Could not close circuit because: %s" % err )
201
+ log .debug ("Could not close circuit because: %s" % err )
202
202
203
203
self .stats .finished_streams += 1
204
204
self .stats .print_progress ()
205
205
self .check_finished ()
206
206
else :
207
- logger .debug ("Read from queue: %s, %s" % (circ_id ,
208
- str (sockname )))
207
+ log .debug ("Read from queue: %s, %s" % (circ_id , str (sockname )))
209
208
port = int (sockname [1 ])
210
209
self .attacher .prepare (port , circuit_id = circ_id )
211
210
self .check_finished ()
@@ -232,25 +231,24 @@ def check_finished(self):
232
231
(self .stats .successful_circuits -
233
232
self .stats .failed_circuits ))
234
233
235
- logger .debug ("failedCircs=%d, builtCircs=%d, totalCircs=%d, "
236
- "finishedStreams=%d" % (
237
- self .stats .failed_circuits ,
238
- self .stats .successful_circuits ,
239
- self .stats .total_circuits ,
240
- self .stats .finished_streams ))
234
+ log .debug ("failedCircs=%d, builtCircs=%d, totalCircs=%d, "
235
+ "finishedStreams=%d" % (self .stats .failed_circuits ,
236
+ self .stats .successful_circuits ,
237
+ self .stats .total_circuits ,
238
+ self .stats .finished_streams ))
241
239
242
240
if circs_done and streams_done :
243
241
self .already_finished = True
244
242
245
243
for proc in multiprocessing .active_children ():
246
- logger .debug ("Terminating remaining PID %d." % proc .pid )
244
+ log .debug ("Terminating remaining PID %d." % proc .pid )
247
245
proc .terminate ()
248
246
249
247
if hasattr (self .module , "teardown" ):
250
- logger .debug ("Calling module's teardown() function." )
248
+ log .debug ("Calling module's teardown() function." )
251
249
self .module .teardown ()
252
250
253
- logger .info (self .stats )
251
+ log .info (self .stats )
254
252
sys .exit (0 )
255
253
256
254
def new_circuit (self , circ_event ):
@@ -266,8 +264,8 @@ def new_circuit(self, circ_event):
266
264
267
265
last_hop = circ_event .path [- 1 ]
268
266
exit_fpr = last_hop [0 ]
269
- logger .debug ("Circuit for exit relay \" %s\" is built. "
270
- "Now invoking probing module." % exit_fpr )
267
+ log .debug ("Circuit for exit relay \" %s\" is built. "
268
+ "Now invoking probing module." % exit_fpr )
271
269
272
270
run_cmd_over_tor = command .Command (self .queue ,
273
271
circ_event .id ,
@@ -305,11 +303,11 @@ def new_stream(self, stream_event):
305
303
306
304
port = util .get_source_port (str (stream_event ))
307
305
if not port :
308
- logger .warning ("Couldn't extract source port from stream "
309
- "event: %s" % str (stream_event ))
306
+ log .warning ("Couldn't extract source port from stream "
307
+ "event: %s" % str (stream_event ))
310
308
return
311
309
312
- logger .debug ("Adding attacher for new stream %s." % stream_event .id )
310
+ log .debug ("Adding attacher for new stream %s." % stream_event .id )
313
311
self .attacher .prepare (port , stream_id = stream_event .id )
314
312
self .check_finished ()
315
313
@@ -325,4 +323,4 @@ def new_event(self, event):
325
323
self .new_stream (event )
326
324
327
325
else :
328
- logger .warning ("Received unexpected event %s." % str (event ))
326
+ log .warning ("Received unexpected event %s." % str (event ))
0 commit comments