docs

Logging Console Messages

On this page

You can get log output for JavaScript errors, network errors, and more to help diagnose issues when writing code for your View.

Seeconsole.log() in JavaScript: https://developer.mozilla.org/en-US/docs/Web/API/console/log_static

Attaching the ViewListener Interface

You should first subclass ViewListener and implement the ViewListener::OnAddConsoleMessage() virtual method.

Then, bind an instance of your subclass to a View via View::set_view_listener().

See the API reference for ViewListener here.

Handling ViewListener::OnAddConsoleMessage()

Here's an example that logs console messages to std::cout:

C++
#include <Ultralight/Ultralight.h>
#include <iostream>
  
using namespace ultralight;

//
// Inherited from ViewListener::OnAddConsoleMessage
//
// Make sure that you bind 'MyApp' to 'View::set_view_listener'
// to receive this event.
//
void MyApp::OnAddConsoleMessage(View* caller, const ConsoleMessage& msg) {
    std::cout << "[OnAddConsoleMessage]\n\t"
              << "\n\tsource:\t" << (uint32_t)msg.source()
              << "\n\ttype:\t" << (uint32_t)msg.type()
              << "\n\tlevel:\t" << (uint32_t)msg.level()
              << "\n\tmessage:\t" << msg.message().utf8().data()
              << "\n\tline_number:\t" << msg.line_number()
              << "\n\tcolumn_number:\t" << msg.column_number()
              << "\n\tsource_id:\t" << msg.source_id().utf8().data()
              << "\n\tnum_arguments:\t" << msg.num_arguments() << std::endl;

    // console.log() in JavaScript can be passed multiple arguments-- you can
    // get the raw JavaScript values passed to the function via the following:
    uint32_t num_args = msg.num_arguments();
    if (num_args > 0) {
        for (uint32_t i = 0; i < num_args; i++) {
            JSValueRef arg = msg.argument_at(i);
            // Optionally print the argument here, see the JavaScriptCore API
            // for converting these values to a string.
        }
    }
}