2022-11-23 07:24:05 +00:00
|
|
|
import logging
|
2023-04-15 15:17:33 +00:00
|
|
|
import gc
|
2023-10-12 17:24:35 +00:00
|
|
|
import sys
|
2023-04-15 15:17:33 +00:00
|
|
|
|
2024-01-16 12:55:24 +00:00
|
|
|
from .utils.shared import DEBUG, DEBUG_LOGGING
|
2023-09-10 14:27:09 +00:00
|
|
|
from .utils.config import logging_settings, main_settings, read_config
|
2024-01-22 08:36:14 +00:00
|
|
|
|
2023-09-10 14:27:09 +00:00
|
|
|
read_config()
|
2023-06-20 15:07:32 +00:00
|
|
|
from . import cli
|
2023-04-18 07:02:03 +00:00
|
|
|
|
2024-01-16 10:09:01 +00:00
|
|
|
if DEBUG:
|
2023-10-24 15:41:42 +00:00
|
|
|
import sys
|
2024-01-22 08:36:14 +00:00
|
|
|
|
2023-10-24 15:41:42 +00:00
|
|
|
sys.setrecursionlimit(100)
|
2023-09-10 14:27:09 +00:00
|
|
|
|
2024-01-16 09:37:22 +00:00
|
|
|
|
|
|
|
class CustomFormatter(logging.Formatter):
|
|
|
|
grey = "\x1b[38;20m"
|
|
|
|
yellow = "\x1b[33;20m"
|
|
|
|
red = "\x1b[31;20m"
|
|
|
|
bold_red = "\x1b[31;1m"
|
|
|
|
reset = "\x1b[0m"
|
|
|
|
format = logging_settings['logging_format']
|
|
|
|
|
|
|
|
FORMATS = {
|
|
|
|
logging.DEBUG: grey + format + reset,
|
|
|
|
logging.INFO: grey + format + reset,
|
|
|
|
logging.WARNING: yellow + format + reset,
|
|
|
|
logging.ERROR: red + format + reset,
|
|
|
|
logging.CRITICAL: bold_red + format + reset
|
|
|
|
}
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
log_fmt = self.FORMATS.get(record.levelno)
|
|
|
|
formatter = logging.Formatter(log_fmt)
|
|
|
|
return formatter.format(record)
|
|
|
|
|
|
|
|
|
|
|
|
stream_handler = logging.StreamHandler()
|
|
|
|
stream_handler.setFormatter(CustomFormatter())
|
|
|
|
|
2023-09-10 14:27:09 +00:00
|
|
|
# configure logger default
|
|
|
|
logging.basicConfig(
|
2024-01-16 12:55:24 +00:00
|
|
|
level=logging_settings['log_level'] if not DEBUG_LOGGING else logging.DEBUG,
|
2023-09-10 14:27:09 +00:00
|
|
|
format=logging_settings['logging_format'],
|
|
|
|
handlers=[
|
|
|
|
logging.FileHandler(main_settings['log_file']),
|
2024-01-16 09:37:22 +00:00
|
|
|
stream_handler
|
2023-09-10 14:27:09 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
if main_settings['modify_gc']:
|
2023-04-04 20:07:56 +00:00
|
|
|
"""
|
|
|
|
At the start I modify the garbage collector to run a bit fewer times.
|
|
|
|
This should increase speed:
|
|
|
|
https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/
|
|
|
|
"""
|
|
|
|
# Clean up what might be garbage so far.
|
|
|
|
gc.collect(2)
|
|
|
|
|
|
|
|
allocs, gen1, gen2 = gc.get_threshold()
|
|
|
|
allocs = 50_000 # Start the GC sequence every 50K not 700 allocations.
|
|
|
|
gen1 = gen1 * 2
|
|
|
|
gen2 = gen2 * 2
|
|
|
|
gc.set_threshold(allocs, gen1, gen2)
|