forked from gen0cide/gscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
98 lines (91 loc) · 2.47 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package engine
import (
"errors"
"fmt"
"strings"
"github.com/robertkrimen/otto"
)
var (
// the console functions that belong to the javascript console object
consoleLogLevels = []string{
"log",
"debug",
"info",
"error",
"warn",
}
)
// converts arguments passed into console into helpful string representations
// otto by default prints objects as [Object object] which is incredibly not
// helpful -.-
func normalizeConsoleArgs(c otto.FunctionCall) string {
o := []string{}
jsonNs, err := c.Otto.Object("JSON")
if err != nil {
return errors.New("runtime error: could not locate the JSON runtime object").Error()
}
for _, i := range c.ArgumentList {
if i.Class() == "Object" {
i, err = jsonNs.Call("stringify", i)
if err != nil {
o = append(o, err.Error())
continue
}
}
o = append(o, fmt.Sprintf("%v", i))
}
return strings.Join(o, " ")
}
// generates native functions for each of the log levels that use the appropriate logger function
// belonging to the provided engine
func logFuncFactory(e *Engine, level string) func(call otto.FunctionCall) otto.Value {
prefix := fmt.Sprintf("console.%s >>> ", level)
switch level {
case "log":
return func(call otto.FunctionCall) otto.Value {
e.Logger.Info(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
case "debug":
return func(call otto.FunctionCall) otto.Value {
e.Logger.Debug(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
case "info":
return func(call otto.FunctionCall) otto.Value {
e.Logger.Info(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
case "error":
return func(call otto.FunctionCall) otto.Value {
e.Logger.Error(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
case "warn":
return func(call otto.FunctionCall) otto.Value {
e.Logger.Warn(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
default:
return func(call otto.FunctionCall) otto.Value {
e.Logger.Info(prefix, normalizeConsoleArgs(call))
return otto.Value{}
}
}
}
// HijackConsoleLogging intercepts the javascript runtimes console logging functions (i.e. console.log)
// and dynamically generates new native function implementations of those build ins that use
// the engine object's Logger interface
func HijackConsoleLogging(e *Engine) error {
c, err := e.VM.Object(`console`)
if err != nil {
return err
}
for _, l := range consoleLogLevels {
err = c.Set(l, logFuncFactory(e, l))
if err != nil {
return err
}
}
return nil
}