forked from mindspore-Ecosystem/mindspore
!20561 [MS][RDR] Fix set GLOG_log_dir bugs
Merge pull request !20561 from louie5/glog
This commit is contained in:
commit
5f92471380
|
@ -28,7 +28,8 @@
|
||||||
namespace mindspore {
|
namespace mindspore {
|
||||||
std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
|
std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
|
||||||
if (input_path.length() >= PATH_MAX) {
|
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('/');
|
auto path_split_pos = input_path.find_last_of('/');
|
||||||
if (path_split_pos == std::string::npos) {
|
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 defined(SYSTEM_ENV_POSIX)
|
||||||
if (file_name.length() > NAME_MAX) {
|
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) {
|
if (realpath(common::SafeCStr(prefix_path), real_path) == nullptr) {
|
||||||
MS_LOG(ERROR) << "The dir " << prefix_path << " does not exist.";
|
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
|
// input_path is only file_name
|
||||||
#if defined(SYSTEM_ENV_POSIX)
|
#if defined(SYSTEM_ENV_POSIX)
|
||||||
if (input_path.length() > NAME_MAX) {
|
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) {
|
if (realpath(common::SafeCStr(input_path), real_path) == nullptr) {
|
||||||
MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created.";
|
MS_LOG(INFO) << "The file " << input_path << " does not exist, it will be created.";
|
||||||
|
@ -145,8 +148,8 @@ 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) {
|
bool Common::IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message) {
|
||||||
auto len_str = str.length();
|
auto len_str = str.length();
|
||||||
if (len_str > length_limit) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -198,14 +201,16 @@ bool Common::IsPathValid(const std::string &path, size_t length_limit, const std
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!std::all_of(path.begin(), path.end(),
|
if (!std::all_of(path.begin(), path.end(), [](char c) {
|
||||||
[](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) {
|
return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.' || c == '/';
|
||||||
MS_LOG(WARNING) << err_msg << "The path only supports alphabets, digit or {'-', '_', '/'}, but got:" << path << ".";
|
})) {
|
||||||
|
MS_LOG(ERROR) << err_msg << "The path only supports alphabets, digit or {'-', '_', '.', '/'}, but got:" << path
|
||||||
|
<< ".";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0] != '/') {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -229,11 +234,10 @@ bool Common::IsFilenameValid(const std::string &filename, size_t length_limit, c
|
||||||
if (!IsStrLengthValid(filename, length_limit, err_msg)) {
|
if (!IsStrLengthValid(filename, length_limit, err_msg)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
auto func = [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; };
|
||||||
if (!std::all_of(filename.begin(), filename.end(),
|
if (!std::all_of(filename.begin(), filename.end(), func)) {
|
||||||
[](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; })) {
|
MS_LOG(ERROR) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
|
||||||
MS_LOG(WARNING) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
|
<< ".";
|
||||||
<< ".";
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -301,16 +305,19 @@ struct GlogLogDirRegister {
|
||||||
std::string log_dir_str = std::string(log_dir);
|
std::string log_dir_str = std::string(log_dir);
|
||||||
|
|
||||||
auto real_log_dir_str = Common::GetRealPath(log_dir_str);
|
auto real_log_dir_str = Common::GetRealPath(log_dir_str);
|
||||||
// While 'GLOG_logtostderr' = 0, logs output to files.
|
// While 'GLOG_logtostderr' = 0, logs output to files. 'GLOG_log_dir' must be specified as the path of log 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 (logtostderr_str == "0" && real_log_dir_str.has_value()) {
|
||||||
if (!Common::IsPathValid(real_log_dir_str.value(), MAX_DIRECTORY_LENGTH, "")) {
|
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())) {
|
} 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") {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -437,7 +437,9 @@ void common_log_init(void) {
|
||||||
if (logtostderr.empty()) {
|
if (logtostderr.empty()) {
|
||||||
FLAGS_logtostderr = true;
|
FLAGS_logtostderr = true;
|
||||||
} else if (logtostderr == "0" && mindspore::GetEnv("GLOG_log_dir").empty()) {
|
} 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
|
// default GLOG_stderrthreshold level to WARNING
|
||||||
|
|
Loading…
Reference in New Issue