123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #ifndef mozilla_dom_indexeddb_profilerhelpers_h__
- #define mozilla_dom_indexeddb_profilerhelpers_h__
- // This file is not exported and is only meant to be included in IndexedDB
- // source files.
- #include "BackgroundChildImpl.h"
- #include "GeckoProfiler.h"
- #include "IDBCursor.h"
- #include "IDBDatabase.h"
- #include "IDBIndex.h"
- #include "IDBKeyRange.h"
- #include "IDBObjectStore.h"
- #include "IDBTransaction.h"
- #include "IndexedDatabaseManager.h"
- #include "Key.h"
- #include "mozilla/Assertions.h"
- #include "mozilla/Attributes.h"
- #include "mozilla/dom/BindingDeclarations.h"
- #include "nsDebug.h"
- #include "nsID.h"
- #include "nsIDOMEvent.h"
- #include "nsString.h"
- #include "mozilla/Logging.h"
- // Include this last to avoid path problems on Windows.
- #include "ActorsChild.h"
- namespace mozilla {
- namespace dom {
- namespace indexedDB {
- class MOZ_STACK_CLASS LoggingIdString final
- : public nsAutoCString
- {
- public:
- LoggingIdString()
- {
- using mozilla::ipc::BackgroundChildImpl;
- if (IndexedDatabaseManager::GetLoggingMode() !=
- IndexedDatabaseManager::Logging_Disabled) {
- const BackgroundChildImpl::ThreadLocal* threadLocal =
- BackgroundChildImpl::GetThreadLocalForCurrentThread();
- if (threadLocal) {
- const ThreadLocal* idbThreadLocal = threadLocal->mIndexedDBThreadLocal;
- if (idbThreadLocal) {
- Assign(idbThreadLocal->IdString());
- }
- }
- }
- }
- explicit
- LoggingIdString(const nsID& aID)
- {
- static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!");
- static_assert(NSID_LENGTH <= kDefaultStorageSize,
- "nID string won't fit in our storage!");
- MOZ_ASSERT(Capacity() > NSID_LENGTH);
- if (IndexedDatabaseManager::GetLoggingMode() !=
- IndexedDatabaseManager::Logging_Disabled) {
- // NSID_LENGTH counts the null terminator, SetLength() does not.
- SetLength(NSID_LENGTH - 1);
- aID.ToProvidedString(
- *reinterpret_cast<char(*)[NSID_LENGTH]>(BeginWriting()));
- }
- }
- };
- class MOZ_STACK_CLASS LoggingString final
- : public nsAutoCString
- {
- static const char kQuote = '\"';
- static const char kOpenBracket = '[';
- static const char kCloseBracket = ']';
- static const char kOpenParen = '(';
- static const char kCloseParen = ')';
- public:
- explicit
- LoggingString(IDBDatabase* aDatabase)
- : nsAutoCString(kQuote)
- {
- MOZ_ASSERT(aDatabase);
- AppendUTF16toUTF8(aDatabase->Name(), *this);
- Append(kQuote);
- }
- explicit
- LoggingString(IDBTransaction* aTransaction)
- : nsAutoCString(kOpenBracket)
- {
- MOZ_ASSERT(aTransaction);
- NS_NAMED_LITERAL_CSTRING(kCommaSpace, ", ");
- const nsTArray<nsString>& stores = aTransaction->ObjectStoreNamesInternal();
- for (uint32_t count = stores.Length(), index = 0; index < count; index++) {
- Append(kQuote);
- AppendUTF16toUTF8(stores[index], *this);
- Append(kQuote);
- if (index != count - 1) {
- Append(kCommaSpace);
- }
- }
- Append(kCloseBracket);
- Append(kCommaSpace);
- switch (aTransaction->GetMode()) {
- case IDBTransaction::READ_ONLY:
- AppendLiteral("\"readonly\"");
- break;
- case IDBTransaction::READ_WRITE:
- AppendLiteral("\"readwrite\"");
- break;
- case IDBTransaction::READ_WRITE_FLUSH:
- AppendLiteral("\"readwriteflush\"");
- break;
- case IDBTransaction::CLEANUP:
- AppendLiteral("\"cleanup\"");
- break;
- case IDBTransaction::VERSION_CHANGE:
- AppendLiteral("\"versionchange\"");
- break;
- default:
- MOZ_CRASH("Unknown mode!");
- };
- }
- explicit
- LoggingString(IDBObjectStore* aObjectStore)
- : nsAutoCString(kQuote)
- {
- MOZ_ASSERT(aObjectStore);
- AppendUTF16toUTF8(aObjectStore->Name(), *this);
- Append(kQuote);
- }
- explicit
- LoggingString(IDBIndex* aIndex)
- : nsAutoCString(kQuote)
- {
- MOZ_ASSERT(aIndex);
- AppendUTF16toUTF8(aIndex->Name(), *this);
- Append(kQuote);
- }
- explicit
- LoggingString(IDBKeyRange* aKeyRange)
- {
- if (aKeyRange) {
- if (aKeyRange->IsOnly()) {
- Assign(LoggingString(aKeyRange->Lower()));
- } else {
- if (aKeyRange->LowerOpen()) {
- Assign(kOpenParen);
- } else {
- Assign(kOpenBracket);
- }
- Append(LoggingString(aKeyRange->Lower()));
- AppendLiteral(", ");
- Append(LoggingString(aKeyRange->Upper()));
- if (aKeyRange->UpperOpen()) {
- Append(kCloseParen);
- } else {
- Append(kCloseBracket);
- }
- }
- } else {
- AssignLiteral("<undefined>");
- }
- }
- explicit
- LoggingString(const Key& aKey)
- {
- if (aKey.IsUnset()) {
- AssignLiteral("<undefined>");
- } else if (aKey.IsFloat()) {
- AppendPrintf("%g", aKey.ToFloat());
- } else if (aKey.IsDate()) {
- AppendPrintf("<Date %g>", aKey.ToDateMsec());
- } else if (aKey.IsString()) {
- nsAutoString str;
- aKey.ToString(str);
- AppendPrintf("\"%s\"", NS_ConvertUTF16toUTF8(str).get());
- } else if (aKey.IsBinary()) {
- AssignLiteral("[object ArrayBuffer]");
- } else {
- MOZ_ASSERT(aKey.IsArray());
- AssignLiteral("[...]");
- }
- }
- explicit
- LoggingString(const IDBCursor::Direction aDirection)
- {
- switch (aDirection) {
- case IDBCursor::NEXT:
- AssignLiteral("\"next\"");
- break;
- case IDBCursor::NEXT_UNIQUE:
- AssignLiteral("\"nextunique\"");
- break;
- case IDBCursor::PREV:
- AssignLiteral("\"prev\"");
- break;
- case IDBCursor::PREV_UNIQUE:
- AssignLiteral("\"prevunique\"");
- break;
- default:
- MOZ_CRASH("Unknown direction!");
- };
- }
- explicit
- LoggingString(const Optional<uint64_t>& aVersion)
- {
- if (aVersion.WasPassed()) {
- AppendInt(aVersion.Value());
- } else {
- AssignLiteral("<undefined>");
- }
- }
- explicit
- LoggingString(const Optional<uint32_t>& aLimit)
- {
- if (aLimit.WasPassed()) {
- AppendInt(aLimit.Value());
- } else {
- AssignLiteral("<undefined>");
- }
- }
- LoggingString(IDBObjectStore* aObjectStore, const Key& aKey)
- {
- MOZ_ASSERT(aObjectStore);
- if (!aObjectStore->HasValidKeyPath()) {
- Append(LoggingString(aKey));
- }
- }
- LoggingString(nsIDOMEvent* aEvent, const char16_t* aDefault)
- : nsAutoCString(kQuote)
- {
- MOZ_ASSERT(aDefault);
- nsString eventType;
- if (aEvent) {
- MOZ_ALWAYS_SUCCEEDS(aEvent->GetType(eventType));
- } else {
- eventType = nsDependentString(aDefault);
- }
- AppendUTF16toUTF8(eventType, *this);
- Append(kQuote);
- }
- };
- inline void
- LoggingHelper(bool aUseProfiler, const char* aFmt, ...)
- {
- MOZ_ASSERT(IndexedDatabaseManager::GetLoggingMode() !=
- IndexedDatabaseManager::Logging_Disabled);
- MOZ_ASSERT(aFmt);
- mozilla::LogModule* logModule = IndexedDatabaseManager::GetLoggingModule();
- MOZ_ASSERT(logModule);
- static const mozilla::LogLevel logLevel = LogLevel::Warning;
- if (MOZ_LOG_TEST(logModule, logLevel) ||
- (aUseProfiler && profiler_is_active())) {
- nsAutoCString message;
- {
- va_list args;
- va_start(args, aFmt);
- message.AppendPrintf(aFmt, args);
- va_end(args);
- }
- MOZ_LOG(logModule, logLevel, ("%s", message.get()));
- if (aUseProfiler) {
- PROFILER_MARKER(message.get());
- }
- }
- }
- } // namespace indexedDB
- } // namespace dom
- } // namespace mozilla
- #define IDB_LOG_MARK(_detailedFmt, _conciseFmt, ...) \
- do { \
- using namespace mozilla::dom::indexedDB; \
- \
- const IndexedDatabaseManager::LoggingMode mode = \
- IndexedDatabaseManager::GetLoggingMode(); \
- \
- if (mode != IndexedDatabaseManager::Logging_Disabled) { \
- const char* _fmt; \
- if (mode == IndexedDatabaseManager::Logging_Concise || \
- mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks) { \
- _fmt = _conciseFmt; \
- } else { \
- MOZ_ASSERT( \
- mode == IndexedDatabaseManager::Logging_Detailed || \
- mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks); \
- _fmt = _detailedFmt; \
- } \
- \
- const bool _useProfiler = \
- mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks || \
- mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks; \
- \
- LoggingHelper(_useProfiler, _fmt, ##__VA_ARGS__); \
- } \
- } while (0)
- #define IDB_LOG_ID_STRING(...) \
- mozilla::dom::indexedDB::LoggingIdString(__VA_ARGS__).get()
- #define IDB_LOG_STRINGIFY(...) \
- mozilla::dom::indexedDB::LoggingString(__VA_ARGS__).get()
- #endif // mozilla_dom_indexeddb_profilerhelpers_h__
|