-
Notifications
You must be signed in to change notification settings - Fork 53
/
loggingrepy.py
executable file
·122 lines (82 loc) · 3.27 KB
/
loggingrepy.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
"""
Author: Conrad Meyer
Start Date: 30 Nov 2009
Description:
Wrapper around loggingrepy_core that provides restriction management
and nannying.
"""
import nanny
import loggingrepy_core
get_size = loggingrepy_core.get_size
myfile = loggingrepy_core.myfile
class flush_logger(loggingrepy_core.flush_logger_core):
"""
A file-like class that can be used in lieu of stdout. It always flushes
data after a write. This one uses nanny.
"""
def write(self, writeitem):
# block if already over
nanny.tattle_quantity('lograte', 0)
# do the actual write
loggingrepy_core.flush_logger_core.write(self, writeitem)
# block if over after log write
writeamt = len(str(writeitem))
nanny.tattle_quantity('lograte', writeamt)
def writelines(self, writelist):
# block if already over
nanny.tattle_quantity('lograte', 0)
# do the actual writelines()
loggingrepy_core.flush_logger_core.writelines(self, writelist)
# block if over after log write
writeamt = 0
for writeitem in writelist:
writeamt = writeamt + len(str(writeitem))
nanny.tattle_quantity('lograte', writeamt)
class circular_logger(loggingrepy_core.circular_logger_core):
"""
A file-like class that writes to what is conceptually a circular buffer.
After being filled, the buffer is always >=16KB and always flushed after
write...
I accomplish this by actually writing to two files. Once >=16 KB has been
written, the first file will always* be of size 16KB and the second file
fills as the user writes. Once the second file reaches 16KB, it is
moved to overwrite the first file and a new second file is created.
*not always on some systems because moving files isn't atomic
This version of the class reports resource consumption with nanny.
"""
def __init__(self, fnp, mbs = 16*1024, use_nanny=True):
loggingrepy_core.circular_logger_core.__init__(self, fnp, mbs)
# Should we be using the nanny to limit the lograte
self.should_nanny = use_nanny
def write(self, writeitem):
# they / we can always log info (or else what happens on exception?)
# acquire (and release later no matter what)
self.writelock.acquire()
try:
if self.should_nanny:
# Only invoke the nanny if the should_nanny flag is set.
# block if already over
nanny.tattle_quantity('lograte',0)
writeamt = self.writedata(writeitem)
if self.should_nanny:
# Only invoke the nanny if the should_nanny flag is set.
nanny.tattle_quantity('lograte',writeamt)
finally:
self.writelock.release()
def writelines(self, writelist):
# we / they can always log info (or else what happens on exception?)
# acquire (and release later no matter what)
self.writelock.acquire()
try:
if self.should_nanny:
# Only invoke the nanny if the should_nanny flag is set.
# block if already over
nanny.tattle_quantity('lograte',0)
writeamt = 0
for writeitem in writelist:
writeamt = writeamt + self.writedata(writeitem)
if self.should_nanny:
# Only invoke the nanny if the should_nanny flag is set.
nanny.tattle_quantity('lograte',writeamt)
finally:
self.writelock.release()