v8console

package
v8.0.0-...-2e3dd65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package v8console provides a simple console implementation to allow JS to log messages.

It supports the console.log, console.info, console.warn, and console.error functions and logs the result of .ToString() on each of the arguments. It can color warning and error messages, but does not support Chrome's fancy %c message styling.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlushSnapshotAndInject

func FlushSnapshotAndInject(ctx *v8.Context, c Config) (exception *v8.Value)

FlushSnapshotAndInject replaces the stub console operations with the console described by Config and flushes any stored log messages to the new console. This is specifically intended for adapting a Context created using WrapForSnapshot().

Example
package main

import (
	"fmt"
	"os"

	v8 "github.com/cloudinaryltd/v8"
	"github.com/cloudinaryltd/v8/v8console"
)

func main() {
	const myJsCode = `
        // Typically this will be an auto-generated js bundle file.
        function require() {} // fake stub
        var when = require('when');
        var _ = require('lodash');
        function renderPage(name) { return "<html><body>Hi " + name + "!"; }
        console.warn('snapshot initialization');
    `
	snapshot := v8.CreateSnapshot(v8console.WrapForSnapshot(myJsCode))
	ctx := v8.NewIsolateWithSnapshot(snapshot).NewContext()
	console := v8console.Config{"console> ", os.Stdout, os.Stdout, false}
	if exception := v8console.FlushSnapshotAndInject(ctx, console); exception != nil {
		panic(fmt.Errorf("Panic during snapshot creation: %v", exception.String()))
	}
	_, err := ctx.Eval(`console.warn('after snapshot');`, `somefile.js`)
	if err != nil {
		panic(err)
	}

}
Output:

console> [<embedded>:8] snapshot initialization
console> [somefile.js:1] after snapshot

func WrapForSnapshot

func WrapForSnapshot(jsCode string) string

WrapForSnapshot wraps the provided javascript code with a small, global console stub object that will record all console logs. This is necessary when creating a snapshot for code that expects console.log to exist. It also surrounds the jsCode with a try/catch that logs the error, since otherwise the snapshot will quietly fail.

Types

type Config

type Config struct {
	// Prefix to prepend to every log message.
	Prefix string
	// Destination for all .log and .info calls.
	Stdout io.Writer
	// Destination for all .warn and .error calls.
	Stderr io.Writer
	// Whether to enable ANSI color escape codes in the output.
	Colorize bool
}

Config holds configuration for a particular console instance.

Example
package main

import (
	"os"

	v8 "github.com/cloudinaryltd/v8"
	"github.com/cloudinaryltd/v8/v8console"
)

func main() {
	ctx := v8.NewIsolate().NewContext()
	v8console.Config{"> ", os.Stdout, os.Stdout, false}.Inject(ctx)
	ctx.Eval(`
        console.log('hi there');
        console.info('info 4 u');
        console.warn("Where's mah bucket?");
        console.error("Oh noes!");
    `, "filename.js")
	// You can also update the console:
	v8console.Config{":-> ", os.Stdout, os.Stdout, false}.Inject(ctx)
	ctx.Eval(`console.log("I'm so happy");`, "file2.js")

}
Output:

> hi there
> info 4 u
> [filename.js:4] Where's mah bucket?
> [filename.js:5] Oh noes!
:-> I'm so happy

func (Config) Error

func (c Config) Error(in v8.CallbackArgs) (*v8.Value, error)

Error is the v8 callback function that is registered for the console.error functions.

func (Config) Info

func (c Config) Info(in v8.CallbackArgs) (*v8.Value, error)

Info is the v8 callback function that is registered for the console.log and console.info functions.

func (Config) Inject

func (c Config) Inject(ctx *v8.Context)

Inject sets the global "console" object of the specified Context to bind .log, .info, .warn, and .error to call this Console object. If the console object already exists in the global namespace, only the log/info/warn/error properties are replaced.

func (Config) Warn

func (c Config) Warn(in v8.CallbackArgs) (*v8.Value, error)

Warn is the v8 callback function that is registered for the console.warn functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL