-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
loggingservice.go
115 lines (99 loc) · 3.96 KB
/
loggingservice.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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/robertkrimen/otto"
)
// LoggingService is a Go wrapper around an instance of the LoggingService JavaScript class.
type LoggingService struct {
This *otto.Object
Stub shim.ChaincodeStubInterface
}
// NewLoggingService creates a Go wrapper around a new instance of the LoggingService JavaScript class.
func NewLoggingService(vm *otto.Otto, container *Container, stub shim.ChaincodeStubInterface) (result *LoggingService) {
logger.Debug("Entering NewLoggingService", vm, container, stub)
defer func() { logger.Debug("Exiting NewLoggingService", result) }()
// Create a new instance of the JavaScript chaincode class.
temp, err := vm.Call("new composer.LoggingService", nil, container.This)
if err != nil {
panic(fmt.Sprintf("Failed to create new instance of LoggingService JavaScript class: %v", err))
} else if !temp.IsObject() {
panic("New instance of LoggingService JavaScript class is not an object")
}
object := temp.Object()
// Add a pointer to the Go object into the JavaScript object.
result = &LoggingService{This: temp.Object(), Stub: stub}
err = object.Set("$this", result)
if err != nil {
panic(fmt.Sprintf("Failed to store Go object in LoggingService JavaScript object: %v", err))
}
// Bind the methods into the JavaScript object.
result.This.Set("logCritical", result.logCritical)
result.This.Set("logDebug", result.logDebug)
result.This.Set("logError", result.logError)
result.This.Set("logInfo", result.logInfo)
result.This.Set("logNotice", result.logNotice)
result.This.Set("logWarning", result.logWarning)
return result
}
// getLogInserts extracts the list of JavaScript arguments and converts them into a Go array.
func (loggingService *LoggingService) getLogInserts(call otto.FunctionCall) (result []interface{}) {
result = []interface{}{}
for _, arg := range call.ArgumentList {
str, err := arg.ToString()
if err != nil {
str = err.Error()
}
result = append(result, str)
}
return result
}
// logCritical writes a critical message to the log.
func (loggingService *LoggingService) logCritical(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Critical(strings...)
return otto.UndefinedValue()
}
// logDebug writes a debug message to the log.
func (loggingService *LoggingService) logDebug(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Debug(strings...)
return otto.UndefinedValue()
}
// logError writes a error message to the log.
func (loggingService *LoggingService) logError(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Error(strings...)
return otto.UndefinedValue()
}
// logInfo writes a info message to the log.
func (loggingService *LoggingService) logInfo(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Info(strings...)
return otto.UndefinedValue()
}
// logNotice writes a notice message to the log.
func (loggingService *LoggingService) logNotice(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Notice(strings...)
return otto.UndefinedValue()
}
// logWarning writes a warning message to the log.
func (loggingService *LoggingService) logWarning(call otto.FunctionCall) otto.Value {
strings := loggingService.getLogInserts(call)
logger.Warning(strings...)
return otto.UndefinedValue()
}