File size: 2,717 Bytes
a9d25c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import logging


def create_logger(logger_name: str, log_file_path: os.PathLike = None):
    """
    Create a logger with the specified name and log file path.
    """
    logger = logging.getLogger(logger_name)
    logger.propagate = False
    logger.setLevel(logging.DEBUG)
    assert log_file_path is not None, "log_file_path is required"
    fh = logging.FileHandler(log_file_path)
    fh_formatter = logging.Formatter("%(asctime)s : %(levelname)s, %(funcName)s Message: %(message)s")
    fh.setFormatter(fh_formatter)
    logger.addHandler(fh)
    logger.info(f"logging start: {logger_name}")
    return logger


class Timer:
    """
    A simple timer class for measuring elapsed time.
    """

    def __init__(self, filename: os.PathLike = "timer_log.log", reset: bool = False):
        """
        Initialize the Timer object.
        """
        self.start_time = None
        self.last_checkpoint = None
        self.filename = filename
        self.logger = create_logger("Timer", filename)
        if reset:
            self._reset_log_file()

    def _reset_log_file(self):
        """
        Reset the log file by clearing its contents.
        """
        with open(self.filename, "w") as file:
            file.write("")

    def start(self):
        """
        Start the timer.
        """
        self.start_time = time.time()
        self.last_checkpoint = self.start_time
        self.logger.info("Timer started.")

    def check(self, message):
        """
        Log a checkpoint with the current time and time since the last checkpoint.

        Args:
            message (str): The message to include in the log.
        """
        if self.start_time is None:
            self.logger.warning("Timer has not been started.")
        else:
            log_message = (
                f"Current time count: {time.time() - self.start_time:.4f} seconds, "
                f"Time since last checkpoint: {time.time() - self.last_checkpoint:.4f} seconds, "
                f"for {message}"
            )
            self.last_checkpoint = time.time()
            self.logger.info(log_message)

    def stop(self):
        """
        Stop the timer and log the elapsed time.
        """
        if self.start_time is None:
            self.logger.warning("Timer has not been started.")
        else:
            self.end_time = time.time()
            self.logger.info(f"Total elapsed time: {self.end_time - self.start_time} seconds\n")


if __name__ == "__main__":
    # Test the Timer class
    timer = Timer(filename="timer_log.log", reset=True)
    timer.start()
    timer.check("First checkpoint")
    time.sleep(1)
    timer.check("Second checkpoint")
    timer.stop()