2026-01-28 06:16:04 +00:00

149 lines
4.6 KiB
Python

import os
import shutil
import logging
import logging.config
class Log(object):
base_config = 'base_config.json'
merge_config = 'merge_config.json'
best_config = 'best_config.json'
best_profile_result = 'best_profile_result.txt'
best_compiler_result = 'best_compiler_result/'
tmp_dir = '_magic_tmp_'
def __init__(self, log_dir, output_dir):
self._log_dir = log_dir
self._output_dir = output_dir
self._create_log_dir()
self._create_output_dir()
self._logger = self._create_logger()
def __del__(self):
#shutil.rmtree(self._log_dir, ignore_errors=True)
pass
def _create_logger(self):
log_path = self.get_opt_log()
tmpl = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"normal": {
#"format": "%(asctime)s - %(filename)-15s:%(lineno)-3s - %(levelname)s: %(message)s",
"format": "%(asctime)s - %(levelname)s: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "normal",
'stream': 'ext://sys.stdout',
"level": "DEBUG"
},
"file": {
"class": "logging.FileHandler",
"formatter": "normal",
"filename": log_path,
"level": "DEBUG"
}
},
"loggers": {
'file': {
"handlers": ["file", 'console'],
'propagate': False,
"level": "DEBUG"
}
}
}
logging.config.dictConfig(tmpl)
return logging.getLogger('file')
def _create_log_dir(self):
os.makedirs(self._log_dir, exist_ok=True)
def _create_output_dir(self):
os.makedirs(self._output_dir, exist_ok=True)
def copy_files(self, model, base_config):
#shutil.copy(model, self._log_dir, follow_symlinks=True)
shutil.copyfile(base_config, '{}/{}'.format(self._log_dir, Log.base_config))
def create_module_dir(self, module):
path = '{}/{}'.format(self._log_dir, module)
os.makedirs(path, exist_ok=True)
path = '{}/{}'.format(self._output_dir, module)
os.makedirs(path, exist_ok=True)
def get_opt_log(self):
t = '{}/opt.log'.format(self._log_dir)
return os.path.abspath(t)
def get_module_log_dir(self, module):
t = '{}/{}'.format(self._log_dir, module)
return os.path.abspath(t)
def get_module_output_dir(self, module):
t = '{}/{}'.format(self._output_dir, module)
return os.path.abspath(t)
def get_module_base_config(self, module):
t = '{}/{}'.format(self.get_module_log_dir(module), Log.base_config)
return os.path.abspath(t)
def get_module_merge_config(self, module):
t = '{}/{}'.format(self.get_module_log_dir(module), Log.merge_config)
return os.path.abspath(t)
def get_module_best_config(self, module):
t = '{}/{}'.format(self.get_module_log_dir(module), Log.best_config)
return os.path.abspath(t)
def get_module_best_profile_result(self, module):
t = '{}/{}'.format(self.get_module_log_dir(module), Log.best_profile_result)
return os.path.abspath(t)
def get_module_best_compiler_result(self, module):
t = '{}/{}'.format(self.get_module_log_dir(module), Log.best_compiler_result)
return os.path.abspath(t)
@staticmethod
def get_base_config(module_path):
t = '{}/{}'.format(module_path, Log.base_config)
return os.path.abspath(t)
@staticmethod
def get_merge_config(module_path):
t = '{}/{}'.format(module_path, Log.merge_config)
return os.path.abspath(t)
@staticmethod
def get_best_config(module_path):
t = '{}/{}'.format(module_path, Log.best_config)
return os.path.abspath(t)
@staticmethod
def get_best_profile_result(module_path):
t = '{}/{}'.format(module_path, Log.best_profile_result)
return os.path.abspath(t)
@staticmethod
def get_best_compiler_result(module_path):
t = '{}/{}'.format(module_path, Log.best_compiler_result)
return os.path.abspath(t)
@property
def logger(self):
return self._logger
@property
def log_dir(self):
return self._log_dir
@property
def output_dir(self):
return self._output_dir