huajsj commented on a change in pull request #9012:
URL: https://github.com/apache/tvm/pull/9012#discussion_r710458479
##########
File path: src/runtime/logging.cc
##########
@@ -166,10 +167,127 @@ namespace tvm {
namespace runtime {
namespace detail {
+std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char*
opt_spec) {
+ // Cache the verbosity level map.
+ std::unordered_map<std::string, int> map;
+ LOG(INFO) << "initializing VLOG map";
+ if (opt_spec == nullptr) {
+ LOG(INFO) << "VLOG disabled, no TVM_LOG_DEBUG environment variable";
+ return map;
+ }
+ std::string spec(opt_spec);
+ // Check we are enabled overall with at least one VLOG option.
+ if (spec.rfind("1;", 0) != 0) {
Review comment:
spec.find ? is there any reason here use rfind instead of find ?
if (spec == "1" || spec.find("1;") != 0)
##########
File path: src/runtime/logging.cc
##########
@@ -166,10 +167,127 @@ namespace tvm {
namespace runtime {
namespace detail {
+std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char*
opt_spec) {
+ // Cache the verbosity level map.
+ std::unordered_map<std::string, int> map;
+ LOG(INFO) << "initializing VLOG map";
+ if (opt_spec == nullptr) {
+ LOG(INFO) << "VLOG disabled, no TVM_LOG_DEBUG environment variable";
+ return map;
+ }
+ std::string spec(opt_spec);
+ // Check we are enabled overall with at least one VLOG option.
+ if (spec.rfind("1;", 0) != 0) {
+ LOG(INFO) << "VLOG disabled, TVM_LOG_DEBUG does not start with '1;'";
+ return map;
+ }
+ size_t start = 2UL;
Review comment:
the hardcode 2UL not readable, how about use
const char *str_enable = "1;";
//use str_enable for whether debug enable check.
size_t start = strlen(str_enable);
##########
File path: src/runtime/logging.cc
##########
@@ -166,10 +167,127 @@ namespace tvm {
namespace runtime {
namespace detail {
+std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char*
opt_spec) {
+ // Cache the verbosity level map.
+ std::unordered_map<std::string, int> map;
+ LOG(INFO) << "initializing VLOG map";
+ if (opt_spec == nullptr) {
+ LOG(INFO) << "VLOG disabled, no TVM_LOG_DEBUG environment variable";
+ return map;
+ }
+ std::string spec(opt_spec);
+ // Check we are enabled overall with at least one VLOG option.
+ if (spec.rfind("1;", 0) != 0) {
+ LOG(INFO) << "VLOG disabled, TVM_LOG_DEBUG does not start with '1;'";
+ return map;
+ }
+ size_t start = 2UL;
+ while (start < spec.size()) {
+ // We are looking for "name=level;" or "*=level;"
+ size_t end = start;
+ // Scan up to '='.
+ while (spec[end] != '=') {
+ ++end;
+ if (end >= spec.size()) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, missing '='";
+ return map;
+ }
+ }
+ if (end == start) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty name";
+ return map;
+ }
+ std::string name(spec.substr(start, end - start));
+ // Skip '='
+ ++end;
+ if (end >= spec.size()) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, missing level";
+ return map;
+ }
+ // Scan up to ';'.
+ start = end;
+ while (spec[end] != ';') {
+ ++end;
+ if (end >= spec.size()) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, missing ';'";
+ return map;
+ }
+ }
+ if (end == start) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty level";
+ return map;
+ }
+ std::string level_str(spec.substr(start, end - start));
+ // Skip ';'.
+ ++end;
+ // Parse level, default to 0 if ill-formed which we don't detect.
+ char* end_of_level = nullptr;
+ int level = static_cast<int>(strtol(level_str.c_str(), &end_of_level, 10));
+ if (end_of_level != level_str.c_str() + level_str.size()) {
+ LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, invalid level";
+ }
+ LOG(INFO) << "adding VLOG entry for '" << name << "' at level " << level;
+ map.emplace(name, level);
+ start = end;
+ }
Review comment:
recommend to use istringstream, just a example.
```
std::istringstream istr(spec.substr(strlen(str_enable)));
std::string str;
while(getline(istr, str, ';')){
istringstream istr_file(str);
string file, level;
if (!getline(istr_file, file, '=')){
break;
}
if (!getline(istr_file, level, '=')) {
break;
}
map.emplace(file, level);
}
```
##########
File path: include/tvm/runtime/logging.h
##########
@@ -395,10 +399,13 @@ class LogMessageVoidify {
inline bool DebugLoggingEnabled() {
static int state = 0;
if (state == 0) {
- if (auto var = std::getenv("TVM_LOG_DEBUG")) {
- if (std::string(var) == "1") {
+ if (const char* var = std::getenv("TVM_LOG_DEBUG")) {
+ std::string var_str(var);
+ if (var_str == "1" || var_str.rfind("1;", 0) == 0) {
Review comment:
var_str.find ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]