!20561 [MS][RDR] Fix set GLOG_log_dir bugs

Merge pull request !20561 from louie5/glog
This commit is contained in:
i-robot 2021-08-10 01:41:10 +00:00 committed by Gitee
commit 5f92471380
2 changed files with 29 additions and 20 deletions

View File

@ -28,7 +28,8 @@
namespace mindspore {
std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
if (input_path.length() >= PATH_MAX) {
MS_LOG(EXCEPTION) << "The length of path: " << input_path << " exceeds limit: " << PATH_MAX;
MS_LOG(ERROR) << "The length of path: " << input_path << " exceeds limit: " << PATH_MAX;
return std::nullopt;
}
auto path_split_pos = input_path.find_last_of('/');
if (path_split_pos == std::string::npos) {
@ -46,7 +47,8 @@ std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
}
#if defined(SYSTEM_ENV_POSIX)
if (file_name.length() > NAME_MAX) {
MS_LOG(EXCEPTION) << "The length of file name : " << file_name.length() << " exceeds limit: " << NAME_MAX;
MS_LOG(ERROR) << "The length of file name : " << file_name.length() << " exceeds limit: " << NAME_MAX;
return std::nullopt;
}
if (realpath(common::SafeCStr(prefix_path), real_path) == nullptr) {
MS_LOG(ERROR) << "The dir " << prefix_path << " does not exist.";
@ -63,7 +65,8 @@ std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
// input_path is only file_name
#if defined(SYSTEM_ENV_POSIX)
if (input_path.length() > NAME_MAX) {
MS_LOG(EXCEPTION) << "The length of file name : " << input_path.length() << " exceeds limit: " << NAME_MAX;
MS_LOG(ERROR) << "The length of file name : " << input_path.length() << " exceeds limit: " << NAME_MAX;
return std::nullopt;
}
if (realpath(common::SafeCStr(input_path), real_path) == nullptr) {
MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created.";
@ -145,7 +148,7 @@ std::optional<std::string> Common::GetConfigFile(const std::string &env) {
bool Common::IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message) {
auto len_str = str.length();
if (len_str > length_limit) {
MS_LOG(WARNING) << error_message << "The length is " << str.length() << ", exceeding the limit of " << length_limit
MS_LOG(ERROR) << error_message << "The length is " << str.length() << ", exceeding the limit of " << length_limit
<< ".";
return false;
}
@ -198,14 +201,16 @@ bool Common::IsPathValid(const std::string &path, size_t length_limit, const std
return false;
}
if (!std::all_of(path.begin(), path.end(),
[](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) {
MS_LOG(WARNING) << err_msg << "The path only supports alphabets, digit or {'-', '_', '/'}, but got:" << path << ".";
if (!std::all_of(path.begin(), path.end(), [](char c) {
return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.' || c == '/';
})) {
MS_LOG(ERROR) << err_msg << "The path only supports alphabets, digit or {'-', '_', '.', '/'}, but got:" << path
<< ".";
return false;
}
if (path[0] != '/') {
MS_LOG(WARNING) << err_msg << "The path only supports absolute path and should start with '/'.";
MS_LOG(ERROR) << err_msg << "The path only supports absolute path and should start with '/'.";
return false;
}
@ -229,10 +234,9 @@ bool Common::IsFilenameValid(const std::string &filename, size_t length_limit, c
if (!IsStrLengthValid(filename, length_limit, err_msg)) {
return false;
}
if (!std::all_of(filename.begin(), filename.end(),
[](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; })) {
MS_LOG(WARNING) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
auto func = [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; };
if (!std::all_of(filename.begin(), filename.end(), func)) {
MS_LOG(ERROR) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
<< ".";
return false;
}
@ -301,16 +305,19 @@ struct GlogLogDirRegister {
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.
// While 'GLOG_logtostderr' = 0, logs output to files. 'GLOG_log_dir' must be specified as the path of log files.
// Here can not throw exception and use python to catch, because the PYBIND11_MODULE is not yet been initialed.
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";
MS_LOG(ERROR) << "The path of log files, which set by 'GLOG_log_dir', is invalid";
exit(EXIT_FAILURE);
} else if (!Common::CreateNotExistDirs(real_log_dir_str.value())) {
MS_LOG(EXCEPTION) << "Create the path of log files, set by 'GLOG_log_dir', failed.";
MS_LOG(ERROR) << "Create the path of log files, which set by 'GLOG_log_dir', failed.";
exit(EXIT_FAILURE);
}
} else if (logtostderr_str == "0") {
MS_LOG(EXCEPTION) << "The path of log files, set by 'GLOG_log_dir', is invalid.";
MS_LOG(ERROR) << "The path of log files, which set by 'GLOG_log_dir', is invalid.";
exit(EXIT_FAILURE);
}
}
}

View File

@ -437,7 +437,9 @@ void common_log_init(void) {
if (logtostderr.empty()) {
FLAGS_logtostderr = true;
} else if (logtostderr == "0" && mindspore::GetEnv("GLOG_log_dir").empty()) {
MS_LOG(EXCEPTION) << "`GLOG_log_dir` is empty, it must be set while 'logtostderr' equals to 0.";
MS_LOG(ERROR) << "`GLOG_log_dir` is empty, it must be set while 'logtostderr' equals to 0.";
// Here can not throw exception and use python to catch, because the PYBIND11_MODULE is not yet been initialed.
exit(EXIT_FAILURE);
}
// default GLOG_stderrthreshold level to WARNING