8
8
import random
9
9
import time
10
10
11
- TIMESTAMP = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
12
- LOGS_DIR = Path (__file__ ).resolve ().parents [1 ] / "logs" / TIMESTAMP
13
- LOGS_DIR .mkdir (parents = True , exist_ok = True )
14
-
15
- EXECUTIONS_LOG = LOGS_DIR / "sim.executions.log"
16
- EVENTS_LOG = LOGS_DIR / "sim.events.log"
17
- ACTIVITY_LOG = LOGS_DIR / "sim.activity.log"
18
-
11
+ LOGS_DIR = Path (__file__ ).resolve ().parents [1 ] / ".logs"
19
12
20
13
logging .Formatter .formatTime = (
21
14
lambda self , record , datefmt = None : datetime .fromtimestamp (record .created ).isoformat (
32
25
)
33
26
34
27
28
+ def make_logger (
29
+ name : str ,
30
+ instance : str ,
31
+ formatter = DEFAULT_FORMATTER ,
32
+ level = logging .INFO ,
33
+ ):
34
+ log_file = f"{ int (time .time ())} _{ instance } "
35
+ log_path = Path (LOGS_DIR , log_file , name ).with_suffix (".log" )
36
+ log_path .parent .mkdir (parents = True , exist_ok = True )
37
+
38
+ logger = logging .getLogger (name )
39
+ file_handler = logging .FileHandler (log_path , mode = "w" )
40
+ file_handler .setFormatter (formatter )
41
+ logger .addHandler (file_handler )
42
+ logger .setLevel (level )
43
+ return logger
44
+
45
+
35
46
class TestFailure (Exception ):
36
47
"""Custom exception to signal test failures"""
37
48
@@ -45,14 +56,10 @@ class BaseEvent(Enum):
45
56
46
57
47
58
class EventManager :
48
- def __init__ (self ):
59
+ def __init__ (self , name : str ):
60
+ self .name = name
49
61
self .events = {}
50
- self .logger = logging .getLogger ("events" )
51
- file_handler = logging .FileHandler (EVENTS_LOG , mode = "w" )
52
- file_handler .setFormatter (EVENT_FORMATTER )
53
- self .logger .addHandler (file_handler )
54
- self .logger .setLevel (logging .INFO )
55
- print (f"EvenManager initialized. Logs are saved in: { LOGS_DIR } " )
62
+ self .logger = make_logger ("events" , instance = name , level = logging .INFO )
56
63
57
64
async def wait_for (self , event : BaseEvent ):
58
65
if event not in self .events :
@@ -72,22 +79,13 @@ def is_set(self, event: BaseEvent) -> bool:
72
79
73
80
74
81
class SimulatorContext :
75
- def __init__ (self , random_wait = None ):
76
- self .events = EventManager ()
82
+ def __init__ (self , name : str , random_wait = None ):
83
+ self .name = name
84
+ self .events = EventManager (name )
77
85
self .random_wait = random_wait
78
86
79
- self .logger = logging .getLogger ("activity" )
80
- file_handler = logging .FileHandler (ACTIVITY_LOG , mode = "w" )
81
- file_handler .setFormatter (DEFAULT_FORMATTER )
82
- self .logger .addHandler (file_handler )
83
- self .logger .setLevel (logging .INFO )
84
-
85
- # private logger
86
- self ._elogger = logging .getLogger ("executions" )
87
- file_handler = logging .FileHandler (EXECUTIONS_LOG , mode = "w" )
88
- file_handler .setFormatter (DEFAULT_FORMATTER )
89
- self ._elogger .addHandler (file_handler )
90
- self ._elogger .setLevel (logging .DEBUG )
87
+ self .logger = make_logger ("activity" , instance = name , level = logging .INFO )
88
+ self ._elogger = make_logger ("executions" , instance = name , level = logging .DEBUG )
91
89
92
90
def unfired_events (self , events : list [BaseEvent ]):
93
91
evts = filter (lambda e : not self .events .is_set (e ), events )
@@ -104,8 +102,11 @@ async def gather(*tasks):
104
102
105
103
106
104
class Simulator :
105
+ def __init__ (self , name : str ):
106
+ self .name = name
107
+
107
108
async def start (self , * tasks , check_events = None , random_wait = None , timeout = 60 ):
108
- context = SimulatorContext (random_wait )
109
+ context = SimulatorContext (self . name , random_wait )
109
110
results = None
110
111
111
112
try :
0 commit comments