From 775722c877ef7110cdb4e992f5c216e0e03775c4 Mon Sep 17 00:00:00 2001 From: Mohamed Abukar Date: Mon, 10 Jun 2019 16:41:57 +0300 Subject: [PATCH] Update logging Change-Id: Idffa0b49219bf6bd6ff0bc255e0a4f3bbf6e0959 Signed-off-by: Mohamed Abukar --- go.mod | 3 +++ go.sum | 4 ++-- pkg/xapp/config.go | 5 ++-- pkg/xapp/db.go | 12 +++++----- pkg/xapp/logger.go | 63 ++++++++++++++++----------------------------------- pkg/xapp/rmr.go | 26 ++++++++++++++------- pkg/xapp/xapp.go | 18 +++++++-------- pkg/xapp/xapp_test.go | 24 ++++++++++++++++++++ 8 files changed, 84 insertions(+), 71 deletions(-) diff --git a/go.mod b/go.mod index 33342f0..d7a9c32 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module gerrit.o-ran-sc.org/r/ric-plt/xapp-frame go 1.12 require ( + gerrit.o-ran-sc.org/r/com/golog v0.0.0 gerrit.o-ran-sc.org/r/ric-plt/sdlgo v0.1.1 github.com/BurntSushi/toml v0.3.1 // indirect github.com/fsnotify/fsnotify v1.4.7 @@ -13,3 +14,5 @@ require ( ) replace gerrit.o-ran-sc.org/r/ric-plt/sdlgo => gerrit.o-ran-sc.org/r/ric-plt/sdlgo.git v0.1.1 + +replace gerrit.o-ran-sc.org/r/com/golog => gerrit.o-ran-sc.org/r/com/golog.git v0.0.0-20190604083303-aaffc8ebe3f1 diff --git a/go.sum b/go.sum index 2d25621..6772fc3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +gerrit.o-ran-sc.org/r/com/golog.git v0.0.0-20190604083303-aaffc8ebe3f1 h1:WAB8G/FFaUjRkpLuUhIo0EfbeBOY7xjkcFAFkpe3Xns= +gerrit.o-ran-sc.org/r/com/golog.git v0.0.0-20190604083303-aaffc8ebe3f1/go.mod h1:b8YB31U8/4iRpABioeSzGi/YMzOQ/Zq7hrJmmXKqlJk= gerrit.o-ran-sc.org/r/ric-plt/sdlgo.git v0.1.1 h1:D2fU0/YXdqSNYsmptSBbkDfG76uBFKjnhQiq5cD4WT4= gerrit.o-ran-sc.org/r/ric-plt/sdlgo.git v0.1.1/go.mod h1:2Y8gw2jqj9urI8VFqFQn7BX0J3A852+YrXVV9V8gOt4= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= @@ -81,7 +83,6 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -94,7 +95,6 @@ gitlabe1.ext.net.nokia.com/ric_dev/ue-nib v0.0.0-20190531121637-5379da45e235 h1: gitlabe1.ext.net.nokia.com/ric_dev/ue-nib v0.0.0-20190531121637-5379da45e235/go.mod h1:rhiDbAhxaCJouoZfj0+vSoYUWM2t9i1EdR0MpewgTYo= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 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 @@ import ( "github.com/fsnotify/fsnotify" "github.com/spf13/viper" "os" + "path/filepath" ) type Configurator struct { @@ -41,8 +42,8 @@ func parseCmd() string { 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 @@ var SDLCounterOpts = []CounterOpts{ } 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 @@ type RNIBClient struct { // 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 -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 (Log) SetLevel(level int) { - l := C.mdclog_severity_t(level) - C.mdclog_level_set(l) +func NewLogger(name string) *Log { + l, _ := mdclog.InitLogger(name) + return &Log{ + logger: l, + } } -func (Log) SetMdc(key string, value string) { - C.mdclog_mdc_add(C.CString(key), C.CString(value)) +func (l *Log) SetLevel(level int) { + l.logger.LevelSet(mdclog.Level(level)) } -func (Log) Fatal(pattern string, args ...interface{}) { - WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...)) - log.Panic("Fatal error occured, exiting ...") +func (l *Log) SetMdc(key string, value string) { + l.logger.MdcAdd(key, value) } -func (Log) Error(pattern string, args ...interface{}) { - WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...)) +func (l *Log) Error(pattern string, args ...interface{}) { + l.logger.Error(pattern, args...) } -func (Log) Warn(pattern string, args ...interface{}) { - WriteLog(LogLvlWarn, fmt.Sprintf(pattern, args...)) +func (l *Log) Warn(pattern string, args ...interface{}) { + l.logger.Warning(pattern, args...) } -func (Log) Info(pattern string, args ...interface{}) { - WriteLog(LogLvlInfo, fmt.Sprintf(pattern, args...)) +func (l *Log) Info(pattern string, args ...interface{}) { + l.logger.Info(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 @@ func NewRMRClient() *RMRClient { 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) parseMessage(rxBuffer *C.rmr_mbuf_t) { 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 @@ func (m *RMRClient) IsReady() bool { 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 @@ var ( 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 @@ func Run(c MessageConsumer) { 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 TestSubscribeChannels(t *testing.T) { } } +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() -- 2.16.6