Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52ac9c1

Browse files
Refactor logging and enable logging to file.
This commit adds the command line options "-o" and "--logfile" that enable logging to file. This functionality required minor restructuring that led to a simplification of the logging code. In particular: - We get rid of log.py because it was never really necessary. - We use named loggers, so it's easier to figure out what module logged a given message. - We rename our logging object from "logger" to "log" to make the code more readable. - The restructuring means that we now also get to see Stem's logging messages.
1 parent 0776550 commit 52ac9c1

16 files changed

+201
-237
lines changed

ChangeLog

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
2016-XX-XX: Changes in version YYYY.MM.DD:
1+
2016-04-21: Changes in version 2016.04.21:
22
- Add command line option '-E' for file containing exit fingerprints.
33
- Add command line option '-n' for random delays between circuit creations.
44
- Add username to data directory, allowing multiple users on a single
55
machine to run exitmap in parallel.
66
- Make dnspoison module populate its A records automatically.
77
- Add cloudflared module to check if a site is behind CloudFlare.
88
- Add rtt module to measure round-trip times from an exit to a destination.
9+
Thanks to Zack Weinberg for the code.
910
- Add dnssec module to check if an exit relay validates DNSSEC.
11+
- Improved logging now shows module names and Stem's log messages.
12+
- Add command line option '-o' to log to file.
1013

1114
2015-08-23: Changes in version 2015.08.23:
1215
- Exclude bad exits by default when selecting exit relays.

src/command.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
import subprocess
2626
import tempfile
2727
import pprint
28+
import logging
2829

29-
import log
3030
import util
3131
import torsocks
3232
import error
3333

34-
logger = log.get_logger()
34+
log = logging.getLogger(__name__)
3535

3636

3737
def run_python_over_tor(queue, circ_id, socks_port):
@@ -49,7 +49,7 @@ def closure(func, *args):
4949
with torsocks.MonkeyPatchedSocket(queue, circ_id, socks_port):
5050
func(*args)
5151
except (error.SOCKSv5Error, socket.error) as err:
52-
logger.info(err)
52+
log.info(err)
5353
return
5454

5555
return closure
@@ -132,16 +132,16 @@ def execute(self, command, timeout=10, output_callback=None):
132132

133133
with tempfile.NamedTemporaryFile(prefix="torsocks_") as fd:
134134

135-
logger.debug("Created temporary torsocks config file %s" % fd.name)
135+
log.debug("Created temporary torsocks config file %s" % fd.name)
136136
os.environ["TORSOCKS_CONF_FILE"] = fd.name
137137
os.environ["TORSOCKS_LOG_LEVEL"] = "5"
138138

139139
fd.write("TorPort %d\n" % self.socks_port)
140140
fd.write("TorAddress 127.0.0.1\n")
141141
fd.flush()
142142

143-
logger.debug("Invoking \"%s\" in environment:\n%s" %
144-
(" ".join(command), pprint.pformat(dict(os.environ))))
143+
log.debug("Invoking \"%s\" in environment:\n%s" %
144+
(" ".join(command), pprint.pformat(dict(os.environ))))
145145

146146
thread = threading.Thread(target=self.invoke_process,
147147
args=(command,))
@@ -152,7 +152,7 @@ def execute(self, command, timeout=10, output_callback=None):
152152
# Attempt to kill the process if it did not finish in time.
153153

154154
if thread.is_alive():
155-
logger.debug("Killing process after %d seconds." % timeout)
155+
log.debug("Killing process after %d seconds." % timeout)
156156
self.process.kill()
157157
thread.join()
158158

src/eventhandler.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
import threading
2525
import multiprocessing
2626
import socket
27+
import logging
2728

2829
import stem
2930
from stem import StreamStatus
3031
from stem import CircStatus
3132

3233
import command
3334
import util
34-
import log
3535

36-
logger = log.get_logger()
36+
log = logging.getLogger(__name__)
3737

3838

3939
def get_relay_desc(controller, fpr):
@@ -45,11 +45,11 @@ def get_relay_desc(controller, fpr):
4545
try:
4646
desc = controller.get_server_descriptor(relay=fpr)
4747
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))
4949
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))
5151
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)
5353

5454
return desc
5555

@@ -105,20 +105,20 @@ def prepare(self, port, circuit_id=None, stream_id=None):
105105
stream_id=stream_id)
106106
self.unattached[port] = partially_attached
107107

108-
logger.debug("Pending attachers: %d." % len(self.unattached))
108+
log.debug("Pending attachers: %d." % len(self.unattached))
109109

110110
def _attach(self, stream_id=None, circuit_id=None):
111111
"""
112112
Attach a stream to a circuit.
113113
"""
114114

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))
117117

118118
try:
119119
self.controller.attach_stream(stream_id, circuit_id)
120120
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)
122122

123123

124124
def module_closure(queue, module, circ_id, *module_args, **module_kwargs):
@@ -137,7 +137,7 @@ def func():
137137
try:
138138
module(*module_args, **module_kwargs)
139139

140-
logger.debug("Informing event handler that module finished.")
140+
log.debug("Informing event handler that module finished.")
141141
queue.put((circ_id, None))
142142
except KeyboardInterrupt:
143143
pass
@@ -180,32 +180,31 @@ def queue_reader(self):
180180
circuits.
181181
"""
182182

183-
logger.debug("Starting thread to read from IPC queue.")
183+
log.debug("Starting thread to read from IPC queue.")
184184

185185
while True:
186186
try:
187187
circ_id, sockname = self.queue.get()
188188
except EOFError:
189-
logger.debug("IPC queue terminated.")
189+
log.debug("IPC queue terminated.")
190190
break
191191

192192
# Over the queue, a module can either signal that it finished
193193
# execution (by sending (circ_id,None)) or that it is ready to have
194194
# its stream attached to a circuit (by sending (circ_id,sockname)).
195195

196196
if sockname is None:
197-
logger.debug("Closing finished circuit %s." % circ_id)
197+
log.debug("Closing finished circuit %s." % circ_id)
198198
try:
199199
self.controller.close_circuit(circ_id)
200200
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)
202202

203203
self.stats.finished_streams += 1
204204
self.stats.print_progress()
205205
self.check_finished()
206206
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)))
209208
port = int(sockname[1])
210209
self.attacher.prepare(port, circuit_id=circ_id)
211210
self.check_finished()
@@ -232,25 +231,24 @@ def check_finished(self):
232231
(self.stats.successful_circuits -
233232
self.stats.failed_circuits))
234233

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))
241239

242240
if circs_done and streams_done:
243241
self.already_finished = True
244242

245243
for proc in multiprocessing.active_children():
246-
logger.debug("Terminating remaining PID %d." % proc.pid)
244+
log.debug("Terminating remaining PID %d." % proc.pid)
247245
proc.terminate()
248246

249247
if hasattr(self.module, "teardown"):
250-
logger.debug("Calling module's teardown() function.")
248+
log.debug("Calling module's teardown() function.")
251249
self.module.teardown()
252250

253-
logger.info(self.stats)
251+
log.info(self.stats)
254252
sys.exit(0)
255253

256254
def new_circuit(self, circ_event):
@@ -266,8 +264,8 @@ def new_circuit(self, circ_event):
266264

267265
last_hop = circ_event.path[-1]
268266
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)
271269

272270
run_cmd_over_tor = command.Command(self.queue,
273271
circ_event.id,
@@ -305,11 +303,11 @@ def new_stream(self, stream_event):
305303

306304
port = util.get_source_port(str(stream_event))
307305
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))
310308
return
311309

312-
logger.debug("Adding attacher for new stream %s." % stream_event.id)
310+
log.debug("Adding attacher for new stream %s." % stream_event.id)
313311
self.attacher.prepare(port, stream_id=stream_event.id)
314312
self.check_finished()
315313

@@ -325,4 +323,4 @@ def new_event(self, event):
325323
self.new_stream(event)
326324

327325
else:
328-
logger.warning("Received unexpected event %s." % str(event))
326+
log.warning("Received unexpected event %s." % str(event))

0 commit comments

Comments
 (0)