File size: 1,421 Bytes
f499d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import List

login_time = ''
log_filepath = ''

class Entry:
    def __init__(self, entry_name):
        self.entry = entry_name
        self.error = None
        self.warning = []
    
    def have_error(self):
        return self.error != None
    
    def have_warning(self):
        return len(self.warning) != 0

logs: List[Entry] = []

def new_log(path, log_name):
    global login_time, log_filepath
    log_filepath = os.path.join(path, f"{log_name}.txt")
    os.makedirs(path, exist_ok=True)
    with open(log_filepath, 'a') as file:
        file.write(f"Log: {log_name}\n")

def end_log():
    global log_filepath
    with open(log_filepath, 'a') as file:
        file.write(f"End of file\n")

def new_entry(entry_name):
    global log_filepath
    print(f"\033[32mNow processing {entry_name}...\033[0m")
    logs.append(Entry(entry_name))

def add_error(error):
    global log_filepath
    print(f"\033[31mError found when processing {logs[-1].entry}: {error}\033[0m")
    logs[-1].error = error
    with open(log_filepath, 'a') as file:
        file.write(f"Entry: {logs[-1].entry}, Error: {error}\n")

def add_warning(warning):
    global log_filepath
    print(f"\033[33mWarning found when processing {logs[-1].entry}: {warning}\033[0m")
    logs[-1].warning.append(warning)
    with open(log_filepath, 'a') as file:
        file.write(f"Entry: {logs[-1].entry}, Warning: {warning}\n")