Bug 1663924 - Add ScopedLogExtraInfo. r=dom-workers-and-storage-reviewers,janv

Differential Revision: https://phabricator.services.mozilla.com/D90112
This commit is contained in:
Simon Giesecke 2020-09-17 09:38:12 +00:00
Родитель db717b00e5
Коммит 4f1ba76c89
4 изменённых файлов: 150 добавлений и 5 удалений

Просмотреть файл

@ -7095,6 +7095,9 @@ DatabaseConnection::GetCachedStatement(const nsACString& aQuery) {
template <typename BindFunctor>
nsresult DatabaseConnection::ExecuteCachedStatement(
const nsACString& aQuery, const BindFunctor& aBindFunctor) {
const auto queryInfo =
ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, aQuery};
IDB_TRY_VAR(const auto stmt, GetCachedStatement(aQuery));
nsresult rv = aBindFunctor(*stmt);

Просмотреть файл

@ -157,11 +157,26 @@ nsDependentCSubstring GetLeafName(const nsACString& aPath) {
void LogError(const nsLiteralCString& aModule, const nsACString& aExpr,
const nsACString& aSourceFile, int32_t aSourceLine) {
nsAutoCString extraInfosString;
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
const auto* const extraInfos = ScopedLogExtraInfo::GetExtraInfoMap();
if (extraInfos) {
for (const auto& item : *extraInfos) {
extraInfosString.Append(", "_ns + nsDependentCString(item.first) +
"="_ns + item.second);
}
}
#endif
#ifdef DEBUG
NS_DebugBreak(NS_DEBUG_WARNING, nsAutoCString(aModule + " failure"_ns).get(),
nsPromiseFlatCString(aExpr).get(),
nsPromiseFlatCString(GetLeafName(aSourceFile)).get(),
aSourceLine);
NS_DebugBreak(
NS_DEBUG_WARNING, nsAutoCString(aModule + " failure"_ns).get(),
(extraInfosString.IsEmpty() ? nsPromiseFlatCString(aExpr)
: static_cast<const nsCString&>(nsAutoCString(
aExpr + extraInfosString)))
.get(),
nsPromiseFlatCString(GetLeafName(aSourceFile)).get(), aSourceLine);
#endif
#if defined(EARLY_BETA_OR_EARLIER) || defined(DEBUG)
@ -170,7 +185,8 @@ void LogError(const nsLiteralCString& aModule, const nsACString& aExpr,
if (console) {
NS_ConvertUTF8toUTF16 message(aModule + " failure: '"_ns + aExpr +
"', file "_ns + GetLeafName(aSourceFile) +
", line "_ns + GetIntCString(aSourceLine));
", line "_ns + GetIntCString(aSourceLine) +
extraInfosString);
// The concatenation above results in a message like:
// QuotaManager failure: 'EXP', file XYZ, line N)

Просмотреть файл

@ -861,6 +861,72 @@ Result<bool, nsresult> WarnIfFileIsUnknown(nsIFile& aFile,
int32_t aSourceLine);
#endif
// XXX Since this uses thread_local, we currently disable it on android, see Bug
// 1324316
#if (defined(EARLY_BETA_OR_EARLIER) || defined(DEBUG)) && \
!defined(MOZ_WIDGET_ANDROID)
# define QM_ENABLE_SCOPED_LOG_EXTRA_INFO
#endif
struct MOZ_STACK_CLASS ScopedLogExtraInfo {
static constexpr const char kTagQuery[] = "query";
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
using ScopedLogExtraInfoMap = std::map<const char*, nsCString>;
template <size_t N>
ScopedLogExtraInfo(const char (&aTag)[N], const nsACString& aExtraInfo)
: mTag{aTag} {
AddInfo(aTag, aExtraInfo);
}
~ScopedLogExtraInfo() {
if (mTag) {
if (mPreviousValue.IsEmpty()) {
sInfos.erase(mTag);
} else {
sInfos.find(mTag)->second = mPreviousValue;
}
}
}
ScopedLogExtraInfo(ScopedLogExtraInfo&& aOther)
: mTag(aOther.mTag), mPreviousValue(std::move(aOther.mPreviousValue)) {
aOther.mTag = nullptr;
}
ScopedLogExtraInfo& operator=(ScopedLogExtraInfo&& aOther) = delete;
ScopedLogExtraInfo(const ScopedLogExtraInfo&) = delete;
ScopedLogExtraInfo& operator=(const ScopedLogExtraInfo&) = delete;
static const ScopedLogExtraInfoMap* GetExtraInfoMap() { return &sInfos; }
private:
const char* mTag;
nsCString mPreviousValue;
inline static thread_local ScopedLogExtraInfoMap sInfos;
void AddInfo(const char* aTag, const nsACString& aExtraInfo) {
auto foundIt = sInfos.find(aTag);
if (foundIt != sInfos.end()) {
mPreviousValue = std::move(foundIt->second);
foundIt->second = aExtraInfo;
} else {
sInfos.emplace(aTag, aExtraInfo);
}
}
#else
template <size_t N>
ScopedLogExtraInfo(const char (&aTag)[N], const nsACString& aExtraInfo) {}
// user-defined to silence unused variable warnings
~ScopedLogExtraInfo() {}
#endif
};
#if defined(EARLY_BETA_OR_EARLIER) || defined(DEBUG)
# define QM_META_HANDLE_ERROR(module) \
MOZ_COLD inline void HandleError( \

Просмотреть файл

@ -1153,3 +1153,63 @@ TEST(QuotaCommon_CollectWhileTest, ConditionFailsOnSecondExecution)
MOZ_RELEASE_ASSERT(1 == bodyExecutions);
MOZ_RELEASE_ASSERT(2 == conditionExecutions);
}
TEST(QuotaCommon_ScopedLogExtraInfo, AddAndRemove)
{
static constexpr auto text = "foo"_ns;
{
const auto extraInfo =
ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, text};
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
const auto* const extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap();
EXPECT_NE(nullptr, extraInfoMap);
EXPECT_EQ(text, extraInfoMap->at(ScopedLogExtraInfo::kTagQuery));
#endif
}
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
const auto* const extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap();
EXPECT_NE(nullptr, extraInfoMap);
EXPECT_EQ(0u, extraInfoMap->count(ScopedLogExtraInfo::kTagQuery));
#endif
}
TEST(QuotaCommon_ScopedLogExtraInfo, Nested)
{
static constexpr auto text = "foo"_ns;
static constexpr auto nestedText = "bar"_ns;
{
const auto extraInfo =
ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, text};
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
const auto* const extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap();
EXPECT_NE(nullptr, extraInfoMap);
#endif
{
const auto extraInfo =
ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, nestedText};
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
EXPECT_EQ(nestedText, extraInfoMap->at(ScopedLogExtraInfo::kTagQuery));
#endif
}
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
EXPECT_EQ(text, extraInfoMap->at(ScopedLogExtraInfo::kTagQuery));
#endif
}
#ifdef QM_ENABLE_SCOPED_LOG_EXTRA_INFO
const auto* const extraInfoMap = ScopedLogExtraInfo::GetExtraInfoMap();
EXPECT_NE(nullptr, extraInfoMap);
EXPECT_EQ(0u, extraInfoMap->count(ScopedLogExtraInfo::kTagQuery));
#endif
}