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 ¶
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" "github.com/augustoroman/v8" "github.com/augustoroman/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 ¶
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" "github.com/augustoroman/v8" "github.com/augustoroman/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 ¶
Error is the v8 callback function that is registered for the console.error functions.
func (Config) Info ¶
Info is the v8 callback function that is registered for the console.log and console.info functions.