-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.py
46 lines (38 loc) · 1.09 KB
/
debug.py
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
import sys, os
import datetime
DEBUG_MODE = True
SENDER_LOG = 'sender.log'
RECEIVER_LOG = 'receiver.log'
STARTING_TIME = None
# Debugging message
nprint = print
def print(*args, **kwargs):
if DEBUG_MODE:
nprint(*args, **kwargs)
# start is the starting time to get pure transmission time
def log(message, time=True):
# update time when first called
global STARTING_TIME
if (STARTING_TIME is None):
STARTING_TIME = datetime.datetime.now()
m = message
if (time):
m = '{0} : {1}\n'.format(time_diff(STARTING_TIME), message)
print(m)
if (message.startswith('[S]')):
log_file = open(SENDER_LOG, 'a')
log_file.write(m)
log_file.close()
elif (message.startswith('[R]')):
log_file = open(RECEIVER_LOG, 'a')
log_file.write(m)
log_file.close()
else:
if (time):
print("Message is '{0}'. Format is wrong please update it.")
# Fatal error
def fatal(message):
sys.exit(message)
# Get current system time in a string
def time_diff(start):
return str(datetime.datetime.now() - start)