From 3482fc22779d1a8520212c8031ba34eecf44c161 Mon Sep 17 00:00:00 2001 From: Mark Shields Date: Fri, 17 Sep 2021 16:06:08 -0700 Subject: [PATCH] [checkpoint] * -> DEFAULT for wildcard. Andrew pointed out *=9 suggests foo/*.cc=9 would work but it is not supported. --- include/tvm/runtime/logging.h | 6 ++--- src/runtime/logging.cc | 37 +++++++++++++++++-------------- tests/cpp/runtime/logging_test.cc | 18 +++++++++------ 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/include/tvm/runtime/logging.h b/include/tvm/runtime/logging.h index 8f25e290e6a48..0bdf816e74ab3 100644 --- a/include/tvm/runtime/logging.h +++ b/include/tvm/runtime/logging.h @@ -441,12 +441,12 @@ inline bool DebugLoggingEnabled() { * * To enable file \p relay/foo.cc up to level 2 and \p ir/bar.cc for level 0 only set: * \code - * TVM_LOG_DEBUG="relay/foo.cc=2;ir/bar.cc=0;" + * TVM_LOG_DEBUG="relay/foo.cc=2;ir/bar.cc=0" * \endcode * * To enable all files up to level 3 but disable \p ir/bar.cc set: * \code - * TVM_LOG_DEBUG="*=2;ir/bar.cc=-1;" + * TVM_LOG_DEBUG="DEFAULT=2;ir/bar.cc=-1" * \endcode * * Any of these settings will also enable DLOG statements. @@ -574,7 +574,7 @@ TVM_CHECK_FUNC(_NE, !=) /*! * \brief If the \p TVM_LOG_DEBUG build flag is enabled, push a context message onto an internal - * stack. All VLOG messages will include this stack as their prefix to help with debugging. E.g.: + * stack. All VLOG messages will include this stack in their prefix to help with debugging. E.g.: * \code * VLOG_CONTEXT << "my context"; * VLOG(1) << "my log message"; diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc index 4d70dbd47b611..363bed3871de9 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -167,6 +167,12 @@ 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 ParseTvmLogDebugSpec(const char* opt_spec) { @@ -184,27 +190,26 @@ std::unordered_map ParseTvmLogDebugSpec(const char* opt_spec) // 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("*", -1); + map.emplace(kDefaultKeyword, -1); return map; } std::istringstream spec_stream(spec); - for (std::string entry; std::getline(spec_stream, entry, ';'); /* no-op */) { - if (entry.empty()) { - LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, empty entry"; - return map; - } - std::istringstream entry_stream(entry); + while (spec_stream) { std::string name; - if (!std::getline(entry_stream, name, '=')) { - LOG(FATAL) << "TVM_LOG_DEBUG ill-formed, missing '='"; - return map; + 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; - entry_stream >> 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; @@ -214,6 +219,7 @@ std::unordered_map ParseTvmLogDebugSpec(const char* opt_spec) int level_val = static_cast(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"; @@ -231,9 +237,6 @@ const std::unordered_map& TvmLogDebugSpec() { 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& map) { if (level < 0) { @@ -248,13 +251,13 @@ bool VerboseEnabledInMap(const std::string& filename, int level, // canonicalization. std::string key = last_src == std::string::npos ? filename : filename.substr(last_src + kSrcPrefixLength); - // Check for exact. + // Check for exact match. auto itr = map.find(key); if (itr != map.end()) { return level <= itr->second; } - // Check for '*' wildcard. - itr = map.find("*"); + // Check for default. + itr = map.find(kDefaultKeyword); if (itr != map.end()) { return level <= itr->second; } diff --git a/tests/cpp/runtime/logging_test.cc b/tests/cpp/runtime/logging_test.cc index f232eddd842f2..ad110cd445e8d 100644 --- a/tests/cpp/runtime/logging_test.cc +++ b/tests/cpp/runtime/logging_test.cc @@ -34,14 +34,19 @@ TEST(ParseTvmLogDebugSpec, Disabled) { TEST(ParseTvmLogDebugSpec, DlogOnly) { auto map = ParseTvmLogDebugSpec("1"); EXPECT_EQ(map.size(), 1); - EXPECT_EQ(map["*"], -1); + EXPECT_EQ(map["DEFAULT"], -1); } -TEST(ParseTvmLogDebugSpec, VLogEnabled) { - auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;*=2;another/file.cc=4"); - EXPECT_EQ(map.size(), 4); +TEST(ParseTvmLogDebugSpec, VLogEnabledDefault) { + auto map = ParseTvmLogDebugSpec("DEFAULT=3"); + EXPECT_EQ(map.size(), 1); + EXPECT_EQ(map["DEFAULT"], 3); +} - EXPECT_EQ(map["*"], 2); +TEST(ParseTvmLogDebugSpec, VLogEnabledComplex) { + auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;DEFAULT=2;another/file.cc=4"); + EXPECT_EQ(map.size(), 4); + EXPECT_EQ(map["DEFAULT"], 2); EXPECT_EQ(map["foo/bar.cc"], 3); EXPECT_EQ(map["baz.cc"], -1); EXPECT_EQ(map["another/file.cc"], 4); @@ -52,8 +57,7 @@ TEST(ParseTvmLogDebugSpec, IllFormed) { } TEST(VerboseEnabledInMap, Lookup) { - auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;*=2;another/file.cc=4;"); - + auto map = ParseTvmLogDebugSpec("foo/bar.cc=3;baz.cc=-1;DEFAULT=2;another/file.cc=4;"); EXPECT_TRUE(VerboseEnabledInMap("my/filesystem/src/foo/bar.cc", 3, map)); EXPECT_FALSE(VerboseEnabledInMap("my/filesystem/src/foo/bar.cc", 4, map)); EXPECT_TRUE(VerboseEnabledInMap("my/filesystem/src/foo/other.cc", 2, map));