Skip to content

Commit

Permalink
FABN-1265 NodeSDK remove logging go chaincode
Browse files Browse the repository at this point in the history
Remove the use of the logging provided by the shim.

Change-Id: Ifecec571d7ce3e1f6f03e985c662f1bc6116a801
Signed-off-by: Bret Harrison <[email protected]>
  • Loading branch information
harrisob committed Jun 7, 2019
1 parent 3fbe8cd commit 1e28d1b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
var logger = shim.NewLogger("events_cc")


// EventSender example simple Chaincode implementation
type EventSender struct {
}

// Init function
func (t *EventSender) Init(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("*********** Init ***********")
fmt.Printf("*********** Init ***********")

err := stub.PutState("numEvents", []byte("0"))
if err != nil {
Expand All @@ -42,7 +42,7 @@ func (t *EventSender) Init(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke - invoke the chaincode
func (t *EventSender) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("*********** Invoke ***********")
fmt.Printf("*********** Invoke ***********")
function, args := stub.GetFunctionAndParameters()

if function != "invoke" {
Expand All @@ -62,7 +62,7 @@ func (t *EventSender) Invoke(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke function
func (t *EventSender) invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### invoke start ###########")
fmt.Printf("########### invoke start ###########")

_ , args := stub.GetFunctionAndParameters()
if len(args) != 2 {
Expand All @@ -77,9 +77,9 @@ func (t *EventSender) invoke(stub shim.ChaincodeStubInterface) pb.Response {
tosend := "Event " + string(b) + args[1]
eventName := "evtsender" + args[0]

logger.Infof("########### invoke - numEvents:%s\n", numEvents)
logger.Infof("########### invoke - tosend:%s\n", tosend)
logger.Infof("########### invoke - eventName:%s\n", eventName)
fmt.Printf("########### invoke - numEvents:%s\n", numEvents)
fmt.Printf("########### invoke - tosend:%s\n", tosend)
fmt.Printf("########### invoke - eventName:%s\n", eventName)

err = stub.PutState("numEvents", []byte(strconv.Itoa(numEvents+1)))
if err != nil {
Expand All @@ -95,7 +95,7 @@ func (t *EventSender) invoke(stub shim.ChaincodeStubInterface) pb.Response {

// Clear State function
func (t *EventSender) clear(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### clear ###########")
fmt.Printf("########### clear ###########")

err := stub.PutState("numEvents", []byte("0"))
if err != nil {
Expand All @@ -106,11 +106,11 @@ func (t *EventSender) clear(stub shim.ChaincodeStubInterface) pb.Response {

// Query function
func (t *EventSender) query(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### query ###########")
fmt.Printf("########### query ###########")

b, err := stub.GetState("numEvents")
numEvents, _ := strconv.Atoi(string(b))
logger.Infof("########### query - numEvents:%s\n", numEvents)
fmt.Printf("########### query - numEvents:%s\n", numEvents)

if err != nil {
return shim.Error("Failed to get state")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
)

var logger = shim.NewLogger("example_cc0")

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

// Init - initialize the state
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc0 Init ###########")
fmt.Printf("########### example_cc0 Init ###########")

_, args := stub.GetFunctionAndParameters()
var A, B string // Entities
Expand All @@ -55,7 +53,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
logger.Infof("Aval = %d, Bval = %d\n", Aval, Bval)
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand All @@ -73,12 +71,12 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke - Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc0 Invoke ###########")
fmt.Printf("########### example_cc0 Invoke ###########")

function, args := stub.GetFunctionAndParameters()

logger.Info("function: ", function)
logger.Info("args: ", args)
fmt.Printf("function: ", function)
fmt.Printf("args: ", args)

if function == "delete" {
// Deletes an entity from its state
Expand All @@ -104,7 +102,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return t.Init(stub)
}

logger.Errorf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0])
fmt.Printf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0])
return shim.Error(fmt.Sprintf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0]))
}

Expand Down Expand Up @@ -149,7 +147,7 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
}
Aval = Aval - X
Bval = Bval + X
logger.Infof("Aval = %d, Bval = %d\n", Aval, Bval)
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand Down Expand Up @@ -207,7 +205,7 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
logger.Infof("Query Response:%s\n", jsonResp)
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}

Expand All @@ -219,6 +217,6 @@ func (t *SimpleChaincode) throwError(stub shim.ChaincodeStubInterface, args []st
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
logger.Errorf("Error starting Simple chaincode: %s", err)
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,22 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
)

var logger = shim.NewLogger("example_cc1")

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

//Init - instantiation of the state
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {

logger.Info("########### example_cc1 Init ###########")
fmt.Printf("########### example_cc1 Init ###########")

// test the transient map support with chaincode instantiation
return t.testTransient(stub)
}

// Invoke - Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc1 Invoke ###########")
fmt.Printf("########### example_cc1 Invoke ###########")

function, args := stub.GetFunctionAndParameters()

Expand Down Expand Up @@ -71,7 +69,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return t.testTransient(stub)
}

logger.Errorf("Unknown action: %s, check the first argument, must be one of 'delete', 'query', 'echo', 'testTransient' or 'move'", args[0])
fmt.Printf("Unknown action: %s, check the first argument, must be one of 'delete', 'query', 'echo', 'testTransient' or 'move'", args[0])
return shim.Error(fmt.Sprintf("Unknown action: %s, check the first argument, must be one of 'delete', 'query', 'echo', 'testTransient' or 'move'", args[0]))
}

Expand Down Expand Up @@ -116,7 +114,7 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
}
Aval = Aval - X
Bval = Bval + X + 10 //new version chaincode gives a bonus
logger.Infof("Aval = %d, Bval = %d\n", Aval, Bval)
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand Down Expand Up @@ -174,7 +172,7 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
logger.Infof("Query Response:%s\n", jsonResp)
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}

Expand All @@ -187,13 +185,13 @@ func (t *SimpleChaincode) throwError(stub shim.ChaincodeStubInterface, args []st
func (t *SimpleChaincode) testTransient(stub shim.ChaincodeStubInterface) pb.Response {
tm, err := stub.GetTransient()
if err != nil {
logger.Error("Did not find expected transient map in the proposal")
fmt.Printf("Did not find expected transient map in the proposal")
return shim.Error("{\"Error\":\"Did not find expected transient map in the proposal}")
}

v, ok := tm["test"]
if !ok {
logger.Error("Did not find expected key \"test\" in the transient map of the proposal")
fmt.Printf("Did not find expected key \"test\" in the transient map of the proposal")
return shim.Error("{\"Error\":\"Did not find expected key \"test\" in the transient map of the proposal}")
}

Expand All @@ -203,13 +201,13 @@ func (t *SimpleChaincode) testTransient(stub shim.ChaincodeStubInterface) pb.Res
// Used to return what's in the input for testing purposes
func (t *SimpleChaincode) echo(stub shim.ChaincodeStubInterface, args []string) pb.Response {

logger.Info("Echo Response\n")
fmt.Printf("Echo Response\n")
return shim.Success([]byte(args[0]))
}

func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
logger.Errorf("Error starting Simple chaincode: %s", err)
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
)

var logger = shim.NewLogger("example_cc2")

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

// Init - Re-initialize one of the parties' asset holdings
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {

logger.Info("########### example_cc2 Init ###########")
fmt.Printf("########### example_cc2 Init ###########")

_, args := stub.GetFunctionAndParameters()
var A string // Entities
Expand All @@ -47,7 +45,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
logger.Info("Aval = %d\n", Aval)
fmt.Printf("Aval = %d\n", Aval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand All @@ -60,7 +58,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke - Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc1 Invoke ###########")
fmt.Printf("########### example_cc1 Invoke ###########")

function, args := stub.GetFunctionAndParameters()

Expand All @@ -69,7 +67,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return t.query(stub, args)
}

logger.Errorf("Unknown action: %s, check the first argument, must be 'query'", args[0])
fmt.Printf("Unknown action: %s, check the first argument, must be 'query'", args[0])
return shim.Error(fmt.Sprintf("Unknown action: %s, check the first argument, must be 'query'", args[0]))
}

Expand Down Expand Up @@ -98,13 +96,13 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
logger.Infof("Query Response:%s\n", jsonResp)
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}

func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
logger.Errorf("Error starting Simple chaincode: %s", err)
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
pb "github.com/hyperledger/fabric/protos/peer"
)

var logger = shim.NewLogger("example_cc0_private")

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
Expand All @@ -51,7 +49,7 @@ type sensitiveCol struct {

// Init - initialize the state
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc0_private Init ###########")
fmt.Printf("########### example_cc0_private Init ###########")

_, args := stub.GetFunctionAndParameters()
var A, B string // Entities
Expand All @@ -73,7 +71,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
if err != nil {
return shim.Error("Expecting integer value for asset holding")
}
logger.Infof("Aval = %d, Bval = %d\n", Aval, Bval)
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand All @@ -91,7 +89,7 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {

// Invoke - Transaction makes payment of X units from A to B
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
logger.Info("########### example_cc0_private Invoke ###########")
fmt.Printf("########### example_cc0_private Invoke ###########")

function, args := stub.GetFunctionAndParameters()

Expand Down Expand Up @@ -125,7 +123,7 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return t.querySensitive(stub, args)
}

logger.Errorf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0])
fmt.Printf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0])
return shim.Error(fmt.Sprintf("Unknown action, check the first argument, must be one of 'delete', 'query', or 'move'. But got: %v", args[0]))
}

Expand Down Expand Up @@ -170,7 +168,7 @@ func (t *SimpleChaincode) move(stub shim.ChaincodeStubInterface, args []string)
}
Aval = Aval - X
Bval = Bval + X
logger.Infof("Aval = %d, Bval = %d\n", Aval, Bval)
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
Expand Down Expand Up @@ -318,7 +316,7 @@ func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string)
}

jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
logger.Infof("Query Response:%s\n", jsonResp)
fmt.Printf("Query Response:%s\n", jsonResp)
return shim.Success(Avalbytes)
}

Expand Down Expand Up @@ -384,6 +382,6 @@ func (t *SimpleChaincode) querySensitive(stub shim.ChaincodeStubInterface, args
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
logger.Errorf("Error starting Simple chaincode: %s", err)
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}

0 comments on commit 1e28d1b

Please sign in to comment.