forked from vadyur/script.media.aggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
98 lines (78 loc) · 2 KB
/
log.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
try:
from xbmc import log
except:
def log(s):
print s
import inspect
prefix = 'script.media.aggregator'
import sys
try:
if len(sys.argv) > 1:
handle = int(sys.argv[1])
prefix += ': ' + sys.argv[1]
except:
pass
def debug(s, line = None):
if isinstance(s, BaseException):
print_tb(s)
return
elif isinstance(s, unicode):
s = s.encode('utf-8')
elif not isinstance(s, str):
s = str(s)
if prefix:
if line:
message = '[%s: %s] %s' % (prefix, str(line), s)
else:
message = '[%s] %s' % (prefix, s)
else:
if line:
message = '[%s] %s' % (line, s)
else:
message = s
log(message)
def print_tb(e=None):
import sys
exc_type, exc_val, exc_tb = sys.exc_info()
import traceback
traceback.print_exception(exc_type, exc_val, exc_tb, limit=10, file=sys.stderr)
if e:
debug(str(e))
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
def fprint_tb(filename):
import sys, filesystem
exc_type, exc_val, exc_tb = sys.exc_info()
import traceback
with filesystem.fopen(filename, 'w') as out:
traceback.print_exception(exc_type, exc_val, exc_tb, limit=10, file=out)
class dump_context:
def __init__(self, module, use_timestamp=True):
self.module = module
self.use_timestamp = use_timestamp
def timestamp(self):
if self.use_timestamp:
import time
return time.strftime('%Y%m%d_%H%M%S')
else:
return ''
def filename(self):
import filesystem
name = self.module + self.timestamp() + '.log'
try:
from xbmc import translatePath
_filename = 'special://logpath/' + name
except ImportError:
_filename = filesystem.abspath(filesystem.join( __file__ ,'../../..', name))
return _filename
def __enter__(self):
fn = self.filename()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
import filesystem
with filesystem.fopen(self.filename(), 'w') as out:
import traceback
traceback.print_exception(exc_type, exc_val, exc_tb, limit=10, file=out)
return True