mbs-octoml commented on a change in pull request #9012: URL: https://github.com/apache/tvm/pull/9012#discussion_r711610622
########## File path: src/runtime/logging.cc ########## @@ -166,10 +167,118 @@ namespace tvm { namespace runtime { namespace detail { +namespace { +constexpr const char* kSrcPrefix = "/src/"; +constexpr const size_t kSrcPrefixLength = 5; +constexpr const char* kDefaultKeyword = "DEFAULT"; +} // namespace + +// Parse \p opt_spec as a VLOG specification as per comment in +// DebugLoggingEnabled and VerboseLoggingEnabled. +std::unordered_map<std::string, int> ParseTvmLogDebugSpec(const char* opt_spec) { + std::unordered_map<std::string, int> map; + if (opt_spec == nullptr) { + // DLOG and VLOG disabled. + return map; + } + std::string spec(opt_spec); + if (spec.empty() || spec == "0") { + // DLOG and VLOG disabled. + return map; + } + if (spec == "1") { + // Legacy specification for enabling just DLOG. + // A wildcard entry in the map will signal DLOG is on, but all VLOG levels are disabled. + LOG(INFO) << "TVM_LOG_DEBUG enables DLOG statements only"; + map.emplace(kDefaultKeyword, -1); + return map; + } + std::istringstream spec_stream(spec); + while (spec_stream) { + std::string name; + if (!std::getline(spec_stream, name, '=')) { + // Reached end. + break; + } + if (name.empty()) { + LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty name"; + return map; + } + + std::string level; + if (!std::getline(spec_stream, level, ';')) { + LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, expecting level"; + return map; + } + if (level.empty()) { + LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty level"; + return map; + } + // Parse level, default to 0 if ill-formed which we don't detect. + char* end_of_level = nullptr; + int level_val = static_cast<int>(strtol(level.c_str(), &end_of_level, 10)); + if (end_of_level != level.c_str() + level.size()) { + LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, invalid level"; + return map; + } + if (map.empty()) { + LOG(INFO) << "TVM_LOG_DEBUG enables DLOG statements"; + } + LOG(INFO) << "TVM_LOG_DEBUG enables VLOG statements in '" << name << "' up to level " << level; + map.emplace(name, level_val); + } + return map; +} + +const std::unordered_map<std::string, int>& TvmLogDebugSpec() { + // Parse and cache the verbosity level map. + static const auto* map = + new std::unordered_map<std::string, int>(ParseTvmLogDebugSpec(std::getenv("TVM_LOG_DEBUG"))); + return *map; +} + +bool VerboseEnabledInMap(const std::string& filename, int level, + const std::unordered_map<std::string, int>& map) { + if (level < 0) { Review comment: Added to VerboseLoggingEnabled comment. -- 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