2017-08-04 23:14:17 +07:00
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-09-25 20:22:57 +02:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2017-08-04 23:14:17 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Write provides the base function to write data to the underline writer
|
|
|
|
// for the underline otto vm.
|
|
|
|
func Write(fn otto.FunctionCall, w io.Writer, consoleEventName string) otto.Value {
|
2017-09-25 20:22:57 +02:00
|
|
|
signal.Send(signal.Envelope{
|
2017-08-04 23:14:17 +07:00
|
|
|
Type: consoleEventName,
|
|
|
|
Event: convertArgs(fn.ArgumentList),
|
|
|
|
})
|
|
|
|
|
|
|
|
// Next print out the giving values.
|
Remove //nolint: gas directives from fmt.Fprintf #590 (#656)
Summary:
Filter out gas linter error checks for fmt.Fprintf commands. This required defining a custom linter around gas that additionally included the offending code.
Notes:
Gas format, without piping it through gometalinter, gives output like this:
$ gas -fmt=csv geth/jail/console/console.go
geth/jail/console/console.go,21,Errors unhandled.,LOW,HIGH,"fmt.Fprintf(w, ""%s: %s"", consoleEventName, formatForConsole(fn.ArgumentList))"
Gometalinter, by default, does not grab the line of code when it filters gas errors. To resolve this, I created a wrapper around gas (I wasn't sure what to call this "gas wrapper", I opted for gasv2, open to other names).
The first part of the regular expression was taken directly from gometalinter (see https://github.com/alecthomas/gometalinter/blob/master/linters.go#L236), and I then appended ,\".*\" to additionally grab the line of code of the offending line. Lastly, I excluded ".*Errors unhandled.*fmt.Fprintf.*" to filter out only fmt.Fprintf errors around omitted errors.
Also as a result of this change, gas lint output will now include the offending code.
Closes #590
2018-02-14 12:58:20 -05:00
|
|
|
fmt.Fprintf(w, "%s: %s", consoleEventName, formatForConsole(fn.ArgumentList))
|
2017-08-04 23:14:17 +07:00
|
|
|
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatForConsole handles conversion of giving otto.Values into
|
|
|
|
// string counter part.
|
|
|
|
func formatForConsole(argumentList []otto.Value) string {
|
|
|
|
output := []string{}
|
|
|
|
for _, argument := range argumentList {
|
|
|
|
output = append(output, fmt.Sprintf("%v", argument))
|
|
|
|
}
|
|
|
|
return strings.Join(output, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertArgs attempts to convert otto.Values into proper go types else
|
|
|
|
// uses original.
|
|
|
|
func convertArgs(argumentList []otto.Value) []interface{} {
|
|
|
|
var items []interface{}
|
|
|
|
|
|
|
|
for _, arg := range argumentList {
|
|
|
|
realArg, err := arg.Export()
|
|
|
|
if err != nil {
|
|
|
|
items = append(items, arg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, realArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|