File size: 2,728 Bytes
bb7f1f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""	Module for logging and debugging"""	
import inspect
from pprint import pprint
from typing import Any

class Logger():
    """
        Log class with different styled logs.
        debugging can be enabled/disabled for the whole instance
    """

    def __init__(self) -> None:
        self.debug = False

    def log_caller(self) -> None:
        """Print the caller of the function"""	
        caller_frame = inspect.currentframe().f_back.f_back  # type: ignore
        caller_function_name = caller_frame.f_code.co_name # type: ignore
        caller_self = caller_frame.f_locals.get('self', None) # type: ignore
        if caller_self is not None:
            caller_class_name = caller_self.__class__.__name__
            print(f"\tat: {caller_class_name}.{caller_function_name}\n")
        else:
            print(f"\tat: {caller_function_name}\n")


    def debug_log(self, msg: str, debug: bool = False) -> None:
        """Print a debug message if debugging is enabled	

        Args:
            msg (str): the message to print
            debug (bool, optional): if True, the message will be printed regardless of the debugging state. 
            Defaults to False.
        """
        if self.debug or debug:
            print(f"\n\tDEBUG: {msg}")
            self.log_caller()

    def pretty_debug_log(self, msg: Any, debug: bool = False) -> None:
        """Print a debug message with pprint if debugging is enabled

        Args:
            msg (Any): the message to print
            debug (bool, optional): if True, the message will be printed regardless of the debugging state.
        """
        if self.debug or debug:
            print("\n\n\n")
            pprint(msg)
            self.log_caller()


    def log_info(self, msg: str) -> None:
        """Print an info message

        Args:
            msg (str): the message to print
        """
        print(f"INFO: Batch-Checkpoint-Prompt: {msg}")

    def debug_print_attributes(self, obj: Any, debug: bool = False) -> None:
        """Print the attributes of an object

        Args:
            obj (Any): the object to print the attributes from
        """
        print("Atributes: ")
        if self.debug or debug:
            attributes = dir(obj)
            for attribute in attributes:
                if not attribute.startswith("__"):
                    value = getattr(obj, attribute)
                    if not callable(value):  # Exclude methods
                        try:
                            print(f"{attribute}:")
                            pprint(value)
                        except:
                            print(f"{attribute}: {value}\n")
            print(f"\n{type(obj)}\n")
            self.log_caller()