Update logging

Change-Id: Idffa0b49219bf6bd6ff0bc255e0a4f3bbf6e0959
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
diff --git a/pkg/xapp/config.go b/pkg/xapp/config.go
index c63e32c..647b3eb 100755
--- a/pkg/xapp/config.go
+++ b/pkg/xapp/config.go
@@ -24,6 +24,7 @@
 	"github.com/fsnotify/fsnotify"
 	"github.com/spf13/viper"
 	"os"
+	"path/filepath"
 )
 
 type Configurator struct {
@@ -41,8 +42,8 @@
 	return *fileName
 }
 
-func LoadConfig() (l Log) {
-	l = Log{}
+func LoadConfig() (l *Log) {
+	l = NewLogger(filepath.Base(os.Args[0]))
 	viper.SetConfigFile(parseCmd())
 
 	if err := viper.ReadInConfig(); err != nil {
diff --git a/pkg/xapp/db.go b/pkg/xapp/db.go
index 36fbfc1..f2286b4 100755
--- a/pkg/xapp/db.go
+++ b/pkg/xapp/db.go
@@ -34,10 +34,10 @@
 }
 
 type SDLClient struct {
-	db   	*sdl.SdlInstance
-	stat 	map[string]Counter
-	mux  	sync.Mutex
-	ready 	bool
+	db    *sdl.SdlInstance
+	stat  map[string]Counter
+	mux   sync.Mutex
+	ready bool
 }
 
 type RNIBClient struct {
@@ -47,8 +47,8 @@
 // NewSDLClient returns a new SDLClient.
 func NewSDLClient(ns string) *SDLClient {
 	return &SDLClient{
-		db: sdl.NewSdlInstance(ns, sdl.NewDatabase()),
-		stat: Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
+		db:    sdl.NewSdlInstance(ns, sdl.NewDatabase()),
+		stat:  Metric.RegisterCounterGroup(SDLCounterOpts, "SDL"),
 		ready: false,
 	}
 }
diff --git a/pkg/xapp/logger.go b/pkg/xapp/logger.go
index a73cb36..484df2c 100755
--- a/pkg/xapp/logger.go
+++ b/pkg/xapp/logger.go
@@ -19,66 +19,41 @@
 
 package xapp
 
-/*
-#cgo CFLAGS: -I/usr/local/include
-#cgo LDFLAGS: -lmdclog
-#
-#include <mdclog/mdclog.h>
-void xAppMgr_mdclog_write(mdclog_severity_t severity, const char *msg) {
-     mdclog_write(severity, "%s", msg);
-}
-*/
-import "C"
-
 import (
-	"fmt"
-	"log"
-	"time"
+	mdclog "gerrit.o-ran-sc.org/r/com/golog"
 )
 
 type Log struct {
+	logger *mdclog.MdcLogger
 }
 
-const (
-	LogLvlErr   = C.MDCLOG_ERR
-	LogLvlWarn  = C.MDCLOG_WARN
-	LogLvlInfo  = C.MDCLOG_INFO
-	LogLvlDebug = C.MDCLOG_DEBUG
-)
-
-func WriteLog(lvl C.mdclog_severity_t, msg string) {
-	t := time.Now().Format("2019-01-02 15:04:05")
-	text := fmt.Sprintf("%s:: %s ", t, msg)
-
-	C.xAppMgr_mdclog_write(lvl, C.CString(text))
+func NewLogger(name string) *Log {
+	l, _ := mdclog.InitLogger(name)
+	return &Log{
+		logger: l,
+	}
 }
 
-func (Log) SetLevel(level int) {
-	l := C.mdclog_severity_t(level)
-	C.mdclog_level_set(l)
+func (l *Log) SetLevel(level int) {
+	l.logger.LevelSet(mdclog.Level(level))
 }
 
-func (Log) SetMdc(key string, value string) {
-	C.mdclog_mdc_add(C.CString(key), C.CString(value))
+func (l *Log) SetMdc(key string, value string) {
+	l.logger.MdcAdd(key, value)
 }
 
-func (Log) Fatal(pattern string, args ...interface{}) {
-	WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...))
-	log.Panic("Fatal error occured, exiting ...")
+func (l *Log) Error(pattern string, args ...interface{}) {
+	l.logger.Error(pattern, args...)
 }
 
-func (Log) Error(pattern string, args ...interface{}) {
-	WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...))
+func (l *Log) Warn(pattern string, args ...interface{}) {
+	l.logger.Warning(pattern, args...)
 }
 
-func (Log) Warn(pattern string, args ...interface{}) {
-	WriteLog(LogLvlWarn, fmt.Sprintf(pattern, args...))
+func (l *Log) Info(pattern string, args ...interface{}) {
+	l.logger.Info(pattern, args...)
 }
 
-func (Log) Info(pattern string, args ...interface{}) {
-	WriteLog(LogLvlInfo, fmt.Sprintf(pattern, args...))
-}
-
-func (Log) Debug(pattern string, args ...interface{}) {
-	WriteLog(LogLvlDebug, fmt.Sprintf(pattern, args...))
+func (l *Log) Debug(pattern string, args ...interface{}) {
+	l.logger.Debug(pattern, args...)
 }
diff --git a/pkg/xapp/rmr.go b/pkg/xapp/rmr.go
index e910ec1..a4955d1 100755
--- a/pkg/xapp/rmr.go
+++ b/pkg/xapp/rmr.go
@@ -75,13 +75,13 @@
 
 	ctx := C.rmr_init(p, m, C.int(0))
 	if ctx == nil {
-		Logger.Fatal("rmrClient: Initializing RMR context failed, bailing out!")
+		Logger.Error("rmrClient: Initializing RMR context failed, bailing out!")
 	}
 
 	return &RMRClient{
-		context: ctx,
+		context:   ctx,
 		consumers: make([]MessageConsumer, 0),
-		stat: Metric.RegisterCounterGroup(RMRCounterOpts, "RMR"),
+		stat:      Metric.RegisterCounterGroup(RMRCounterOpts, "RMR"),
 	}
 }
 
@@ -148,7 +148,7 @@
 func (m *RMRClient) Allocate() *C.rmr_mbuf_t {
 	buf := C.rmr_alloc_msg(m.context, 0)
 	if buf == nil {
-		Logger.Fatal("rmrClient: Allocating message buffer failed!")
+		Logger.Error("rmrClient: Allocating message buffer failed!")
 	}
 
 	return buf
@@ -216,10 +216,20 @@
 	return m.ready != 0
 }
 
-func (m *RMRClient) GetRicMessageId(mid string) int {
-	return RICMessageTypes[mid]
-}
-
 func (m *RMRClient) SetReadyCB(cb ReadyCB) {
 	m.readyCb = cb
 }
+
+func (m *RMRClient) GetRicMessageId(name string) (int, bool) {
+	id, ok := RICMessageTypes[name]
+	return id, ok
+}
+
+func (m *RMRClient) GetRicMessageName(id int) (s string) {
+	for k, v := range RICMessageTypes {
+		if id == v {
+			return k
+		}
+	}
+	return
+}
diff --git a/pkg/xapp/xapp.go b/pkg/xapp/xapp.go
index 78df93e..c51e0ac 100755
--- a/pkg/xapp/xapp.go
+++ b/pkg/xapp/xapp.go
@@ -34,10 +34,18 @@
 	Rnib     *RNIBClient
 	Resource *Router
 	Metric   *Metrics
-	Logger   Log
+	Logger   *Log
 	Config   Configurator
 )
 
+func IsReady() bool {
+	return Rmr.IsReady() && Sdl.IsReady()
+}
+
+func SetReadyCB(cb ReadyCB) {
+	Rmr.SetReadyCB(cb)
+}
+
 func init() {
 	// Load xapp configuration
 	Logger = LoadConfig()
@@ -69,11 +77,3 @@
 	Sdl.TestConnection()
 	Rmr.Start(c)
 }
-
-func IsReady() bool {
-	return Rmr.IsReady() && Sdl.IsReady()
-}
-
-func SetReadyCB(cb ReadyCB) {
-	Rmr.SetReadyCB(cb)
-}
\ No newline at end of file
diff --git a/pkg/xapp/xapp_test.go b/pkg/xapp/xapp_test.go
index 51ac171..06345af 100755
--- a/pkg/xapp/xapp_test.go
+++ b/pkg/xapp/xapp_test.go
@@ -159,6 +159,30 @@
 	}
 }
 
+func TestGetRicMessageSuccess(t *testing.T) {
+	id, ok := Rmr.GetRicMessageId("RIC_SUB_REQ")
+	if !ok || id != 12010 {
+		t.Errorf("Error: GetRicMessageId failed: id=%d", id)
+	}
+
+	name := Rmr.GetRicMessageName(12010)
+	if name !=  "RIC_SUB_REQ" {
+		t.Errorf("Error: GetRicMessageName failed: name=%s", name)
+	}
+}
+
+func TestGetRicMessageFails(t *testing.T) {
+	id, ok := Rmr.GetRicMessageId("INVALID")
+	if ok {
+		t.Errorf("Error: GetRicMessageId returned invalid value id=%d", id)
+	}
+
+	name := Rmr.GetRicMessageName(123456)
+	if name !=  "" {
+		t.Errorf("Error: GetRicMessageName returned invalid value: name=%s", name)
+	}
+}
+
 func TestTeardown(t *testing.T) {
 	Sdl.Clear()
 	Rnib.Clear()