diff --git a/mindspore/ccsrc/debug/common.cc b/mindspore/ccsrc/debug/common.cc index 5927f5a7c17..d64cfd32563 100644 --- a/mindspore/ccsrc/debug/common.cc +++ b/mindspore/ccsrc/debug/common.cc @@ -293,4 +293,29 @@ bool Common::FileExists(const std::string &filepath) { f.close(); return cache_file_existed; } + +struct GlogLogDirRegister { + GlogLogDirRegister() { + const char *logtostderr = ::getenv("GLOG_logtostderr"); + const char *log_dir = ::getenv("GLOG_log_dir"); + if (logtostderr != nullptr && log_dir != nullptr) { + std::string logtostderr_str = std::string(logtostderr); + std::string log_dir_str = std::string(log_dir); + + auto real_log_dir_str = Common::GetRealPath(log_dir_str); + + // While 'GLOG_logtostderr' = 0, logs output to files. + // 'GLOG_log_dir' must be specified as the path of log files. + if (logtostderr_str == "0" && real_log_dir_str.has_value()) { + if (!Common::IsPathValid(real_log_dir_str.value(), MAX_DIRECTORY_LENGTH, "")) { + MS_LOG(EXCEPTION) << "The path of log files, set by 'GLOG_log_dir', is invalid"; + } else if (!Common::CreateNotExistDirs(real_log_dir_str.value())) { + MS_LOG(EXCEPTION) << "Create the path of log files, set by 'GLOG_log_dir', failed."; + } + } else if (logtostderr_str == "0") { + MS_LOG(EXCEPTION) << "The path of log files, set by 'GLOG_log_dir', is invalid."; + } + } + } +} _glog_log_dir_register; } // namespace mindspore diff --git a/mindspore/core/utils/log_adapter.cc b/mindspore/core/utils/log_adapter.cc index ed076fed7f9..48a1d74feef 100644 --- a/mindspore/core/utils/log_adapter.cc +++ b/mindspore/core/utils/log_adapter.cc @@ -433,8 +433,7 @@ void common_log_init(void) { if (logtostderr.empty()) { FLAGS_logtostderr = true; } else if (logtostderr == "0" && mindspore::GetEnv("GLOG_log_dir").empty()) { - FLAGS_logtostderr = true; - MS_LOG(WARNING) << "`GLOG_log_dir` is not set, output log to screen."; + MS_LOG(EXCEPTION) << "`GLOG_log_dir` is empty, it must be set while 'logtostderr' equals to 0."; } // default GLOG_stderrthreshold level to WARNING diff --git a/mindspore/log.py b/mindspore/log.py index dad398ac5c2..c69a07b23bb 100644 --- a/mindspore/log.py +++ b/mindspore/log.py @@ -17,7 +17,6 @@ log module """ import sys import os -import re import stat import time import logging @@ -290,38 +289,6 @@ def _get_env_config(): config_dict[key] = value.strip() return config_dict -def check_directory_by_regular(target, reg=None, flag=re.ASCII, prim_name=None): - """Check whether directory is legitimate.""" - if not isinstance(target, str): - raise ValueError("Args directory {} must be string, please check it".format(target)) - if reg is None: - reg = r"^[\/0-9a-zA-Z\_\-\.\:\\]+$" - if re.match(reg, target, flag) is None: - prim_name = f'in `{prim_name}`' if prim_name else "" - raise ValueError("'{}' {} is illegal, it should be match regular'{}' by flags'{}'".format( - target, prim_name, reg, flag)) - return True - -def _make_directory(path: str): - """Make directory.""" - if path is None or not isinstance(path, str) or path.strip() == "": - raise TypeError("Input path '{}' is invalid type".format(path)) - - path = os.path.realpath(path) - check_directory_by_regular(path) - if os.path.exists(path): - real_path = path - else: - try: - permissions = os.R_OK | os.W_OK | os.X_OK - os.umask(permissions << 3 | permissions) - mode = permissions << 6 - os.makedirs(path, mode=mode, exist_ok=True) - real_path = path - except PermissionError: - raise TypeError("No write permission on the directory `{path}`.") - return real_path - def _verify_config(kwargs): """ Verify log configurations. @@ -362,7 +329,8 @@ def _verify_config(kwargs): if console == _std_off and file_path is not None: file_real_path = os.path.realpath(file_path) if not os.path.exists(file_real_path): - _make_directory(file_real_path) + raise ValueError(f'The file path does not exist.' + f'{_confmap_dict["filepath"]}:{file_path}') # Check the input value of maxBytes max_bytes = kwargs.get('maxBytes', None)