-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
132 lines (109 loc) · 3.16 KB
/
console.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//go:build js && wasm
// +build js,wasm
package console
import (
"syscall/js"
)
// MISSING:
// console.table()
//
// Most functions call js.Call() with a variadic argument list.
// Thus, the arguments get mapped to JavaScript values according to the js.ValueOf function.
type Stringer interface {
String() string
}
// Javascript Console.assert() method
func Assert(condition bool, args ...interface{}) {
if condition {
js.Global().Get("console").Call("assert", append([]any{condition}, args...)...)
return
}
}
// Javascript Console.clear() method
func Clear() {
js.Global().Get("console").Call("clear")
}
// Javascript Console.count() method
func Count(label string) {
js.Global().Get("console").Call("count", label)
}
// Javascript Console.countReset() method
func CountReset(label string) {
js.Global().Get("console").Call("countReset", label)
}
// Javascript Console.debug() method
func Debug(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("debug", args...)
}
// Javascript Console.dir() method
func Dir(item interface{}) {
js.Global().Get("console").Call("dir", item)
}
// Javascript Console.dirxml() method
func DirXML(item interface{}) {
js.Global().Get("console").Call("dirxml", item)
}
// Javascript Console.error() method
func Error(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("error", args...)
}
// Javascript Console.exception() method
func Group(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("group", args...)
}
// Javascript Console.groupCollapsed() method
func GroupCollapsed(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("groupCollapsed", args...)
}
// Javascript Console.groupEnd() method
func GroupEnd() {
js.Global().Get("console").Call("groupEnd")
}
// Javascript Console.info() method
func Info(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("info", args...)
}
// Javascript Console.log() method
func Log(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("log", args...)
}
// Javascript Console.time() method
func Time(label string) {
js.Global().Get("console").Call("time", label)
}
// Javascript Console.timeEnd() method
func TimeEnd(label string) {
js.Global().Get("console").Call("timeEnd", label)
}
// Javascript Console.timeLog() method
func TimeLog(label string, args ...interface{}) {
js.Global().Get("console").Call("timeLog", append([]any{label}, args...)...)
}
// Javascript Console.trace() method
func Trace(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("trace", args...)
}
// Javascript Console.warn() method
func Warn(args ...interface{}) {
args = replaceArgs(args)
js.Global().Get("console").Call("warn", args...)
}
func replaceArgs(args []interface{}) []interface{} {
if len(args) > 0 {
for i, arg := range args {
if err, ok := arg.(error); ok {
args[i] = err.Error()
} else if err, ok := arg.(Stringer); ok {
args[i] = err.String()
}
}
}
return args
}