Implement Console message handler
authorSeungkeun Lee <sngn.lee@samsung.com>
Tue, 14 Apr 2015 06:13:58 +0000 (15:13 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Wed, 15 Apr 2015 01:38:58 +0000 (10:38 +0900)
Change-Id: I68ee0c43d34457db023561b5023e2855c2545441

src/runtime/web_application.cc
src/runtime/web_application.h
src/runtime/web_view.cc
src/runtime/web_view.h

index c3827bf..b017919 100755 (executable)
@@ -20,6 +20,9 @@ namespace {
   // TODO(sngn.lee) : It should be declare in common header
   const char* kKeyNameBack = "back";
 
+  const char* kConsoleLogEnableKey = "WRT_CONSOLE_LOG_ENABLE";
+  const char* kConsoleMessageLogTag = "ConsoleMessage";
+
   const char* kAppControlEventScript = \
         "var __event = document.createEvent(\"CustomEvent\");\n"
         "__event.initCustomEvent(\"appcontrol\", true, true);\n"
@@ -281,6 +284,26 @@ void WebApplication::OnLanguageChanged() {
   }
 }
 
+void WebApplication::OnConsoleMessage(const std::string& msg, int level) {
+  static bool enabled = (getenv(kConsoleLogEnableKey) != NULL);
+  // TODO(sngn.lee): check debug mode
+  if (true/*debug mode*/ || enabled) {
+    int dlog_level = DLOG_DEBUG;
+    switch (level) {
+      case EWK_CONSOLE_MESSAGE_LEVEL_WARNING:
+          dlog_level = DLOG_WARN;
+          break;
+      case EWK_CONSOLE_MESSAGE_LEVEL_ERROR:
+          dlog_level = DLOG_ERROR;
+          break;
+      default:
+          dlog_level = DLOG_DEBUG;
+          break;
+    }
+    LOG_(LOG_ID_MAIN, dlog_level, kConsoleMessageLogTag, "%s", msg.c_str());
+  }
+}
+
 void WebApplication::OnLoadStart(WebView* view) {
   LoggerD("LoadStart");
 }
index f084092..3e50926 100755 (executable)
@@ -39,6 +39,7 @@ class WebApplication : public WebView::EventListener {
                                  bool lock,
                                  int preferred_rotation);
   virtual void OnHardwareKey(WebView* view, const std::string& keyname);
+  virtual void OnConsoleMessage(const std::string& msg, int level);
 
   virtual void OnLoadStart(WebView* view);
   virtual void OnLoadFinished(WebView* view);
index ba5c3b0..8413874 100755 (executable)
@@ -6,6 +6,7 @@
 
 #include <ewk_chromium.h>
 #include <functional>
+#include <sstream>
 
 #include "runtime/native_window.h"
 
@@ -297,6 +298,31 @@ void WebView::Initialize() {
                                          orientation_lock_callback,
                                          this);
 
+  auto console_message_callback = [](void* user_data,
+                                 Evas_Object*,
+                                 void* event_info) {
+    WebView* self = static_cast<WebView*>(user_data);
+    if (!self->listener_) {
+      return;
+    }
+    Ewk_Console_Message* msg = static_cast<Ewk_Console_Message*>(event_info);
+    unsigned int line_number = ewk_console_message_line_get(msg);
+
+    std::stringstream buf;
+    if (line_number) {
+        buf << ewk_console_message_source_get(msg) << ":";
+        buf << line_number << ":";
+    }
+    buf << ewk_console_message_text_get(msg);
+    int level = ewk_console_message_level_get(msg);
+    self->listener_->OnConsoleMessage(buf.str(), level);
+  };
+  // console log
+  evas_object_smart_callback_add(ewk_view_,
+                                 "console,message",
+                                 console_message_callback,
+                                 this);
+
   // rotation support
   ewk_view_orientation_send(ewk_view_, ToWebRotation(window_->rotation()));
   rotation_handler_id_ = window_->AddRotationHandler(
index 2dd0ee9..08e57c9 100755 (executable)
@@ -39,6 +39,7 @@ class WebView {
         WebView* /*view*/,
         bool /*lock*/,
         int /*preferred_rotation*/) {}
+    virtual void OnConsoleMessage(const std::string& /*msg*/, int /*level*/) {}
   };
 
   WebView(wrt::NativeWindow* window, Ewk_Context* context);