From 25ec5c8219d97daff7bba896d488b9e1dd40bda2 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Mon, 8 Sep 2014 16:30:11 -0700 Subject: [PATCH] Bug 1062077 - Hoist console logging duties into xpc::ErrorReport. r=bz --- dom/base/nsJSEnvironment.cpp | 93 ++------------------------------ dom/base/nsJSEnvironment.h | 5 +- js/xpconnect/src/nsXPConnect.cpp | 56 +++++++++++++++++++ js/xpconnect/src/xpcpublic.h | 2 + 4 files changed, 63 insertions(+), 93 deletions(-) diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index 794e9369b50..b9dbb4cdcaa 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -97,10 +97,6 @@ using namespace mozilla::dom; const size_t gStackSize = 8192; -#ifdef PR_LOGGING -static PRLogModuleInfo* gJSDiagnostics; -#endif - // Thank you Microsoft! #ifdef CompareString #undef CompareString @@ -352,45 +348,7 @@ NS_HandleScriptError(nsIScriptGlobalObject *aScriptGlobal, return called; } -namespace mozilla { -namespace dom { - -void -AsyncErrorReporter::ReportError() -{ - nsCOMPtr errorObject = - do_CreateInstance("@mozilla.org/scripterror;1"); - if (!errorObject) { - return; - } - - uint64_t windowID = mReport->mWindow ? mReport->mWindow->WindowID() : 0; - nsresult rv = errorObject->InitWithWindowID(mReport->mErrorMsg, - mReport->mFileName, - mReport->mSourceLine, - mReport->mLineNumber, - mReport->mColumn, - mReport->mFlags, - mReport->Category(), - windowID); - if (NS_FAILED(rv)) { - return; - } - - nsCOMPtr consoleService = - do_GetService(NS_CONSOLESERVICE_CONTRACTID); - if (!consoleService) { - return; - } - - consoleService->LogMessage(errorObject); - return; -} - -} // namespace dom -} // namespace mozilla - -class ScriptErrorEvent : public AsyncErrorReporter +class ScriptErrorEvent : public nsRunnable { public: ScriptErrorEvent(JSRuntime* aRuntime, @@ -398,8 +356,7 @@ public: nsIPrincipal* aScriptOriginPrincipal, JS::Handle aError, bool aDispatchEvent) - // Pass an empty category, then compute ours - : AsyncErrorReporter(aRuntime, aReport) + : mReport(aReport) , mOriginPrincipal(aScriptOriginPrincipal) , mDispatchEvent(aDispatchEvent) , mError(aRuntime, aError) @@ -464,13 +421,14 @@ public: } if (status != nsEventStatus_eConsumeNoDefault) { - AsyncErrorReporter::ReportError(); + mReport->LogToConsole(); } return NS_OK; } private: + nsRefPtr mReport; nsCOMPtr mOriginPrincipal; bool mDispatchEvent; JS::PersistentRootedValue mError; @@ -521,49 +479,6 @@ NS_ScriptErrorReporter(JSContext *cx, */ report->errorNumber != JSMSG_OUT_OF_MEMORY)); } - - if (nsContentUtils::DOMWindowDumpEnabled()) { - // Print it to stderr as well, for the benefit of those invoking - // mozilla with -console. - nsAutoCString error; - error.AssignLiteral("JavaScript "); - if (JSREPORT_IS_STRICT(report->flags)) - error.AppendLiteral("strict "); - if (JSREPORT_IS_WARNING(report->flags)) - error.AppendLiteral("warning: "); - else - error.AppendLiteral("error: "); - error.Append(report->filename); - error.AppendLiteral(", line "); - error.AppendInt(report->lineno, 10); - error.AppendLiteral(": "); - if (report->ucmessage) { - AppendUTF16toUTF8(reinterpret_cast(report->ucmessage), - error); - } else { - error.Append(message); - } - - fprintf(stderr, "%s\n", error.get()); - fflush(stderr); - } - -#ifdef PR_LOGGING - if (!gJSDiagnostics) - gJSDiagnostics = PR_NewLogModule("JSDiagnostics"); - - if (gJSDiagnostics) { - PR_LOG(gJSDiagnostics, - JSREPORT_IS_WARNING(report->flags) ? PR_LOG_WARNING : PR_LOG_ERROR, - ("file %s, line %u: %s\n%s%s", - report->filename, report->lineno, message, - report->linebuf ? report->linebuf : "", - (report->linebuf && - report->linebuf[strlen(report->linebuf)-1] != '\n') - ? "\n" - : "")); - } -#endif } #ifdef DEBUG diff --git a/dom/base/nsJSEnvironment.h b/dom/base/nsJSEnvironment.h index 5842f3e7684..08c166cee78 100644 --- a/dom/base/nsJSEnvironment.h +++ b/dom/base/nsJSEnvironment.h @@ -197,14 +197,11 @@ public: NS_IMETHOD Run() { - ReportError(); + mReport->LogToConsole(); return NS_OK; } protected: - // Do the actual error reporting - void ReportError(); - nsRefPtr mReport; }; diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index 76fa330456d..52c5bec596b 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -297,6 +297,62 @@ xpc::ErrorReport::InitInternal(JSErrorReport *aReport, mFlags = aReport->flags; } +#ifdef PR_LOGGING +static PRLogModuleInfo* gJSDiagnostics; +#endif + +void +xpc::ErrorReport::LogToConsole() +{ + // Log to stdout. + if (nsContentUtils::DOMWindowDumpEnabled()) { + nsAutoCString error; + error.AssignLiteral("JavaScript "); + if (JSREPORT_IS_STRICT(mFlags)) + error.AppendLiteral("strict "); + if (JSREPORT_IS_WARNING(mFlags)) + error.AppendLiteral("warning: "); + else + error.AppendLiteral("error: "); + error.Append(NS_LossyConvertUTF16toASCII(mFileName)); + error.AppendLiteral(", line "); + error.AppendInt(mLineNumber, 10); + error.AppendLiteral(": "); + error.Append(NS_LossyConvertUTF16toASCII(mErrorMsg)); + + fprintf(stderr, "%s\n", error.get()); + fflush(stderr); + } + +#ifdef PR_LOGGING + // Log to the PR Log Module. + if (!gJSDiagnostics) + gJSDiagnostics = PR_NewLogModule("JSDiagnostics"); + if (gJSDiagnostics) { + PR_LOG(gJSDiagnostics, + JSREPORT_IS_WARNING(mFlags) ? PR_LOG_WARNING : PR_LOG_ERROR, + ("file %s, line %u\n%s", NS_LossyConvertUTF16toASCII(mFileName).get(), + mLineNumber, NS_LossyConvertUTF16toASCII(mErrorMsg).get())); + } +#endif + + // Log to the console. We do this last so that we can simply return if + // there's no console service without affecting the other reporting + // mechanisms. + nsCOMPtr consoleService = + do_GetService(NS_CONSOLESERVICE_CONTRACTID); + nsCOMPtr errorObject = + do_CreateInstance("@mozilla.org/scripterror;1"); + NS_ENSURE_TRUE_VOID(consoleService && errorObject); + + nsresult rv = errorObject->InitWithWindowID(mErrorMsg, mFileName, mSourceLine, + mLineNumber, mColumn, mFlags, Category(), + mWindow ? mWindow->WindowID() : 0); + NS_ENSURE_SUCCESS_VOID(rv); + consoleService->LogMessage(errorObject); + +} + /***************************************************************************/ diff --git a/js/xpconnect/src/xpcpublic.h b/js/xpconnect/src/xpcpublic.h index ebdbeae6c2f..b6d2f550be5 100644 --- a/js/xpconnect/src/xpcpublic.h +++ b/js/xpconnect/src/xpcpublic.h @@ -506,6 +506,8 @@ class ErrorReport { void InitOnWorkerThread(JSErrorReport *aReport, const char *aFallbackMessage, bool aIsChrome); + void LogToConsole(); + private: void InitInternal(JSErrorReport *aReport, const char *aFallbackMessage); bool mIsChrome;