1
|
|
|
import enarksh |
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class ChunkLogger: |
|
|
|
|
5
|
|
|
_file_count = 0 |
6
|
|
|
|
7
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
8
|
|
|
def __init__(self): |
9
|
|
|
self._chunk_count = 0 |
10
|
|
|
self._position = 0 |
11
|
|
|
self._buffer = bytearray(b' ' * enarksh.CHUNK_SIZE) |
12
|
|
|
self._filename1 = '' |
13
|
|
|
self._filename2 = '' |
14
|
|
|
|
15
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
16
|
|
|
@property |
17
|
|
|
def filename1(self): |
18
|
|
|
""" |
19
|
|
|
Getter for filename1. |
20
|
|
|
|
21
|
|
|
:rtype: str |
22
|
|
|
""" |
23
|
|
|
return self._filename1 |
24
|
|
|
|
25
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
26
|
|
|
@property |
27
|
|
|
def filename2(self): |
28
|
|
|
""" |
29
|
|
|
Getter for filename2. |
30
|
|
|
|
31
|
|
|
:rtype: str |
32
|
|
|
""" |
33
|
|
|
return self._filename2 |
34
|
|
|
|
35
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
36
|
|
|
@staticmethod |
37
|
|
|
def _get_filename(): |
|
|
|
|
38
|
|
|
ChunkLogger._file_count += 1 |
39
|
|
|
|
40
|
|
|
return '{0!s}/{1!s}/{2:010d}.log'.format(enarksh.HOME, 'var/lib/logger', ChunkLogger._file_count) |
41
|
|
|
|
42
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
43
|
|
|
def write(self, buffer): |
44
|
|
|
""" |
45
|
|
|
:param bytes buffer: |
46
|
|
|
""" |
47
|
|
|
size = len(buffer) |
48
|
|
|
pos = 0 |
49
|
|
|
|
50
|
|
|
while size > 0: |
51
|
|
|
n = min(size, enarksh.CHUNK_SIZE - self._position) |
|
|
|
|
52
|
|
|
self._buffer[self._position:self._position + n] = buffer[pos:pos + n] |
53
|
|
|
|
54
|
|
|
if n < size: |
55
|
|
|
if self._chunk_count == 0: |
56
|
|
|
self._filename1 = self._get_filename() |
57
|
|
|
with open(self._filename1, "wb") as file: |
58
|
|
|
file.write(self._buffer[:enarksh.CHUNK_SIZE]) |
59
|
|
|
|
60
|
|
|
self._position = 0 |
61
|
|
|
self._chunk_count += 1 |
62
|
|
|
else: |
63
|
|
|
self._position += n |
64
|
|
|
|
65
|
|
|
size -= n |
66
|
|
|
pos += n |
67
|
|
|
|
68
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
69
|
|
|
def get_total_log_size(self): |
|
|
|
|
70
|
|
|
return self._chunk_count * enarksh.CHUNK_SIZE + self._position |
71
|
|
|
|
72
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
73
|
|
|
def close(self): |
|
|
|
|
74
|
|
|
if self._position != 0: |
75
|
|
|
if self._chunk_count == 0: |
76
|
|
|
# Write first chunk to the file system. |
77
|
|
|
self._filename1 = self._get_filename() |
78
|
|
|
with open(self._filename1, "wb") as file: |
79
|
|
|
file.write(self._buffer[:self._position]) |
80
|
|
|
file.close() |
81
|
|
|
|
82
|
|
|
else: |
83
|
|
|
self._filename2 = self._get_filename() |
84
|
|
|
with open(self._filename2, "wb") as file: |
85
|
|
|
if self._chunk_count >= 2: |
86
|
|
|
file.write(self._buffer[self._position:enarksh.CHUNK_SIZE]) |
87
|
|
|
file.write(self._buffer[:self._position]) |
88
|
|
|
file.close() |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
92
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.