forked from Tribler/dispersy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdprint.py
925 lines (819 loc) · 39.1 KB
/
dprint.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
"""
Easily print a message to the console or a remote application
It is important to note that this module must be as independent from
other user made modules as possible since dprint is often used to
report bugs in other modules.
"""
# todo: maybe add a feature that does not redisplay a msg that repeats itself again and again
# from pickle import dumps
from os.path import dirname, basename, expanduser, isfile, join
from sys import stdout, stderr, exc_info
# from threading import current_thread, Thread, Lock
from time import time, strftime
from traceback import extract_stack, print_exception, print_stack, format_list
import inspect
import re
# import socket
# from os import getcwd
from pprint import pformat
# maxsize is introduced in Python 2.6
try:
from sys import maxsize
except ImportError:
from sys import maxint as maxsize
LEVEL_DEBUG = 0
LEVEL_NORMAL = 128
LEVEL_LOG = 142
LEVEL_NOTICE = 167
LEVEL_WARNING = 192
LEVEL_ERROR = 224
LEVEL_FORCE = 1024
level_map = {"debug":LEVEL_DEBUG, # informative only to a developer
"normal":LEVEL_NORMAL, # informative to a user running from console
"log":LEVEL_LOG, # a message that is logged
"notice":LEVEL_NOTICE, # something is wrong but we can recover (external failure, we are not the cause nor can we fix this)
"warning":LEVEL_WARNING, # something is wrong but we can recover (internal failure, we are the cause and should fix this)
"error":LEVEL_ERROR, # something is wrong and recovering is impossible
"force":LEVEL_FORCE} # explicitly force this print to pass through the filter
level_tag_map = {LEVEL_DEBUG:"D",
LEVEL_NORMAL:" ",
LEVEL_LOG:"L",
LEVEL_NOTICE:"N",
LEVEL_WARNING:"W",
LEVEL_ERROR:"E",
LEVEL_FORCE:"F"}
# allows us to determine that the last dprint call was an error, and hence, that the message 'see
# stderr...' has already been printed to stdout
_last_dprint_see_stderr = False
_dprint_settings = {
"binary":False, # print a binary representation of the arguments
"box":False, # add a single line above and below the message
"box_char":"-", # when a box is added to the message use this character to generate the line
"callback":None, # optional callback. the callback is only performed if the filters accept the message. the callback result is added to the displayed message
"exception":False, # add the last occured exception, including its stacktrace, to the message
"force":False, # ignore all filters, equivalent to level="force"
"glue":"", # use this string to join() *args together
"level":LEVEL_NORMAL, # either "debug", "normal", "warning", "error", or a number in the range [0, 255]
"line":False, # add a single line above the message
"line_char":"-", # when a line is added to the message use this character to generate the line
"lines":False, # write each value on a seperate line
"meta":False, # write each value on a seperate line including metadata
"pprint":False, # pretty print arg[0] if there is only one argument, otherwize pretty print arg
"remote":False, # write message to remote logging application
"remote_host":"localhost", # when remote logging is enabled this hostname is used
"remote_port":12345, # when remote logging is enabled this port is used
"source_file":None, # force a source filename. otherwise the filename is retrieved from the callstack
"source_function":None, # force a source function. otherwise the function is retrieved from the callstack
"source_line":None, # force a source line. otherwise the line number is retrieved from the callstack
"stack":False, # add a stacktrace to the message. optionally this can be a list optained through extract_stack()
"stack_ident":None, # when stack is printed use this ident to determine the thread name
"stack_origin_modifier":-1, # modify the length of the callstack that is displayed and used to retrieve the source-filename, -function, and -line
"stderr":False, # write message to sys.stderr
"stdout":True, # write message to sys.stdout
"style":"column", # output style. either "short" or "column"
"time":False, # include a timestamp at the start of each line
"time_format":"%H:%M:%S", # the timestamp format (see strftime)
"width":80}
# We allow message filtering in a 'iptables like' fashion. Each
# messages is passed to the ENTRY chain in _filters, when a
# filter in the chain matches its target is used (accept, drop,
# continue, or jump). If no filters in a chain match the default chain
# policy (accept/True, drop/False, or return/None) is used. An
# exception to this is the ENTRY chain which may only use accept and
# drop its default.
#
# _filters contains chain-name:[policy, chain-list]
# pairs. Where policy can be accept/True, drop/False, or return/None.
#
# chain-list contains lists in the form: [function, target]. Where
# target can be accept/True, drop/False, continue/None, or
# jump/callable.
_filters = {"ENTRY":[False, []]}
_filter_entry = _filters["ENTRY"]
_filter_policy_map = {"accept":True, "drop":False, "return":None}
_filter_reverse_policy_map = {True:"accept", False:"drop", None:"return"}
_filter_reverse_target_map = {True:"accept", False:"drop", None:"continue"}
_filter_target_map = {"accept":True, "drop":False, "continue":None}
def _filter_reverse_dictionary_lookup(dic, value):
"""Returns key associated with the first value that matches VALUE"""
for key, value_ in dic.items():
if value is value_:
return key
return None
def filter_chains_get():
"""
Return a list of (chain-name, default-policy) tuples.
Where chain-name is the name of the chain. And where
default-policy is either accept, drop, or return.
"""
return [(chain, _filter_reverse_policy_map[policy]) for chain, (policy, rules) in _filters.items()]
def filter_get(chain):
"""
Return a list of (check, target, jump) tuples.
Where check is the name of the function used to check the
rule. Where target is either accept, drop, continue, or jump. And
where jump is either None or the name of the target chain.
"""
assert chain in _filters, chain
return [(function.__name__,
target in (True, False, None) and _filter_reverse_target_map[target] or "jump",
not target in (True, False, None) and _filter_reverse_dictionary_lookup(_filters, target) or None)
for function, target
in _filters[chain][1]]
def filter_chain_create(chain, policy):
"""
Create a new chain
CHAIN must indicate a non-existing chain ("ENTRY" always exists)
POLICY must be either accept, drop, or return
"""
assert not chain in _filters, "Chain \"%s\" already exists" % chain
assert policy in _filter_policy_map, "Invalid policy \"%s\"" % policy
_filters[chain] = [_filter_policy_map[policy], []]
def filter_chain_policy(chain, policy):
"""
Set the policy of an exiting chain
CHAIN must indicate an existing chain ("ENTRY" always exists)
POLICY must be either accept, drop, or return
"""
assert chain in _filters, "Unknown chain \"%s\"" % chain
assert policy in _filter_policy_map, "Invalid policy \"%s\"" % policy
_filters[chain][0] = _filter_policy_map[policy]
def filter_chain_remove(chain):
"""
Remove an existing chain
"""
assert chain in _filters, chain
# todo: also remove jumps to this chain
del _filters[chain]
def filter_add(chain, function, target, jump=None, position=maxsize):
"""
Add a filter entry to an existing chain.
CHAIN must indicate an existing chain ("ENTRY" always exists)
FUNCTION must be a callable function that returns True or False
TARGET must be either accept, drop, continue, or jump
JUMP must be an existing chain name when TARGET is jump
POSITION indicates the position in the chain to insert the rule. The default is the end of the chain
"""
assert chain in _filters, chain
assert hasattr(function, "__call__"), function
assert target == "jump" or target in _filter_target_map, "Invalid target [%s]" % target
assert target != "jump" or jump in _filters, jump
assert type(position) is int, position
if target in _filter_target_map:
target = _filter_target_map[target]
else:
target =_filters[jump]
_filters[chain][1].insert(position, [function, target])
def filter_remove(chain, position):
"""
Remove the n'th rule from an existing chain
CHAIN must indicate an existing chain ("ENTRY" always exists)
POSITION indicates the n'th rule in the chain. The first rule has number 0
"""
assert chain in _filters, chain
assert -len(_filters[chain][1]) < position < len(_filters[chain][1]), position
del _filters[chain][1][position]
def filter_add_by_source(chain, target, file=None, function=None, path=None, jump=None, position=maxsize):
"""
Helper function for filter_add to add a filter on the message source
CHAIN must indicate an existing chain ("ENTRY" always exists)
TARGET must be either accept, drop, continue, or jump
FILE indicates an optional file path. matches if: source_file.endswith(FILE)
FUNCTION indicates an optional function. matches if: source_function == FUNCTION
PATH indicates an optional path. directory seperators should be '.' and not OS dependend '/', or '\', etc. matches if: PATH in source_file
JUMP must be an existing chain name when TARGET is jump
POSITION indicates the position in the chain to insert the rule. The default is the end of the chain
At least one of FILE, FUNCTION, or PATH must be given. When more
then one are given the source message will match if all given
filters match.
"""
# assert for CHAIN is done in filter_add
# assert for TARGET is done in filter_add
# assert for POSITION is done in filter_add
# assert for JUMP is done in filter_add
assert file or function or path, "At least one of FILE, FUNCTION, or PATH must be given"
assert file is None or type(file) is str, file
assert function is None or type(function) is str, function
assert path is None or type(path) is str, path
def match(args, settings):
result = True
if file: result = result and settings["source_file"].endswith(file)
if path: result = result and path in settings["source_file"]
if function: result = result and function == settings["source_function"]
return result
if not path is None:
path = join(*path.split("."))
match.__name__ = "by_source(%s, %s, %s)" % (file, function, path)
filter_add(chain, match, target, jump=jump, position=position)
def filter_add_by_level(chain, target, exact=None, min=None, max=None, jump=None, position=maxsize):
"""
Helper function for filter_add to add a filter on the message level
CHAIN must indicate an existing chain ("ENTRY" always exists)
TARGET must be either accept, drop, continue, or jump
EXACT indicates an exact message level. matches if: level == EXACT
MIN indicates a minimal message level. matches if: MIN <= level <= MAX
MAX indicates a maximum message level. matches if: MIN <= level <= MAX
JUMP must be an existing chain name when TARGET is jump
POSITION indicates the position in the chain to insert the rule. The default is the end of the chain
It is not allowed to give MIN without MAX or vise-versa.
Either EXACT or (MIN and MAX) must be given. When both EXACT and
(MIN and MAX) are given only EXACT is used.
"""
# assert for CHAIN is done in filter_add
# assert for TARGET is done in filter_add
# assert for POSITION is done in filter_add
# assert for JUMP is done in filter_add
assert exact is None or exact in level_map or type(exact) is int, exact
assert min is None or min in level_map or type(min) is int, min
assert max is None or max in level_map or type(max) is int, max
assert (min is None and max is None) or (not min is None and not max is None), (min, max)
if exact in level_map: exact = level_map[exact]
if min in level_map: min = level_map[min]
if max in level_map: max = level_map[max]
if exact is None:
def match(args, settings):
return min <= settings["level"] <= max
else:
def match(args, settings):
return exact == settings["level"]
match.__name__ = "by_level(%s, %s, %s)" % (exact, min, max)
filter_add(chain, match, target, jump=jump, position=position)
def filter_add_by_pattern(chain, target, pattern, jump=None, position=maxsize):
"""
Helper function for filter_add to add a regular expression filter on the message
CHAIN must indicate an existing chain ("ENTRY" always exists)
TARGET must be either accept, drop, continue, or jump
PATTERN is a regular expression. matches if any: re.match(PATTERN, str(arg)) where arg is any argument to dprint
JUMP must be an existing chain name when TARGET is jump
POSITION indicates the position in the chain to insert the rule. The default is the end of the chain
"""
# assert for CHAIN is done in filter_add
# assert for TARGET is done in filter_add
# assert for POSITION is done in filter_add
# assert for JUMP is done in filter_add
assert type(pattern) is str, "Pattern must be a string [%s]" % pattern
pattern = re.compile(pattern)
def match(args, settings):
for arg in args:
if pattern.match(str(arg)):
return True
return False
match.__name__ = "by_pattern(%s)" % pattern.pattern
filter_add(chain, match, target, jump=jump, position=position)
def filter_print():
"""
Print the filter-chains and filter-rules to the stdout.
"""
for chain, policy in filter_chains_get():
print("Chain %s (policy %s)" % (chain, policy))
for check, target, jump in filter_get(chain):
if not jump: jump = ""
print("%-6s %-15s %s" % (target, jump, check))
print()
def filter_check(args, settings):
"""
Check if a message passes a specific chain
ARGS is a tuple containing the message
SETTINGS is a dictionaty in in the format of _dprint_settings
returns True when all filters pass. Otherwise returns False
"""
return _filter_check(args, settings, _filter_entry)
def _filter_check(args, settings, chain_info):
"""
Check if a message passes a specific chain
ARGS is a tuple containing the message
SETTINGS is a dictionaty in in the format of _dprint_settings
CHAIN_INFO is a list at _filters[chain-name]
returns True when all filters pass. Otherwise returns False
"""
for filter_info in chain_info[1]:
if filter_info[0](args, settings):
if filter_info[1] is True:
return True
elif filter_info[1] is False:
return False
elif filter_info[1] is None:
continue
else: # should be callable jump
result = _filter_check(args, settings, filter_info[1])
if result is None:
continue
else:
return result
return chain_info[0]
def _config_read():
"""
Read dprint.conf configuration files
Note: while we use 'normal' ini file structure we do not use the
ConfigParser that python supplies. Unfortunately ConfigParser uses
dictionaries to store the options making it unusable to us (the
filter rules are order dependend.)
"""
def get_arguments(string, conversions, glue):
"""
get_arguments("filename, function, 42", (strip, strip, int), ",")
--> ["filename", "function", 42]
get_arguments("filename", (strip, strip, int), ",")
--> ["filename", None, None]
"""
def helper(index, func):
if len(args) > index:
return func(args[index])
return None
args = string.split(glue)
return [helper(index, func) for index, func in zip(xrange(len(conversions)), conversions)]
def strip(string):
return string.strip()
re_section = re.compile("^\s*\[\s*(.+?)\s*\]\s*$")
re_option = re.compile("^\s*([^#].+?)\s*=\s*(.+?)\s*$")
re_true = re.compile("^true|t|1$")
options = []
sections = {"default":options}
for file_ in ['dprint.conf', expanduser('~/dprint.conf')]:
if isfile(file_):
line_number = 0
for line in open(file_, "r"):
line_number += 1
match = re_option.match(line)
if match:
options.append((line_number, line[:-1], match.group(1), match.group(2)))
continue
match = re_section.match(line)
if match:
section = match.group(1)
if section in sections:
options = sections[section]
else:
options = []
sections[section] = options
continue
string = ["box_char", "glue", "line_char", "remote_host", "source_file", "source_function", "style", "time_format"]
int_ = ["width", "remote_port", "source_line", "stack_origin_modifier"]
boolean = ["box", "binary", "exception", "force", "line", "lines", "meta", "pprint", "remote", "stack", "stderr", "stdout", "time"]
for line_number, line, before, after in sections["default"]:
try:
if before in string:
_dprint_settings[before] = after
elif before in int_:
if after.isdigit():
_dprint_settings[before] = int(after)
else:
raise ValueError("Not a number")
elif before in boolean:
_dprint_settings[before] = bool(re_true.match(after))
elif before == "level":
_dprint_settings["level"] = int(level_map.get(after, after))
except Exception, e:
raise Exception("Error parsing line %s \"%s\"\n%s %s" % (line_number, line, type(e), str(e)))
chains = []
for section in sections:
if section.startswith("filter "):
chain = section.split(" ", 1)[1]
filter_chain_create(chain, "return")
chains.append((section, chain))
if "filter" in sections:
chains.append(("filter", "ENTRY"))
for section, chain in chains:
for line_number, line, before, after in sections[section]:
try:
if before == "policy":
filter_chain_policy(chain, after)
else:
type_, before = before.split(" ", 1)
after, jump = get_arguments(after, (strip, strip), " ")
if type_ == "source":
file_, function, path = get_arguments(before, (strip, strip, strip), ",")
filter_add_by_source(chain, after, file=file_, function=function, path=path, jump=jump)
elif type_ == "level":
def conv(x):
if x.isdigit(): return int(x)
x = x.strip()
if x: return x
return None
exact, min_, max_ = get_arguments(before, (conv, conv, conv), ",")
filter_add_by_level(chain, after, exact=exact, min=min_, max=max_, jump=jump)
elif type_ == "pattern":
filter_add_by_pattern(chain, after, before, jump=jump)
except Exception, e:
raise Exception("Error parsing line %s \"%s\"\n%s %s" % (line_number, line, type(e), str(e)))
_config_read()
# class RemoteProtocol:
# @staticmethod
# def encode(key, value):
# """
# 1 byte (reserved)
# 1 byte with len(key)
# 2 bytes with len(value)
# n bytes with the key where n=len(key)
# m bytes with the value where m=len(value)
# """
# assert type(key) is str
# assert len(key) < 2**8
# assert type(value) is str
# assert len(value) < 2**16
# m = len(value)
# return "".join((chr(0),
# chr(len(key)),
# chr((m >> 8) & 0xFF), chr(m & 0xFF),
# key,
# value))
# @staticmethod
# def decode(data):
# """
# decode raw data.
# returns (data, messages) where data contains the remaining raw
# data and messages is a list containing (key, message) tuples.
# """
# assert type(data) is str
# size = len(data)
# messages = []
# while size >= 4:
# n = ord(data[1])
# m = ord(data[2]) << 8 | ord(data[3])
# # check if the entire message is available
# if size - 4 >= n + m:
# messages.append((data[4:4+n], (data[4+n:4+n+m], )))
# data = data[4+n+m:]
# size -= (4+n+m)
# else:
# break
# return data, messages
# class RemoteConnection(RemoteProtocol):
# __singleton = None
# __lock = Lock()
# @classmethod
# def get_instance(cls, *args, **kargs):
# if not cls.__singleton:
# cls.__lock.acquire()
# try:
# if not cls.__singleton:
# cls.__singleton = cls(*args, **kargs)
# finally:
# cls.__lock.release()
# return cls.__singleton
# @classmethod
# def send(cls, args, settings):
# remote = cls.get_instance(settings["remote_host"], settings["remote_port"])
# remote._queue.put(("dprint", (args, settings)))
# def __init__(self, host, port):
# # thread protected write buffer
# self._queue = Queue(0)
# self._address = (host, port)
# # start a thread to handle async socket communication
# thread = Thread(target=self._loop)
# thread.start()
# def _loop(self):
# # connect
# connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connection.setblocking(1)
# try:
# connection.connect(self._address)
# except:
# print >>stderr, "Could not connect to Dremote at", self._address
# raise
# # handshake
# connection.send(self.encode("protocol", "remote-dprint-1.0"))
# # send data from the queue
# while True:
# key, value = self._queue.get()
# connection.send(self.encode(key, dumps(value)))
def dprint_wrap(func):
source_file = inspect.getsourcefile(func)
source_line = inspect.getsourcelines(func)[1]
source_function = func.__name__
def wrapper(*args, **kargs):
dprint("PRE ", args, kargs, source_file=source_file, source_line=source_line, source_function=source_function)
try:
result = func(*args, **kargs)
except Exception, e:
dprint("POST", e, source_file=source_file, source_line=source_line, source_function=source_function)
raise
else:
dprint("POST", result, source_file=source_file, source_line=source_line, source_function=source_function)
return result
return wrapper
def dprint_pre(func):
source_file = inspect.getsourcefile(func)
source_line = inspect.getsourcelines(func)[1]
source_function = func.__name__
def wrapper(*args, **kargs):
dprint("PRE ", args, kargs, source_file=source_file, source_line=source_line, source_function=source_function)
return func(*args, **kargs)
return wrapper
def dprint_post(func):
source_file = inspect.getsourcefile(func)
source_line = inspect.getsourcelines(func)[1]
source_function = func.__name__
def wrapper(*args, **kargs):
try:
result = func(*args, **kargs)
return result
finally:
dprint("POST", result, source_file=source_file, source_line=source_line, source_function=source_function)
return wrapper
def dprint_wrap_object(object_, pattern="^(?!__)"):
"""
Experimental feature: add a dprint before and after each method
call to object.
"""
re_pattern = re.compile(pattern)
for name, member in inspect.getmembers(object_):
if hasattr(member, "__call__") and re_pattern.match(name):
try:
setattr(object_, member.__name__, dprint_wrap(member))
except:
dprint("Failed wrapping", member, "in object", object_)
def dprint(*args, **kargs):
"""
Create a message from ARGS and output it somewhere.
The message can be send to:
- stdout
- stderr (default)
- remote (send message and context to external program)
The message can contain a stacktrace and thread-id when kargs
contains "stack" which evaluates to True. The callstack can
be shortened (leaving of the call to extract_stack() or event
dprint()) my supplying "stack_origin_modifier" to kargs. The
default is -1 which removes the last call to extract_stack().
Each ARGS will be presented on a seperate line with meta data when
kargs contains "meta" which evaluates to True.
Each ARGS will be presented in binary format when kargs contains
"binary" which evaluates to True.
A string representation is derived from anything in ARGS using
str(). Therefore, no objects should be supplied that define a
__str__ method which causes secondary effects. Furthermore, to
reduce screen clutter, only the first 1000 characters returned by
str() are used. (an exception to this is the remote output where
everything is transfered).
Usage:
from dispersy.dprint import dprint
(Display a message "foo bar")
| if __debug__: dprint("foo", "bar")
filename:1 <module> foo bar
---
(Display a message in a function)
| def my_function():
| if __debug__: dprint("foo")
| pass
| my_function()
filename:2 my_function foo
---
(Display a value types)
| if __debug__: dprint("foo", 123, 1.5, meta=1)
filename:1 <module> (StringType, len 3) foo
filename:1 <module> (IntType) 123
filename:1 <module> (FloatType) 1.5
---
(Display a message with a callstack)
| def my_function():
| if __debug__: dprint("foo", stack=1)
| pass
| my_function()
filename:2 my_function foo
filename:2 my_function ---
filename:2 my_function Stacktrace on thread: "MainThread"
filename:2 my_function Dprint.py:470 <module>
filename:2 my_function filename.py:4 <module>
filename:2 my_function filename.py:2 my_function
---
(Display an exception)
| try:
| raise RuntimeError("Wrong")
| except:
| if __debug__: dprint("An exception occured", exception=1)
| pass
filename:4 <module> An exception occured
filename:4 <module> ---
filename:4 <module> --- Exception: <type 'exceptions.RuntimeError'> ---
filename:4 <module> Wrong
filename:4 <module> Traceback where the exception originated:
filename:4 <module> filename.py:2
---
(Display a cpu-intensive message)
| if __debug__:
| def expensive_calculation():
| import time
| time.sleep(1)
| return "moo-milk",
| dprint("foo-bar", callback=expensive_calculation)
filename:6 <module> | foo-bar moo-milk
---
"""
# ensure that kargs contains only known options
for key in kargs:
if not key in _dprint_settings:
raise ValueError("Unknown option: %s" % key)
# merge default dprint settings with kargs
# todo: it might be faster to clone _dprint_settings and call update(kargs) on it
for key, value in _dprint_settings.items():
if not key in kargs:
kargs[key] = value
# type check all kargs
# TODO
# fetch the callstack
callstack = extract_stack()[:kargs["stack_origin_modifier"]]
if callstack:
# use the callstack to determine where the call came from
if kargs["source_file"] is None: kargs["source_file"] = callstack[-1][0]
if kargs["source_line"] is None: kargs["source_line"] = callstack[-1][1]
if kargs["source_function"] is None: kargs["source_function"] = callstack[-1][2]
else:
if kargs["source_file"] is None: kargs["source_file"] = "unknown"
if kargs["source_line"] is None: kargs["source_line"] = 0
if kargs["source_function"] is None: kargs["source_function"] = "unknown"
# exlicitly force the message
if kargs["force"]:
kargs["level"] = "force"
# when level is below ERROR, apply filters on the message
if kargs["level"] in level_map: kargs["level"] = level_map[kargs["level"]]
if kargs["level"] < LEVEL_ERROR and not _filter_check(args, kargs, _filter_entry):
return
if kargs["source_file"].endswith(".py"):
short_source_file = join(basename(dirname(kargs["source_file"])), basename(kargs["source_file"][:-3]))
else:
short_source_file = join(basename(dirname(kargs["source_file"])), basename(kargs["source_file"]))
prefix = [level_tag_map.get(kargs["level"], "U")]
if kargs["time"]:
prefix.append(strftime(kargs["time_format"]))
if kargs["style"] == "short":
prefix.append("%s:%s %s " % (short_source_file, kargs["source_line"], kargs["source_function"]))
elif kargs["style"] == "column":
prefix.append("%25s:%-4s %-25s | " % (short_source_file[-25:], kargs["source_line"], kargs["source_function"][-25:]))
else:
raise ValueError("Invalid/unknown style: \"%s\"" % kargs["style"])
prefix = " ".join(prefix)
messages = []
if kargs["callback"]:
args = args + kargs["callback"]()
# print each variable in args
if kargs["binary"]:
string = kargs["glue"].join([str(v) for v in args])
messages.append(" ".join(["%08d" % int(bin(ord(char))[2:]) for char in string]))
# for index, char in zip(xrange(len(string)), string):
# messages.append("{0:3d} {1:08d} \\x{2}".format(index, int(bin(ord(char))[2:]), char.encode("HEX")))
elif kargs["meta"]:
messages.extend([dprint_format_variable(v) for v in args])
elif kargs["lines"] and len(args) == 1 and type(args[0]) in (list, tuple):
messages.extend([str(v) for v in args[0]])
elif kargs["lines"] and len(args) == 1 and type(args[0]) is dict:
messages.extend(["%s: %s" % (str(k), str(v)) for k, v in args[0].items()])
elif kargs["lines"]:
messages.extend([str(v) for v in args])
elif kargs["pprint"] and len(args) == 1:
messages.extend(pformat(args[0], width=kargs["width"]).split("\n"))
elif kargs["pprint"]:
messages.extend(pformat(args, width=kargs["width"]).split("\n"))
else:
messages.append(kargs["glue"].join([str(v) for v in args]))
# add a line of characters at the top to seperate messages
if kargs["line"]:
messages.insert(0, "".join(kargs["line_char"] * kargs["width"]))
# add a line of characters above and below to seperate messages
if kargs["box"]:
messages.insert(0, "".join(kargs["box_char"] * kargs["width"]))
messages.append("".join(kargs["box_char"] * kargs["width"]))
# always add stderr to output if level is error or exception is set
global _last_dprint_see_stderr
if kargs["level"] == LEVEL_ERROR or kargs["exception"]:
kargs["stderr"] = True
kargs["stdout"] = False
# do not outpuy 'see stderr' on consecutive dprint calls
if not _last_dprint_see_stderr:
_last_dprint_see_stderr = True
print >> stdout, prefix + "See stderr for exception"
else:
_last_dprint_see_stderr = False
if kargs["stdout"]:
print >> stdout, prefix + ("\n"+prefix).join([msg[:10000] for msg in messages])
if kargs["stack"]:
for line in format_list(callstack):
print >> stdout, line,
# if isinstance(kargs["stack"], bool):
# for line in format_list(callstack):
# print >> stdout, line,
# else:
# for line in format_list(kargs["stack"][:kargs["stack_origin_modifier"]]):
# print >> stdout, line,
if kargs["exception"]:
print_exception(*exc_info(), **{"file":stdout})
stdout.flush()
if kargs["stderr"]:
print >> stderr, prefix + ("\n"+prefix).join([msg[:10000] for msg in messages])
if kargs["stack"]:
print_stack(file=stderr)
if kargs["exception"]:
print_exception(*exc_info(), **{"file":stderr})
stderr.flush()
if kargs["remote"]:
# todo: the remote_host and remote_port are values that may change
# for each message. when this happens different connections should
# be created!
kargs["timestamp"] = time()
kargs["callstack"] = callstack
kargs["prefix"] = prefix
kargs["thread_name"] = current_thread().name
RemoteConnection.send(args, kargs)
def dprint_format_variable(v):
return "%22s %s" % (type(v), str(v))
# t = type(v)
# if t is BooleanType: return "(BooleanType) {!s}".format(v)
# if t is BufferType: return "(BufferType) {!s}".format(v)
# if t is BuiltinFunctionType: return "(BuiltinFunctionType) {!s}".format(v)
# if t is BuiltinMethodType: return "(BuiltinMethodType) {!s}".format(v)
# if t is ClassType: return "(ClassType) {!s}".format(v)
# if t is CodeType: return "(CodeType) {!s}".format(v)
# if t is ComplexType: return "(ComplexType) {!s}".format(v)
# if t is DictProxyType: return "(DictProxyType) {!s}".format(v)
# if t in (DictType, DictionaryType): return "(DictType, len {8} {!s}".format(len(v), str(v))
# if t is EllipsisType: return "(EllipsisType) {!s}".format(v)
# if t is FileType: return "(FileType) {!s}".format(v)
# if t is FloatType: return "(FloatType) {!s}".format(v)
# if t is FrameType: return "(FrameType) {!s}".format(v)
# if t is FunctionType: return "(FunctionType) {!s}".format(v)
# if t is GeneratorType: return "(GeneratorType) {!s}".format(v)
# if t is GetSetDescriptorType: return "(GetSetDescriptorType) {!s}".format(v)
# if t is InstanceType: return "(InstanceType) {!s}".format(v)
# if t is int: return "(IntType) {!s}".format(v)
# if t is LambdaType: return "(LambdaType) {!s}".format(v)
# if t is ListType: return "(ListType, len {8} {!s}".format(len(v), str(v))
# if t is LongType: return "(LongType) {!s}".format(v)
# if t is MemberDescriptorType: return "(MemberDescriptorType) {!s}".format(v)
# if t is MethodType: return "(MethodType) {!s}".format(v)
# if t is ModuleType: return "(ModuleType) {!s}".format(v)
# if t is NoneType: return "(NoneType) {!s}".format(v)
# if t is NotImplementedType: return "(NotImplementedType) {!s}".format(v)
# if t is ObjectType: return "(ObjectType) {!s}".format(v)
# if t is SliceType: return "(SliceType) {!s}".format(v)
# if t is str: return "(StringType, len {6} {!s}".format(len(v), str(v))
# if t is TracebackType: return "(TracebackType) {!s}".format(v)
# if t is TupleType: return "(TupleType, len {7} {!s}".format(len(v), str(v))
# if t is TypeType: return "(TypeType) {!s}".format(v)
# if t is UnboundMethodType: return "(UnboundMethodType) {!s}".format(v)
# if t is UnicodeType: return "(UnicodeType) {!s}".format(v)
# if t is XRangeType: return "(XRangeType) {!s}".format(v)
# # default return
# return "({22!s}) {!s}".format(t, v)
def strip_prefix(prefix, string):
if string.startswith(prefix):
return string[len(prefix):]
else:
return string
if __debug__:
if __name__ == "__main__":
dprint(1, level="error")
dprint(2, level="error")
dprint(3, level="error")
dprint("---", force=1)
dprint(1, level="error")
dprint(2, level="error")
dprint(3, level="error")
# if __name__ == "__main__":
# def examples():
# examples = [('Display a message "foo bar"', """if __debug__: dprint("foo", "bar")"""),
# ('Display a message in a function', """def my_function():
# if __debug__: dprint("foo")
# pass
# my_function()"""),
# ('Display a value types', """if __debug__: dprint("foo", 123, 1.5, meta=1)"""),
# ('Display a message with a callstack', """def my_function():
# if __debug__: dprint("foo", stack=1)
# pass
# my_function()"""),
# ('Display an exception', """try:
# raise RuntimeError("Wrong")
# except:
# if __debug__: dprint("An exception occured", exception=1)
# pass"""),
# ('Display a cpu-intensive message', """if __debug__:
# def expensive_calculation():
# import time
# time.sleep(0.1)
# return "moo-milk"
# dprint("foo-bar", callback=expensive_calculation)""")
# ]
# for title, code in examples:
# print("({})".format(title))
# print("| " + "\n| ".join(code.split("\n")))
# eval(compile(code, "filename.py", "exec"))
# print("---")
# print()
# for title, code in examples:
# print("{{{")
# print("#!python")
# print("# {}".format(title))
# print(code)
# eval(compile(code, "filename.py", "exec"))
# print("}}}")
# print()
# def filter_():
# filter_chain_policy("ENTRY", "drop")
# filter_add_by_level("ENTRY", "accept", level=LEVEL_ERROR)
# filter_add_by_level("ENTRY", "accept", min=LEVEL_WARNING, max=LEVEL_ERROR)
# filter_add_by_pattern("ENTRY", "accept", "foo")
# filter_add_by_source("ENTRY", "accept", line=644)
# filter_add_by_source("ENTRY", "accept", function="filter_")
# filter_add_by_source("ENTRY", "accept", "print.py")
# dprint("foo-bar", level=LEVEL_ERROR)
# dprint("foo-bar", level=LEVEL_WARNING)
# dprint("foo-bar")
# filter_print()