github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/chaincode/config.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package chaincode 18 19 import ( 20 "flag" 21 "fmt" 22 "runtime" 23 "strings" 24 25 "github.com/op/go-logging" 26 "github.com/spf13/viper" 27 28 "github.com/hyperledger/fabric/bccsp/factory" 29 ) 30 31 // Config the config wrapper structure 32 type Config struct { 33 } 34 35 func init() { 36 37 } 38 39 // SetupTestLogging setup the logging during test execution 40 func SetupTestLogging() { 41 level, err := logging.LogLevel(viper.GetString("logging.peer")) 42 if err == nil { 43 // No error, use the setting 44 logging.SetLevel(level, "main") 45 logging.SetLevel(level, "server") 46 logging.SetLevel(level, "peer") 47 } else { 48 chaincodeLogger.Warningf("Log level not recognized '%s', defaulting to %s: %s", viper.GetString("logging.peer"), logging.ERROR, err) 49 logging.SetLevel(logging.ERROR, "main") 50 logging.SetLevel(logging.ERROR, "server") 51 logging.SetLevel(logging.ERROR, "peer") 52 } 53 } 54 55 // SetupTestConfig setup the config during test execution 56 func SetupTestConfig() { 57 flag.Parse() 58 59 // Now set the configuration file 60 viper.SetEnvPrefix("CORE") 61 viper.AutomaticEnv() 62 replacer := strings.NewReplacer(".", "_") 63 viper.SetEnvKeyReplacer(replacer) 64 viper.SetConfigName("chaincodetest") // name of config file (without extension) 65 viper.AddConfigPath("./") // path to look for the config file in 66 err := viper.ReadInConfig() // Find and read the config file 67 if err != nil { // Handle errors reading the config file 68 panic(fmt.Errorf("Fatal error config file: %s \n", err)) 69 } 70 71 SetupTestLogging() 72 73 // Set the number of maxprocs 74 var numProcsDesired = viper.GetInt("peer.gomaxprocs") 75 chaincodeLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2)) 76 77 // Init the BCCSP 78 err = factory.InitFactories(nil) 79 if err != nil { 80 panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err)) 81 } 82 }