mbs-octoml commented on a change in pull request #9012:
URL: https://github.com/apache/tvm/pull/9012#discussion_r709538852



##########
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;
+  }
+  return map;
+}
+
+constexpr const char* kSrcPrefix = "/src/";
+constexpr const size_t kSrcPrefixLength = 5;
+
+bool VerboseEnabledInMap(const std::string& filename, int level,
+                         const std::unordered_map<std::string, int>& map) {
+  if (level < 0) {
+    return false;
+  }
+  // Canonicalize filename.
+  // TODO(mbs): Not Windows friendly.
+
+  size_t last_src = filename.rfind(kSrcPrefix, std::string::npos, 
kSrcPrefixLength);
+  // Strip anything before the /src/ prefix, on the assumption that will yield 
the
+  // TVM project relative filename. If no such prefix fallback to filename 
without
+  // canonicalization.
+  std::string key =
+      last_src == std::string::npos ? filename : filename.substr(last_src + 
kSrcPrefixLength);
+  // Check for exact.
+  auto itr = map.find(key);
+  if (itr != map.end()) {
+    return level <= itr->second;
+  }
+  // Check for '*' wildcard.
+  itr = map.find("*");
+  if (itr != map.end()) {
+    return level <= itr->second;
+  }
+  return false;
+}
+
+bool VerboseLoggingEnabled(const char* filename, int level) {
+  // Cache the verbosity level map.
+  static const std::unordered_map<std::string, int>* map =
+      new std::unordered_map<std::string, 
int>(ParseTvmLogDebugSpec(std::getenv("TVM_LOG_DEBUG")));

Review comment:
       This is the idiom for statically initialized aggregate structures. Since 
they can never be freed they go in 'raw' pointers.




-- 
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: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to