Restores logging of console calls to the STDOUT
authortimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 May 2008 18:16:46 +0000 (18:16 +0000)
committertimothy@apple.com <timothy@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 21 May 2008 18:16:46 +0000 (18:16 +0000)
when Interpreter::shouldPrintExceptions() is true.

<rdar://problem/5636442> REGRESSION: Console.log no longer logs to
the system console or terminal

<rdar://problem/5146079> JavaScript exception logging should print
accurate file and line info when called from WebScriptObject

Reviewed by Adam Roben.

* bindings/js/JSCustomSQLStatementCallback.cpp:
(WebCore::JSCustomSQLStatementCallback::handleEvent): Remove existing
printf and check for Interpreter::shouldPrintExceptions() since there is
a call to Console::addMessage.
* bindings/js/JSCustomSQLStatementErrorCallback.cpp:
(WebCore::JSCustomSQLStatementErrorCallback::handleEvent): Ditto.
* bindings/js/JSCustomSQLTransactionCallback.cpp:
(WebCore::JSCustomSQLTransactionCallback::handleEvent): Ditto.
* bindings/js/JSCustomSQLTransactionErrorCallback.cpp:
(WebCore::JSCustomSQLTransactionErrorCallback::handleEvent): Ditto.
* bindings/js/JSCustomVoidCallback.cpp:
(WebCore::JSCustomVoidCallback::handleEvent): Ditto.
* bindings/js/JSCustomXPathNSResolver.cpp:
(WebCore::JSCustomXPathNSResolver::lookupNamespaceURI): Ditto.
* bindings/js/JSDOMWindowBase.cpp:
(WebCore::JSDOMWindowBase::printErrorMessage): Ditto.
* bindings/js/ScheduledAction.cpp:
(WebCore::ScheduledAction::execute): Ditto.
* bindings/js/kjs_events.cpp:
(WebCore::JSAbstractEventListener::handleEvent): Ditto.
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::shouldAllowNavigation): Ditto.
* bindings/objc/WebScriptObject.mm:
(WebCore::addExceptionToConsole): Added helper static function that
calls Console::addMessage.
(-[WebScriptObject callWebScriptMethod:withArguments:]): Call addExceptionToConsole.
(-[WebScriptObject evaluateWebScript:]): Ditto.
(-[WebScriptObject setValue:forKey:]): Ditto.
(-[WebScriptObject valueForKey:]): Ditto.
(-[WebScriptObject removeWebScriptKey:]): Ditto.
(-[WebScriptObject webScriptValueAtIndex:]): Ditto.
(-[WebScriptObject setWebScriptValueAtIndex:value:]): Ditto.
* page/Console.cpp:
(WebCore::Console::addMessage): Print the level, message,
URL and line number to STDOUT.
(WebCore::printToStandardOut): Helper static function that
takes a prefix string, ExecState, an argument List and URL.
Prints the prefix and loops through the arguments calling
toString on each and printing the string. Finally printing
the URL and new line.
(WebCore::Console::error): Call printToStandardOut.
(WebCore::Console::info): Ditto.
(WebCore::Console::log): Ditto.
(WebCore::Console::assertCondition): Ditto.
(WebCore::Console::warn): Ditto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@33970 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
WebCore/ChangeLog
WebCore/bindings/js/JSCustomSQLStatementCallback.cpp
WebCore/bindings/js/JSCustomSQLStatementErrorCallback.cpp
WebCore/bindings/js/JSCustomSQLTransactionCallback.cpp
WebCore/bindings/js/JSCustomSQLTransactionErrorCallback.cpp
WebCore/bindings/js/JSCustomVoidCallback.cpp
WebCore/bindings/js/JSCustomXPathNSResolver.cpp
WebCore/bindings/js/JSDOMWindowBase.cpp
WebCore/bindings/js/ScheduledAction.cpp
WebCore/bindings/js/kjs_events.cpp
WebCore/bindings/objc/WebScriptObject.mm
WebCore/loader/FrameLoader.cpp
WebCore/page/Console.cpp

index fa51d99..0538e79 100644 (file)
@@ -1,3 +1,62 @@
+2008-05-21  Timothy Hatcher  <timothy@apple.com>
+
+        Restores logging of console calls to the STDOUT
+        when Interpreter::shouldPrintExceptions() is true.
+
+        <rdar://problem/5636442> REGRESSION: Console.log no longer logs to
+        the system console or terminal
+
+        <rdar://problem/5146079> JavaScript exception logging should print
+        accurate file and line info when called from WebScriptObject
+
+        Reviewed by Adam Roben.
+
+        * bindings/js/JSCustomSQLStatementCallback.cpp:
+        (WebCore::JSCustomSQLStatementCallback::handleEvent): Remove existing
+        printf and check for Interpreter::shouldPrintExceptions() since there is
+        a call to Console::addMessage.
+        * bindings/js/JSCustomSQLStatementErrorCallback.cpp:
+        (WebCore::JSCustomSQLStatementErrorCallback::handleEvent): Ditto.
+        * bindings/js/JSCustomSQLTransactionCallback.cpp:
+        (WebCore::JSCustomSQLTransactionCallback::handleEvent): Ditto.
+        * bindings/js/JSCustomSQLTransactionErrorCallback.cpp:
+        (WebCore::JSCustomSQLTransactionErrorCallback::handleEvent): Ditto.
+        * bindings/js/JSCustomVoidCallback.cpp:
+        (WebCore::JSCustomVoidCallback::handleEvent): Ditto.
+        * bindings/js/JSCustomXPathNSResolver.cpp:
+        (WebCore::JSCustomXPathNSResolver::lookupNamespaceURI): Ditto.
+        * bindings/js/JSDOMWindowBase.cpp:
+        (WebCore::JSDOMWindowBase::printErrorMessage): Ditto.
+        * bindings/js/ScheduledAction.cpp:
+        (WebCore::ScheduledAction::execute): Ditto.
+        * bindings/js/kjs_events.cpp:
+        (WebCore::JSAbstractEventListener::handleEvent): Ditto.
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::shouldAllowNavigation): Ditto.
+        * bindings/objc/WebScriptObject.mm:
+        (WebCore::addExceptionToConsole): Added helper static function that
+        calls Console::addMessage.
+        (-[WebScriptObject callWebScriptMethod:withArguments:]): Call addExceptionToConsole.
+        (-[WebScriptObject evaluateWebScript:]): Ditto.
+        (-[WebScriptObject setValue:forKey:]): Ditto.
+        (-[WebScriptObject valueForKey:]): Ditto.
+        (-[WebScriptObject removeWebScriptKey:]): Ditto.
+        (-[WebScriptObject webScriptValueAtIndex:]): Ditto.
+        (-[WebScriptObject setWebScriptValueAtIndex:value:]): Ditto.
+        * page/Console.cpp:
+        (WebCore::Console::addMessage): Print the level, message,
+        URL and line number to STDOUT.
+        (WebCore::printToStandardOut): Helper static function that
+        takes a prefix string, ExecState, an argument List and URL.
+        Prints the prefix and loops through the arguments calling
+        toString on each and printing the string. Finally printing
+        the URL and new line.
+        (WebCore::Console::error): Call printToStandardOut.
+        (WebCore::Console::info): Ditto.
+        (WebCore::Console::log): Ditto.
+        (WebCore::Console::assertCondition): Ditto.
+        (WebCore::Console::warn): Ditto.
+
 2008-05-20  Timothy Hatcher  <timothy@apple.com>
 
         Changes to work with the new Profiler API. The Profile is now
index ca508f1..719ba13 100644 (file)
@@ -91,8 +91,6 @@ void JSCustomSQLStatementCallback::handleEvent(SQLTransaction* transaction, SQLR
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("SQLStatementCallback: %s\n", message.utf8().data());
         m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         
         raisedException = true;
index 39ea8a1..883fb39 100644 (file)
@@ -92,8 +92,6 @@ bool JSCustomSQLStatementErrorCallback::handleEvent(SQLTransaction* transaction,
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("SQLStatementErrorCallback: %s\n", message.utf8().data());
         m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         exec->clearException();
             
index 02b8960..bd1b9ca 100644 (file)
@@ -144,8 +144,6 @@ void JSCustomSQLTransactionCallback::handleEvent(SQLTransaction* transaction, bo
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("SQLTransactionCallback: %s\n", message.utf8().data());
         m_data->frame()->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         exec->clearException();
         
index 3c82ba2..b34cfd8 100644 (file)
@@ -90,8 +90,6 @@ bool JSCustomSQLTransactionErrorCallback::handleEvent(SQLError* error)
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("SQLTransactionErrorCallback: %s\n", message.utf8().data());
         m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         exec->clearException();
     }
index f8aa40d..868e062 100644 (file)
@@ -89,8 +89,6 @@ void JSCustomVoidCallback::handleEvent()
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("VoidCallback: %s\n", message.utf8().data());
         m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         exec->clearException();            
     }
index d1d604b..81a47eb 100644 (file)
@@ -113,8 +113,6 @@ String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
         String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
         int lineNumber = exception->get(exec, "line")->toInt32(exec);
         String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-        if (Interpreter::shouldPrintExceptions())
-            printf("XPathNSResolver: %s\n", message.utf8().data());
         m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
         exec->clearException();
     } else {
index 2f4f612..1c14eea 100644 (file)
@@ -804,9 +804,6 @@ void JSDOMWindowBase::printErrorMessage(const String& message) const
     if (frame->settings()->privateBrowsingEnabled())
         return;
 
-    if (Interpreter::shouldPrintExceptions())
-        printf("%s", message.utf8().data());
-
     impl()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, 1, String()); // FIXME: provide a real line number and source URL.
 }
 
index 7c23fbc..630e22b 100644 (file)
@@ -78,8 +78,6 @@ void ScheduledAction::execute(JSDOMWindowShell* windowShell)
                 exec->clearException();
                 String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
                 int lineNumber = exception->get(exec, "line")->toInt32(exec);
-                if (Interpreter::shouldPrintExceptions())
-                    printf("(timer):%s\n", message.utf8().data());
                 frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, String());
             }
         }
index a5680aa..f79fcbc 100644 (file)
@@ -108,8 +108,6 @@ void JSAbstractEventListener::handleEvent(Event* ele, bool isWindowEvent)
             String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
             int lineNumber = exception->get(exec, "line")->toInt32(exec);
             String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
-            if (Interpreter::shouldPrintExceptions())
-                printf("(event handler):%s\n", message.utf8().data());
             frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
             exec->clearException();
         } else {
index 2a22e5e..657dad1 100644 (file)
 #import "config.h"
 #import "WebScriptObjectPrivate.h"
 
+#import "Console.h"
 #import "DOMInternal.h"
+#import "DOMWindow.h"
 #import "Frame.h"
+#import "JSDOMWindow.h"
 #import "PlatformString.h"
 #import "WebCoreObjCExtras.h"
 #import <JavaScriptCore/ExecState.h>
@@ -47,10 +50,6 @@ using namespace KJS;
 using namespace KJS::Bindings;
 using namespace WebCore;
 
-#define LOG_EXCEPTION(exec) \
-    if (Interpreter::shouldPrintExceptions()) \
-        printf("%s:%d:[%d]  JavaScript exception:  %s\n", __FILE__, __LINE__, getpid(), exec->exception()->toObject(exec)->get(exec, exec->propertyNames().message)->toString(exec).ascii());
-
 namespace WebCore {
 
 typedef HashMap<JSObject*, NSObject*> JSWrapperMap;
@@ -84,6 +83,18 @@ id createJSWrapper(KJS::JSObject* object, PassRefPtr<KJS::Bindings::RootObject>
     return [[[WebScriptObject alloc] _initWithJSObject:object originRootObject:origin rootObject:root] autorelease];
 }
 
+static void addExceptionToConsole(ExecState* exec)
+{
+    JSDOMWindow* window = asJSDOMWindow(exec->dynamicGlobalObject());
+    JSObject* exception = exec->exception()->toObject(exec);
+    if (!window || !exception)
+        return;
+    String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
+    int lineNumber = exception->get(exec, "line")->toInt32(exec);
+    String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
+    window->impl()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
+}
+
 } // namespace WebCore
 
 @implementation WebScriptObjectPrivate
@@ -306,7 +317,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
     [self _rootObject]->globalObject()->stopTimeoutCheck();
 
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         result = jsUndefined();
         exec->clearException();
     }
@@ -343,7 +354,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
         result = jsUndefined();
     
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         result = jsUndefined();
         exec->clearException();
     }
@@ -367,7 +378,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
     [self _imp]->put(exec, String(key), convertObjcValueToValue(exec, &value, ObjcObjectType, [self _rootObject]));
 
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         exec->clearException();
     }
 
@@ -392,7 +403,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
         JSValue *result = [self _imp]->get(exec, String(key));
         
         if (exec->hadException()) {
-            LOG_EXCEPTION(exec);
+            addExceptionToConsole(exec);
             result = jsUndefined();
             exec->clearException();
         }
@@ -421,7 +432,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
     [self _imp]->deleteProperty(exec, String(key));
 
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         exec->clearException();
     }
 
@@ -458,7 +469,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
     JSValue *result = [self _imp]->get(exec, index);
 
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         result = jsUndefined();
         exec->clearException();
     }
@@ -482,7 +493,7 @@ static void getListFromNSArray(ExecState *exec, NSArray *array, RootObject* root
     [self _imp]->put(exec, index, convertObjcValueToValue(exec, &value, ObjcObjectType, [self _rootObject]));
 
     if (exec->hadException()) {
-        LOG_EXCEPTION(exec);
+        addExceptionToConsole(exec);
         exec->clearException();
     }
 
index 55c5f9a..b502cf9 100644 (file)
@@ -2473,9 +2473,6 @@ bool FrameLoader::shouldAllowNavigation(Frame* targetFrame) const
         String message = String::format("Unsafe JavaScript attempt to initiate a navigation change for frame with URL %s from frame with URL %s.\n",
             targetDocument->url().string().utf8().data(), activeDocument->url().string().utf8().data());
 
-        if (KJS::Interpreter::shouldPrintExceptions())
-            printf("%s", message.utf8().data());
-
         // FIXME: should we print to the console of the activeFrame as well?
         targetFrame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, 1, String());
     }
index 208d509..33e92b1 100644 (file)
@@ -30,6 +30,7 @@
 #include "Console.h"
 
 #include "ChromeClient.h"
+#include "CString.h"
 #include "Frame.h"
 #include "FrameLoader.h"
 #include "FrameTree.h"
@@ -37,6 +38,7 @@
 #include "Page.h"
 #include "PageGroup.h"
 #include "PlatformString.h"
+#include <kjs/interpreter.h>
 #include <kjs/list.h>
 #include <profiler/Profiler.h>
 
@@ -54,6 +56,89 @@ void Console::disconnectFrame()
     m_frame = 0;
 }
 
+static void printSourceURLAndLine(const String& sourceURL, unsigned lineNumber)
+{
+    if (!sourceURL.isEmpty()) {
+        if (lineNumber > 0)
+            printf("%s:%d: ", sourceURL.utf8().data(), lineNumber);
+        else
+            printf("%s: ", sourceURL.utf8().data());
+    }
+}
+
+static void printMessageSourceAndLevelPrefix(MessageSource source, MessageLevel level)
+{
+    const char* sourceString;
+    switch (source) {
+        case HTMLMessageSource:
+            sourceString = "HTML";
+            break;
+        case XMLMessageSource:
+            sourceString = "XML";
+            break;
+        case JSMessageSource:
+            sourceString = "JS";
+            break;
+        case CSSMessageSource:
+            sourceString = "CSS";
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+            // Fall thru.
+        case OtherMessageSource:
+            sourceString = "OTHER";
+            break;
+    }
+
+    const char* levelString;
+    switch (level) {
+        case TipMessageLevel:
+            levelString = "TIP";
+            break;
+        default:
+            ASSERT_NOT_REACHED();
+            // Fall thru.
+        case LogMessageLevel:
+            levelString = "LOG";
+            break;
+        case WarningMessageLevel:
+            levelString = "WARN";
+            break;
+        case ErrorMessageLevel:
+            levelString = "ERROR";
+            break;
+    }
+
+    printf("%s %s:", sourceString, levelString);
+}
+
+static void printToStandardOut(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber)
+{
+    if (!Interpreter::shouldPrintExceptions())
+        return;
+
+    printSourceURLAndLine(sourceURL, lineNumber);
+    printMessageSourceAndLevelPrefix(source, level);
+
+    printf(" %s\n", message.utf8().data());
+}
+
+static void printToStandardOut(MessageLevel level, ExecState* exec, const List& arguments, const KURL& url)
+{
+    if (!Interpreter::shouldPrintExceptions())
+        return;
+
+    printSourceURLAndLine(url.prettyURL(), 0);
+    printMessageSourceAndLevelPrefix(JSMessageSource, level);
+
+    for (size_t i = 0; i < arguments.size(); ++i) {
+        UString argAsString = arguments[i]->toString(exec);
+        printf(" %s", argAsString.UTF8String().c_str());
+    }
+
+    printf("\n");
+}
+
 void Console::addMessage(MessageSource source, MessageLevel level, const String& message, unsigned lineNumber, const String& sourceURL)
 {
     if (!m_frame)
@@ -67,6 +152,8 @@ void Console::addMessage(MessageSource source, MessageLevel level, const String&
         page->chrome()->client()->addMessageToConsole(message, lineNumber, sourceURL);
 
     page->inspectorController()->addMessageToConsole(source, level, message, lineNumber, sourceURL);
+
+    printToStandardOut(source, level, message, sourceURL, lineNumber);
 }
 
 void Console::error(ExecState* exec, const List& arguments)
@@ -87,6 +174,8 @@ void Console::error(ExecState* exec, const List& arguments)
 
     page->chrome()->client()->addMessageToConsole(message, 0, prettyURL);
     page->inspectorController()->addMessageToConsole(JSMessageSource, ErrorMessageLevel, exec, arguments, 0, url.string());
+
+    printToStandardOut(ErrorMessageLevel, exec, arguments, url);
 }
 
 void Console::info(ExecState* exec, const List& arguments)
@@ -107,6 +196,8 @@ void Console::info(ExecState* exec, const List& arguments)
 
     page->chrome()->client()->addMessageToConsole(message, 0, prettyURL);
     page->inspectorController()->addMessageToConsole(JSMessageSource, LogMessageLevel, exec, arguments, 0, url.string());
+
+    printToStandardOut(LogMessageLevel, exec, arguments, url);
 }
 
 void Console::log(ExecState* exec, const List& arguments)
@@ -127,6 +218,8 @@ void Console::log(ExecState* exec, const List& arguments)
 
     page->chrome()->client()->addMessageToConsole(message, 0, prettyURL);
     page->inspectorController()->addMessageToConsole(JSMessageSource, LogMessageLevel, exec, arguments, 0, url.string());
+
+    printToStandardOut(LogMessageLevel, exec, arguments, url);
 }
 
 void Console::assertCondition(bool condition, ExecState* exec, const List& arguments)
@@ -147,6 +240,8 @@ void Console::assertCondition(bool condition, ExecState* exec, const List& argum
     // FIXME: <https://bugs.webkit.org/show_bug.cgi?id=19136> We should print a message even when arguments.isEmpty() is true.
 
     page->inspectorController()->addMessageToConsole(JSMessageSource, ErrorMessageLevel, exec, arguments, 0, url.string());
+
+    printToStandardOut(ErrorMessageLevel, exec, arguments, url);
 }
 
 void Console::profile(ExecState* exec, const List& arguments) const
@@ -192,6 +287,8 @@ void Console::warn(ExecState* exec, const List& arguments)
 
     page->chrome()->client()->addMessageToConsole(message, 0, prettyURL);
     page->inspectorController()->addMessageToConsole(JSMessageSource, WarningMessageLevel, exec, arguments, 0, url.string());
+
+    printToStandardOut(WarningMessageLevel, exec, arguments, url);
 }
 
 } // namespace WebCore