-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogutils.py
104 lines (84 loc) · 2.73 KB
/
logutils.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
def print_log_record_on_error(func):
def wrap(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except:
import sys
print >>sys.stderr, "Unable to create log message msg=%r, args=%r " % (
getattr(self, 'msg', '?'), getattr(self, 'args', '?'))
raise
return wrap
import logging
logging.LogRecord.getMessage = print_log_record_on_error(logging.LogRecord.getMessage)
import logging.config
import os, os.path, subprocess
import re
def init_log(log_path, cls, mode='a'):
# print "log_path: %s, log_file: %s"%(log_path, log_path%cls)
# print "mode:", mode
# Setup a config file
nlog = log_path%cls
nlogcfg = nlog+'.cfg'
with open('blank.log.cfg', 'r') as f:
blog = f.read()
blog2 = re.sub("'blank.log','a'", "'%s','%s'"%(log_path%cls, mode), blog)
with open(nlogcfg, 'w') as f:
f.write(blog2)
# Setup logger
logging.config.fileConfig(nlogcfg, \
disable_existing_loggers=False)
log = logging.getLogger('')
# Remove config file
os.remove(nlogcfg)
f = MemuseFilter()
log.handlers[0].addFilter(f)
return log
class MemuseFilter(logging.Filter):
"""
This is a filter which injects contextual information into the log.
Rather than use actual contextual information, we just use random
data in this demo.
"""
_proc_status = '/proc/%d/status' % os.getpid()
_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
'KB': 1024.0, 'MB': 1024.0*1024.0}
def filter(self, record):
record.memuse = self.str_mem()
return True
def _VmB(self,VmKey):
'''Private.
'''
# get pseudo file /proc/<pid>/status
try:
t = open(self._proc_status)
v = t.read()
t.close()
except:
return 0.0 # non-Linux?
# get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
i = v.index(VmKey)
v = v[i:].split(None, 3) # whitespace
if len(v) < 3:
return 0.0 # invalid format?
# convert Vm value to bytes
return ''.join(v[1:3])
def memory(self):
'''Return memory usage in bytes.
'''
return self._VmB('VmSize:')
def resident(self):
'''Return resident memory usage in bytes.
'''
return self._VmB('VmRSS:')
def stacksize(self):
'''Return stack size in bytes.
'''
return self._VmB('VmStk:')
def swapsize(self):
'''Return swap size in bytes.
'''
return self._VmB('VmSwap:')
def byte_to_mb(self,byte):
return byte/(1024*1024)
def str_mem(self):
return "Tot:%s,Swap:%s"%(self.memory(),self.swapsize() )