-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
97 lines (82 loc) · 3.26 KB
/
utilities.py
File metadata and controls
97 lines (82 loc) · 3.26 KB
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
91
92
93
94
95
96
97
import inspect
import time
from datetime import datetime, timezone
import os
import psutil
from rich import print as _print
def Print(logType: str, message: str) -> None:
"""
Prints a log message with timestamp, function name, symbols wrapping the logType, and the message.
"""
try:
# Mapping of logType to symbols
logTypeSymbols = {
'SUCCESS': ('^^^', '^^^'),
'FAILURE': ('###', '###'),
'STATE': ('~~~', '~~~'),
'INFO': ('---', '---'),
'IMPORTANT': ('===', '==='),
'CRITICAL': ('***', '***'), # Changed symbols for CRITICAL
'EXCEPTION': ('!!!', '!!!'),
'WARNING': ('(((', ')))'),
'DEBUG': ('[[[', ']]]'),
'ATTEMPT': ('???', '???'),
'STARTING': ('>>>', '>>>'),
'PROGRESS': ('vvv', 'vvv'),
'COMPLETED': ('<<<', '<<<'),
}
# Mapping of logType to styles
logTypeStyles = {
'SUCCESS': 'green',
'FAILURE': 'red bold',
'STATE': 'cyan',
'INFO': 'blue',
'IMPORTANT': 'magenta',
'CRITICAL': 'red bold',
'EXCEPTION': 'red bold',
'WARNING': 'yellow',
'DEBUG': 'white',
'ATTEMPT': 'cyan',
'STARTING': 'green',
'PROGRESS': 'blue',
'COMPLETED': 'green',
}
# Get current timestamp with microseconds
current_time = time.time()
timestamp = datetime.fromtimestamp(current_time, tz=timezone.utc).isoformat(timespec='microseconds') + 'Z'
# Get symbols for the logType
logTypeUpper = logType.upper()
symbols = logTypeSymbols.get(logTypeUpper, ('', ''))
before_symbol, after_symbol = symbols
# Construct the formatted logType with symbols
formattedLogType = f"{before_symbol} {logTypeUpper} {after_symbol}"
# Apply style if available
style = logTypeStyles.get(logTypeUpper, '')
if style:
formattedLogType = f"[{style}]{formattedLogType}[/{style}]"
# Get the caller function name
caller_frame = inspect.stack()[1]
function_name = caller_frame.function
# If the caller is Print, get the next frame
if function_name == 'Print':
caller_frame = inspect.stack()[2]
function_name = caller_frame.function
# Pad the function name for alignment (optional)
functionNamePadding = 40
paddedFunctionName = function_name.ljust(functionNamePadding)
# Construct the output line
output_line = f"{timestamp} {formattedLogType} {paddedFunctionName} {message}"
# Print the output using rich
_print(output_line)
except Exception as e:
error_message = f"Something went wrong when attempting to print.\nError: {e}"
print(error_message)
def CPU_and_Mem_usage() -> str:
"""
Returns a string with the CPU usage and memory usage of the current process.
"""
current_process = psutil.Process(os.getpid())
cpu_usage = psutil.cpu_percent(interval=1)
memory_info = current_process.memory_info()
memory_usage_mb = memory_info.rss / (1024 ** 2)
return f"CPU Usage: {cpu_usage}%, Process Memory Usage: {memory_usage_mb:.2f} MB"