This commit consists of:
1) Fixing the core tests
2) Fix an error in the logger where the runtime stack trace behaved
differently in go 1.10 vs go 1.9.
3) Minor other fixes
Change-Id: I1263df38ffcd733174f776a0901583cfb59c616e
diff --git a/common/log/log.go b/common/log/log.go
index e86ba0d..7f43e87 100644
--- a/common/log/log.go
+++ b/common/log/log.go
@@ -149,26 +149,6 @@
return defaultLogger, nil
}
-func SetPackageLevelLoggers(outputType string, level int, defaultFields Fields, pkgNames []string) error {
- cfgs = make(map[string]zp.Config)
- loggers = make(map[string]*logger)
- for _, pkg := range pkgNames {
- // Build a custom config using zap - for now initialzie all packages uses the same config
- cfgs[pkg] = getDefaultConfig(outputType, level, defaultFields)
-
- l, err := cfgs[pkg].Build()
- if err != nil {
- return err
- }
-
- loggers[pkg] = &logger{
- log: l.Sugar(),
- parent: l,
- }
- }
-
- return nil
-}
func AddPackage(outputType string, level int, defaultFields Fields) error {
if cfgs == nil {
@@ -217,24 +197,6 @@
return nil
}
-//func SetDefaultLoglevel(level int) {
-// switch level {
-// case DebugLevel:
-// cfg.Level.SetLevel(zc.DebugLevel)
-// case InfoLevel:
-// cfg.Level.SetLevel(zc.InfoLevel)
-// case WarnLevel:
-// cfg.Level.SetLevel(zc.WarnLevel)
-// case ErrorLevel:
-// cfg.Level.SetLevel(zc.ErrorLevel)
-// case PanicLevel:
-// cfg.Level.SetLevel(zc.PanicLevel)
-// case FatalLevel:
-// cfg.Level.SetLevel(zc.FatalLevel)
-// default:
-// cfg.Level.SetLevel(zc.ErrorLevel)
-// }
-//}
func SetPackageLogLevel(packageName string, level int) {
// Get proper config
@@ -258,6 +220,28 @@
}
}
+func SetAllLogLevel(level int) {
+ // Get proper config
+ for _, cfg := range cfgs{
+ switch level {
+ case DebugLevel:
+ cfg.Level.SetLevel(zc.DebugLevel)
+ case InfoLevel:
+ cfg.Level.SetLevel(zc.InfoLevel)
+ case WarnLevel:
+ cfg.Level.SetLevel(zc.WarnLevel)
+ case ErrorLevel:
+ cfg.Level.SetLevel(zc.ErrorLevel)
+ case PanicLevel:
+ cfg.Level.SetLevel(zc.PanicLevel)
+ case FatalLevel:
+ cfg.Level.SetLevel(zc.FatalLevel)
+ default:
+ cfg.Level.SetLevel(zc.ErrorLevel)
+ }
+ }
+}
+
// CleanUp flushed any buffered log entries. Applications should take care to call
// CleanUp before exiting.
func CleanUp() error {
@@ -280,82 +264,55 @@
return nil
}
-// GetLogger returned the default logger. If SetLogger was not previously invoked then
-// this method will return an error
-//func GetLogger() (Logger, error) {
-// if defaultLogger == nil {
-// // Setup the logger with default values - debug level,
-// SetDefaultLogger(JSON, 0, Fields{"instanceId": "default-logger"})
-// //return nil, errors.New("Uninitialized-logger")
-// }
-// return defaultLogger, nil
-//}
-
-//func extractFileNameAndLineNumber(skipLevel int) (string, int) {
-// _, file, line, ok := runtime.Caller(skipLevel)
-// var key string
-// if !ok {
-// key = "<???>"
-// line = 1
-// } else {
-// slash := strings.LastIndex(file, "/")
-// key = file[slash+1:]
-// }
-// return key, line
-//}
-
-// sourced adds a source field to the logger that contains
-// the file name and line where the logging happened.
-//func (l *logger) sourced() *zp.SugaredLogger {
-// key, line := extractFileNameAndLineNumber(3)
-// if strings.HasSuffix(key, "log.go") || strings.HasSuffix(key, "proc.go") {
-// // Go to a lower level
-// key, line = extractFileNameAndLineNumber(2)
-// }
-// if !strings.HasSuffix(key, ".go") {
-// // Go to a higher level
-// key, line = extractFileNameAndLineNumber(4)
-// }
-//
-// return l.log.With("caller", fmt.Sprintf("%s:%d", key, line))
-//}
-
-func retrieveCallInfo(skiplevel int) (string, string, string, int) {
- pc, file, line, _ := runtime.Caller(skiplevel)
- _, fileName := path.Split(file)
- parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
- pl := len(parts)
+func getCallerInfo() (string, string, string, int){
+ // Since the caller of a log function is one stack frame before (in terms of stack higher level) the log.go
+ // filename, then first look for the last log.go filename and then grab the caller info one level higher.
+ maxLevel := 3
+ skiplevel := 3 // Level with the most empirical success to see the last log.go stack frame.
+ pc := make([]uintptr, maxLevel)
+ n := runtime.Callers(skiplevel, pc)
packageName := ""
- funcName := parts[pl-1]
-
- if parts[pl-2][0] == '(' {
- //funcName = parts[pl-2] + "." + funcName
- packageName = strings.Join(parts[0:pl-2], ".")
- } else {
- packageName = strings.Join(parts[0:pl-1], ".")
+ funcName := ""
+ fileName := ""
+ var line int
+ if n == 0 {
+ return packageName, fileName, funcName, line
+ }
+ frames := runtime.CallersFrames(pc[:n])
+ var frame runtime.Frame
+ var foundFrame runtime.Frame
+ more := true
+ for more {
+ frame, more = frames.Next()
+ _, fileName = path.Split(frame.File)
+ if fileName != "log.go" {
+ foundFrame = frame // First frame after log.go in the frame stack
+ break
+ }
+ }
+ parts := strings.Split(foundFrame.Function, ".")
+ pl := len(parts)
+ if pl >= 2 {
+ funcName = parts[pl-1]
+ if parts[pl-2][0] == '(' {
+ packageName = strings.Join(parts[0:pl-2], ".")
+ } else {
+ packageName = strings.Join(parts[0:pl-1], ".")
+ }
}
- return packageName, fileName, funcName, line
-}
-
-func getCallerInfo() (string, string, string, int) {
- packageName, fileName, funcName, line := retrieveCallInfo(3)
-
- if strings.HasSuffix(funcName, "log.go") || strings.HasSuffix(funcName, "proc.go") || strings.HasSuffix(packageName, ".init") {
- // Go to a lower level
- packageName, fileName, funcName, line = retrieveCallInfo(2)
- }
- if !strings.HasSuffix(funcName, ".go") {
- // Go to a higher level
- packageName, fileName, funcName, line = retrieveCallInfo(4)
+ if strings.HasSuffix(packageName, ".init") {
+ packageName= strings.TrimSuffix(packageName, ".init")
}
if strings.HasSuffix(fileName, ".go") {
- fileName = strings.TrimSuffix(fileName, ".go")
+ fileName= strings.TrimSuffix(fileName, ".go")
}
- return packageName, fileName, funcName, line
+
+ return packageName, fileName, funcName, foundFrame.Line
}
+
func getPackageLevelLogger() *zp.SugaredLogger {
pkgName, fileName, funcName, line := getCallerInfo()
if _, exist := loggers[pkgName]; exist {
diff --git a/db/model/proxy_test.go b/db/model/proxy_test.go
index ad02eb6..e7b644c 100644
--- a/db/model/proxy_test.go
+++ b/db/model/proxy_test.go
@@ -52,9 +52,10 @@
)
func init() {
- if _, err := log.SetLogger(log.CONSOLE, 0, log.Fields{"instanceId": "proxy_test"}); err != nil {
- log.With(log.Fields{"error": err}).Fatal("cannot setup logging")
- }
+
+ log.AddPackage(log.JSON, log.ErrorLevel, nil)
+ log.UpdateAllLoggers(log.Fields{"instanceId": "proxy_test"})
+
defer log.CleanUp()
pt.Backend = NewBackend(pt.DbType, pt.DbHost, pt.DbPort, pt.DbTimeout, pt.DbPrefix)
diff --git a/db/model/transaction_test.go b/db/model/transaction_test.go
index 064b9ef..130a992 100644
--- a/db/model/transaction_test.go
+++ b/db/model/transaction_test.go
@@ -50,9 +50,10 @@
)
func init() {
- if _, err := log.SetLogger(log.CONSOLE, 0, log.Fields{"instanceId": "transaction_test"}); err != nil {
- log.With(log.Fields{"error": err}).Fatal("cannot setup logging")
- }
+
+ log.AddPackage(log.JSON, log.ErrorLevel, nil)
+ log.UpdateAllLoggers(log.Fields{"instanceId": "transaction_test"})
+
defer log.CleanUp()
tx.Backend = NewBackend(tx.DbType, tx.DbHost, tx.DbPort, tx.DbTimeout, tx.DbPrefix)
diff --git a/kafka/kafka_inter_container_library.go b/kafka/kafka_inter_container_library.go
index c00fb60..619bd17 100644
--- a/kafka/kafka_inter_container_library.go
+++ b/kafka/kafka_inter_container_library.go
@@ -772,6 +772,10 @@
}
for _, arg := range kvArgs {
+ if arg == nil {
+ // In case the caller sends an array with empty args
+ continue
+ }
var marshalledArg *any.Any
var err error
// ascertain the value interface type is a proto.Message
diff --git a/rw_core/core/adapter_request_handler.go b/rw_core/core/adapter_request_handler.go
index 95ad69d..bfc4ee4 100644
--- a/rw_core/core/adapter_request_handler.go
+++ b/rw_core/core/adapter_request_handler.go
@@ -118,15 +118,15 @@
err := errors.New("invalid-number-of-args")
return nil, err
}
- pID := &ca.StrType{}
+ pID := &voltha.ID{}
if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
log.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
return nil, err
}
- log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Val})
+ log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Id})
if rhp.TestMode { // Execute only for test cases
- return &voltha.Device{Id: pID.Val}, nil
+ return &voltha.Device{Id: pID.Id}, nil
}
return nil, nil
}
@@ -137,7 +137,7 @@
err := errors.New("invalid-number-of-args")
return nil, err
}
- pID := &ca.StrType{}
+ pID := &voltha.ID{}
if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
log.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
return nil, err
@@ -149,7 +149,7 @@
return nil, err
}
- log.Debugw("GetPorts", log.Fields{"deviceID": pID.Val, "portype": pt.Val})
+ log.Debugw("GetPorts", log.Fields{"deviceID": pID.Id, "portype": pt.Val})
if rhp.TestMode { // Execute only for test cases
aPort := &voltha.Port{Label: "test_port"}
@@ -167,15 +167,15 @@
err := errors.New("invalid-number-of-args")
return nil, err
}
- pID := &ca.StrType{}
+ pID := &voltha.ID{}
if err := ptypes.UnmarshalAny(args[0].Value, pID); err != nil {
log.Warnw("cannot-unmarshal-ID", log.Fields{"error": err})
return nil, err
}
- log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Val})
+ log.Debugw("GetChildDevice", log.Fields{"deviceId": pID.Id})
if rhp.TestMode { // Execute only for test cases
- return &voltha.Device{Id: pID.Val}, nil
+ return &voltha.Device{Id: pID.Id}, nil
}
//TODO: Complete
return nil, nil
diff --git a/rw_core/core/grpc_nbi_api_handler_client_test.go b/rw_core/core/grpc_nbi_api_handler_client_test.go
index 721271e..6a32ee5 100644
--- a/rw_core/core/grpc_nbi_api_handler_client_test.go
+++ b/rw_core/core/grpc_nbi_api_handler_client_test.go
@@ -36,9 +36,10 @@
var testMode string
/*
-NOTE: These tests require the rw_core to run prior to executing these test cases
+Prerequite: These tests require the rw_core to run prior to executing these test cases.
*/
+
func setup() {
var err error
diff --git a/rw_core/main.go b/rw_core/main.go
index b73f131..25adee6 100644
--- a/rw_core/main.go
+++ b/rw_core/main.go
@@ -45,7 +45,7 @@
}
func init() {
- log.AddPackage(log.JSON, log.WarnLevel, nil)
+ log.AddPackage(log.JSON, log.DebugLevel, nil)
}
func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
diff --git a/tests/kafka/kafka_inter_container_messaging_test.go b/tests/kafka/kafka_inter_container_messaging_test.go
index 1f60cb1..99ba2c4 100644
--- a/tests/kafka/kafka_inter_container_messaging_test.go
+++ b/tests/kafka/kafka_inter_container_messaging_test.go
@@ -29,20 +29,25 @@
"time"
)
+/*
+Prerequite: Start the kafka/zookeeper containers.
+*/
+
var coreKafkaProxy *kk.KafkaMessagingProxy
var adapterKafkaProxy *kk.KafkaMessagingProxy
func init() {
- if _, err := log.SetLogger(log.JSON, 3, log.Fields{"instanceId": "testing"}); err != nil {
- log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
- }
+ log.AddPackage(log.JSON, log.ErrorLevel, nil)
+ log.UpdateAllLoggers(log.Fields{"instanceId": "testing"})
+ log.SetAllLogLevel(log.ErrorLevel)
+
coreKafkaProxy, _ = kk.NewKafkaMessagingProxy(
- kk.KafkaHost("10.176.215.107"),
+ kk.KafkaHost("192.168.0.20"),
kk.KafkaPort(9092),
kk.DefaultTopic(&kk.Topic{Name: "Core"}))
adapterKafkaProxy, _ = kk.NewKafkaMessagingProxy(
- kk.KafkaHost("10.176.215.107"),
+ kk.KafkaHost("192.168.0.20"),
kk.KafkaPort(9092),
kk.DefaultTopic(&kk.Topic{Name: "Adapter"}))
@@ -53,7 +58,7 @@
func subscribeTarget(kmp *kk.KafkaMessagingProxy) {
topic := kk.Topic{Name: "Core"}
- requestProxy := &rhp.RequestHandlerProxy{TestMode: true}
+ requestProxy := &rhp.AdapterRequestHandlerProxy{TestMode: true}
kmp.SubscribeWithTarget(topic, requestProxy)
}
@@ -94,7 +99,7 @@
func TestMultipleSubscribeUnsubscribe(t *testing.T) {
// First subscribe to the specific topic
- log.SetLoglevel(0)
+ //log.SetPackageLogLevel("github.com/opencord/voltha-go/kafka", log.DebugLevel)
var err error
var ch1 <-chan *ca.InterContainerMessage
var ch2 <-chan *ca.InterContainerMessage
@@ -133,7 +138,7 @@
}
func TestIncorrectAPI(t *testing.T) {
- log.SetLoglevel(3)
+ log.SetPackageLogLevel("github.com/opencord/voltha-go/kafka", log.ErrorLevel)
trnsId := uuid.New().String()
protoMsg := &voltha.Device{Id: trnsId}
args := make([]*kk.KVArg, 1)
@@ -181,7 +186,7 @@
func TestGetDevice(t *testing.T) {
trnsId := uuid.New().String()
- protoMsg := &ca.StrType{Val: trnsId}
+ protoMsg := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 1)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -207,7 +212,7 @@
func TestGetDeviceTimeout(t *testing.T) {
trnsId := uuid.New().String()
- protoMsg := &ca.StrType{Val: trnsId}
+ protoMsg := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 1)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -232,7 +237,7 @@
func TestGetChildDevice(t *testing.T) {
trnsId := uuid.New().String()
- protoMsg := &ca.StrType{Val: trnsId}
+ protoMsg := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 1)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -255,7 +260,7 @@
func TestGetChildDevices(t *testing.T) {
trnsId := uuid.New().String()
- protoMsg := &ca.StrType{Val: trnsId}
+ protoMsg := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 1)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -278,7 +283,7 @@
func TestGetPorts(t *testing.T) {
trnsId := uuid.New().String()
- protoArg1 := &ca.StrType{Val: trnsId}
+ protoArg1 := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 2)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -306,7 +311,7 @@
func TestGetPortsMissingArgs(t *testing.T) {
trnsId := uuid.New().String()
- protoArg1 := &ca.StrType{Val: trnsId}
+ protoArg1 := &voltha.ID{Id:trnsId}
args := make([]*kk.KVArg, 1)
args[0] = &kk.KVArg{
Key: "deviceID",
@@ -423,11 +428,6 @@
Key: "childDeviceType",
Value: protoArg3,
}
- protoArg4 := &voltha.Device_ProxyAddress{DeviceId: trnsId, ChannelId: 100}
- args[3] = &kk.KVArg{
- Key: "proxyAddress",
- Value: protoArg4,
- }
rpc := "ChildDeviceDetected"
topic := kk.Topic{Name: "Core"}