7
7
import time
8
8
import uuid
9
9
from datetime import datetime , timezone
10
+ from typing import Optional
10
11
11
12
from . import trace as logging
12
13
from .log_manifest import DeviceType , LogManifest
@@ -20,7 +21,7 @@ class LogManager(threading.Thread):
20
21
21
22
def __init__ (
22
23
self , device_id , device_type = 'UNKNOWN' , logs_base_dir = '/logs' , files = None , log_extension = '.raw' ,
23
- create_symlink = True , log_created_cmd = None , log_timestamps = True ):
24
+ create_symlink = True , log_created_cmd = None , log_timestamps = True , directory_to_reuse : Optional [ str ] = None ):
24
25
super ().__init__ (name = 'log_manager' )
25
26
26
27
self .device_id = device_id
@@ -29,11 +30,13 @@ def __init__(
29
30
self .create_symlink = create_symlink
30
31
self .log_created_cmd = log_created_cmd
31
32
self .data_filename = 'input' + log_extension
33
+ self .directory_to_reuse = directory_to_reuse
32
34
33
35
self .log_guid = None
34
36
self .creation_time = None
35
37
self .sequence_num = None
36
38
self .log_dir = None
39
+ self .timestamp_path = None
37
40
self .log_timestamps = log_timestamps
38
41
self .start_time = time .time ()
39
42
self .last_timestamp = time .time ()
@@ -54,14 +57,19 @@ def get_abs_file_path(self, relative_path):
54
57
else :
55
58
return os .path .join (self .log_dir , relative_path )
56
59
57
- def start (self ):
58
- self .logger .debug ('Starting log manager.' )
60
+ def create_log_dir (self ):
61
+ if self .directory_to_reuse :
62
+ self .log_dir = self .directory_to_reuse
63
+ if not os .path .exists (self .log_dir ):
64
+ raise IOError ("Log directory '%s' doesn't exists." % self .log_dir )
65
+ else :
66
+ return
59
67
60
68
self .log_guid = str (uuid .uuid4 ()).replace ('-' , '' )
61
69
self .creation_time = datetime .now (tz = timezone .utc )
70
+
62
71
self .log_dir = os .path .join (self .logs_base_dir , self .creation_time .strftime ('%Y-%m-%d' ), self .device_id ,
63
72
self .log_guid )
64
-
65
73
if os .path .exists (self .log_dir ):
66
74
raise IOError ("Log directory '%s' already exists." % self .log_dir )
67
75
else :
@@ -93,6 +101,10 @@ def start(self):
93
101
94
102
self ._create_manifest ()
95
103
104
+ def start (self ):
105
+ self .logger .debug ('Starting log manager.' )
106
+ if self .log_dir is None :
107
+ self .create_log_dir ()
96
108
super ().start ()
97
109
98
110
def stop (self ):
@@ -114,9 +126,9 @@ def run(self):
114
126
self .logger .debug ("Opening bin file '%s'." % path )
115
127
timestamp_file = None
116
128
if self .log_timestamps :
117
- timestamp_path = os .path .join (self .log_dir , self .data_filename + '.timestamps' )
118
- self .logger .debug ("Opening timestamp file '%s'." % timestamp_path )
119
- timestamp_file = open (timestamp_path , 'wb' )
129
+ self . timestamp_path = os .path .join (self .log_dir , self .data_filename + '.timestamps' )
130
+ self .logger .debug ("Opening timestamp file '%s'." % self . timestamp_path )
131
+ timestamp_file = open (self . timestamp_path , 'wb' )
120
132
with open (path , 'wb' ) as bin_file :
121
133
if self .log_created_cmd is not None :
122
134
try :
@@ -165,6 +177,8 @@ def _create_manifest(self):
165
177
manifest .device_type = self .device_type if self .device_type is not None else 'UNKNOWN'
166
178
167
179
manifest .channels .append (self .data_filename )
180
+ if self .timestamp_path :
181
+ manifest .channels .append (self .timestamp_path )
168
182
manifest .channels .extend (self .files )
169
183
manifest .channels .sort ()
170
184
0 commit comments