Skip to content

Commit

Permalink
Replace logrus with zap logger (#232)
Browse files Browse the repository at this point in the history
Signed-off-by: Arrobo, Gabriel <[email protected]>
  • Loading branch information
gab-arrobo authored Oct 3, 2024
1 parent 927ed1e commit fff34f3
Show file tree
Hide file tree
Showing 41 changed files with 401 additions and 475 deletions.
20 changes: 0 additions & 20 deletions .fossa.yml

This file was deleted.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.6-dev
1.5.0
17 changes: 0 additions & 17 deletions common/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
package common

import (
"bytes"
"fmt"
"testing"

"github.com/omec-project/gnbsim/logger"
"github.com/stretchr/testify/assert"
)

func TestString_ValidEventTypeReturnsEventTypeString(t *testing.T) {
Expand All @@ -25,16 +21,3 @@ func TestString_ValidEventTypeReturnsEventTypeString(t *testing.T) {
)
}
}

func TestString_InvalidEventTypeExitsWithLogMsg(t *testing.T) {
var INVALID_EVENT EventType = 0x0
var logBuf bytes.Buffer
assert := assert.New(t)
patchedExit := func(int) { panic("Dummy exit function") }

logger.AppLog.Logger.SetOutput(&logBuf)
logger.AppLog.Logger.ExitFunc = patchedExit

assert.Panics(func() { _ = INVALID_EVENT.String() })
assert.Contains(logBuf.String(), "Invalid Event ID: 0x0")
}
8 changes: 4 additions & 4 deletions config/gnbsim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ configuration:
plmnId:
mcc: 208 # Mobile Country Code (3 digits string, digit: 0~9)
mnc: 93 # Mobile Network Code (2 or 3 digits string, digit: 0~9)
gNbId:
gNbId:
bitLength: 24
gNBValue: 000102 # gNB identifier (3 bytes hex string, range: 000000~FFFFFF)
supportedTaList:
Expand All @@ -54,7 +54,7 @@ configuration:
profileType: custom # profile type
profileName: custom1 # uniqely identifies a profile within application
enable: false # Set true to execute the profile, false otherwise.
execInParallel: false #run all subscribers in parallel
execInParallel: false #run all subscribers in parallel
stepTrigger: true #wait for trigger to move to next step
gnbName: gnb1 # gNB to be used for this profile
startImsi: 208930100007487
Expand Down Expand Up @@ -159,7 +159,7 @@ configuration:
execInParallel: false #run all subscribers within profile in parallel
plmnId: # Public Land Mobile Network ID, <PLMN ID> = <MCC><MNC>. Should match startImsi
mcc: 208 # Mobile Country Code (3 digits string, digit: 0~9)
mnc: 93 # Mobile Network Code (2 or 3 digits string, digit: 0~9)
mnc: 93 # Mobile Network Code (2 or 3 digits string, digit: 0~9)
- profileType: deregister # profile type
profileName: profile5 # uniqely identifies a profile within application
enable: false # Set true to execute the profile, false otherwise.
Expand Down Expand Up @@ -235,4 +235,4 @@ configuration:
mnc: 93 # Mobile Network Code (2 or 3 digits string, digit: 0~9)

logger:
logLevel: info # how detailed the log will be, values: trace, debug, info, warn, error, fatal, panic
logLevel: info # how detailed the log will be, values: debug, info, warn, error, fatal, panic
6 changes: 3 additions & 3 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ var AppConfig *Config
func InitConfigFactory(f string) error {
content, err := os.ReadFile(f)
if err != nil {
logger.CfgLog.Errorln("Failed to read", f, "file:", err)
logger.CfgLog.Errorln("failed to read", f, "file:", err)
return err
}

AppConfig = &Config{}

err = yaml.Unmarshal(content, AppConfig)
if err != nil {
logger.CfgLog.Errorln("Failed to unmarshal:", err)
logger.CfgLog.Errorln("failed to unmarshal:", err)
return err
}

err = AppConfig.Validate()
if err != nil {
logger.CfgLog.Errorln("Invalid Configuration:", err)
logger.CfgLog.Errorln("invalid Configuration:", err)
}

return err
Expand Down
28 changes: 16 additions & 12 deletions gnbsim.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
profctx "github.com/omec-project/gnbsim/profile/context"
"github.com/omec-project/gnbsim/stats"
"github.com/urfave/cli"
"go.uber.org/zap/zapcore"
)

func main() {
Expand All @@ -37,21 +38,21 @@ func main() {
logger.AppLog.Infoln("App Name:", app.Name)

if err := app.Run(os.Args); err != nil {
logger.AppLog.Errorln("Failed to run GNBSIM:", err)
logger.AppLog.Errorln("failed to run GNBSIM:", err)
return
}
}

func action(c *cli.Context) error {
cfg := c.String("cfg")
if cfg == "" {
logger.AppLog.Warnln("No configuration file provided. Using default configuration file:", factory.GNBSIM_DEFAULT_CONFIG_PATH)
logger.AppLog.Infoln("Application Usage:", c.App.Usage)
logger.AppLog.Warnln("no configuration file provided. Using default configuration file:", factory.GNBSIM_DEFAULT_CONFIG_PATH)
logger.AppLog.Infoln("application usage:", c.App.Usage)
cfg = factory.GNBSIM_DEFAULT_CONFIG_PATH
}

if err := factory.InitConfigFactory(cfg); err != nil {
logger.AppLog.Errorln("Failed to initialize config factory:", err)
logger.AppLog.Errorln("failed to initialize config factory:", err)
return err
}

Expand All @@ -61,26 +62,29 @@ func action(c *cli.Context) error {
if config.Configuration.GoProfile.Enable {
go func() {
endpt := fmt.Sprintf(":%v", config.Configuration.GoProfile.Port)
fmt.Println("endpoint for profile server ", endpt)
logger.AppLog.Infoln("endpoint for profile server", endpt)
err := http.ListenAndServe(endpt, nil)
if err != nil {
logger.AppLog.Errorln("Failed to start profiling server")
logger.AppLog.Errorln("failed to start profiling server")
}
}()
}
lvl := config.Logger.LogLevel
logger.AppLog.Infoln("Setting log level to:", lvl)
lvl, errLevel := zapcore.ParseLevel(config.Logger.LogLevel)
if errLevel != nil {
logger.AppLog.Errorln("can not parse input level")
}
logger.AppLog.Infoln("setting log level to:", lvl)
logger.SetLogLevel(lvl)

err := prof.InitializeAllProfiles()
if err != nil {
logger.AppLog.Errorln("Failed to initialize Profiles:", err)
logger.AppLog.Errorln("failed to initialize Profiles:", err)
return err
}

err = gnodeb.InitializeAllGnbs()
if err != nil {
logger.AppLog.Errorln("Failed to initialize gNodeBs:", err)
logger.AppLog.Errorln("failed to initialize gNodeBs:", err)
return err
}

Expand Down Expand Up @@ -159,7 +163,7 @@ func getCliFlags() []cli.Flag {
}
}

// TODO : we don't keep track of how many profiles are started...
// TODO: we don't keep track of how many profiles are started...
func ListenAndLogSummary() {
for intfcMsg := range profctx.SummaryChan {
// TODO: do we need this event ?
Expand All @@ -171,7 +175,7 @@ func ListenAndLogSummary() {
// Waiting for execution summary from profile routine
msg, ok := intfcMsg.(*common.SummaryMessage)
if !ok {
logger.AppLog.Fatalln("Invalid Message Type")
logger.AppLog.Fatalln("invalid Message Type")
}

logger.AppSummaryLog.Infoln("Profile Name:", msg.ProfileName, ", Profile Type:", msg.ProfileType)
Expand Down
14 changes: 4 additions & 10 deletions gnodeb/context/gnbamf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/omec-project/amf/factory"
"github.com/omec-project/gnbsim/logger"
"github.com/omec-project/openapi/models"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

const NGAP_SCTP_PORT int = 38412
Expand All @@ -25,7 +25,7 @@ type GnbAmf struct {
/*Socket Connection*/
Conn net.Conn

Log *logrus.Entry
Log *zap.SugaredLogger

ServedGuamiList []models.Guami
PlmnSupportList []factory.PlmnSupportItem
Expand All @@ -41,18 +41,12 @@ func NewGnbAmf(ip string, port int) *GnbAmf {
gnbAmf := &GnbAmf{}
gnbAmf.AmfIp = ip
gnbAmf.AmfPort = port
gnbAmf.Log = logger.GNodeBLog.WithFields(logrus.Fields{
"subcategory": "GnbAmf",
logger.FieldIp: gnbAmf.AmfIp,
})
gnbAmf.Log = logger.GNodeBLog.With("subcategory", "GnbAmf", logger.FieldIp, gnbAmf.AmfIp)
return gnbAmf
}

func (amf *GnbAmf) Init() {
amf.Log = logger.GNodeBLog.WithFields(logrus.Fields{
"subcategory": "GnbAmf",
logger.FieldIp: amf.AmfIp,
})
amf.Log = logger.GNodeBLog.With("subcategory", "GnbAmf", logger.FieldIp, amf.AmfIp)
}

func (amf *GnbAmf) GetIpAddr() string {
Expand Down
17 changes: 7 additions & 10 deletions gnodeb/context/gnbcpue.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

"github.com/omec-project/gnbsim/common"
"github.com/omec-project/gnbsim/logger"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

type GnbCpUe struct {
Supi string
Amf *GnbAmf
Gnb *GNodeB
Log *logrus.Entry
Log *zap.SugaredLogger

// GnbCpUe writes messages to UE on this channel
WriteUeChan chan common.InterfaceMessage
Expand All @@ -40,17 +40,14 @@ func NewGnbCpUe(ngapId int64, gnb *GNodeB, amf *GnbAmf) *GnbCpUe {
gnbue.Amf = amf
gnbue.Gnb = gnb
gnbue.ReadChan = make(chan common.InterfaceMessage, 5)
gnbue.Log = logger.GNodeBLog.WithFields(logrus.Fields{
"subcategory": "GnbCpUe",
logger.FieldGnbUeNgapId: ngapId,
})
gnbue.Log.Traceln("Context Created")
gnbue.Log = logger.GNodeBLog.With("subcategory", "GnbCpUe", logger.FieldGnbUeNgapId, ngapId)
gnbue.Log.Debugln("context created")
return &gnbue
}

// GetGnbUpUe returns the GnbUpUe instance corresponding to provided PDU Sess ID
func (ctx *GnbCpUe) GetGnbUpUe(pduSessId int64) (*GnbUpUe, error) {
ctx.Log.Infoln("Fetching GnbUpUe for pduSessId:", pduSessId)
ctx.Log.Infoln("fetching GnbUpUe for pduSessId:", pduSessId)
val, ok := ctx.GnbUpUes.Load(pduSessId)
if ok {
return val.(*GnbUpUe), nil
Expand All @@ -61,13 +58,13 @@ func (ctx *GnbCpUe) GetGnbUpUe(pduSessId int64) (*GnbUpUe, error) {

// AddGnbUpUe adds the GnbUpUe instance corresponding to provided PDU Sess ID
func (ctx *GnbCpUe) AddGnbUpUe(pduSessId int64, gnbue *GnbUpUe) {
ctx.Log.Infoln("Adding new GnbUpUe for PDU Sess ID:", pduSessId)
ctx.Log.Infoln("adding new GnbUpUe for PDU Sess ID:", pduSessId)
ctx.GnbUpUes.Store(pduSessId, gnbue)
}

// RemoveGnbUpUe removes the GnbUpUe instance corresponding to provided PDU
// sess ID from the map
func (ctx *GnbCpUe) RemoveGnbUpUe(pduSessId int64) {
ctx.Log.Infoln("Deleting GnbUpUe for pduSessId:", pduSessId)
ctx.Log.Infoln("deleting GnbUpUe for pduSessId:", pduSessId)
ctx.GnbUpUes.Delete(pduSessId)
}
10 changes: 5 additions & 5 deletions gnodeb/context/gnbpeerdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"sync"

"github.com/omec-project/gnbsim/logger"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

// GnbPeerDao acts as a Data Access Object that stores and provides access to all
// the GnbUpf and GnbAmf instances
type GnbPeerDao struct {
Log *logrus.Entry
Log *zap.SugaredLogger

// Map of UPF IP address vs GnbUpf Context. Not considering Port as they
// will be same for all UPFs i.e GTP-U port 2152
Expand All @@ -27,13 +27,13 @@ type GnbPeerDao struct {

func NewGnbPeerDao() *GnbPeerDao {
dao := &GnbPeerDao{}
dao.Log = logger.GNodeBLog.WithFields(logrus.Fields{"subcategory": "GnbPeerDao"})
dao.Log = logger.GNodeBLog.With("subcategory", "GnbPeerDao")
return dao
}

// GetGnbUpf returns the GnbUpf instance corresponding to provided IP
func (dao *GnbPeerDao) GetGnbUpf(ip string) *GnbUpf {
dao.Log.Infoln("Fetching GnbUpf corresponding to IP:", ip)
dao.Log.Infoln("fetching GnbUpf corresponding to IP:", ip)
val, ok := dao.gnbUpfMap.Load(ip)
if ok {
return val.(*GnbUpf)
Expand Down Expand Up @@ -62,6 +62,6 @@ func (dao *GnbPeerDao) GetOrAddGnbUpf(ip string) (*GnbUpf, bool) {

// AddGnbUpf adds a GnbUpf instance corresponding to the IP into the map
func (dao *GnbPeerDao) AddGnbUpf(ip string, gnbupf *GnbUpf) {
dao.Log.Infoln("Adding new GnbUpf corresponding to IP:", ip)
dao.Log.Infoln("adding new GnbUpf corresponding to IP:", ip)
dao.gnbUpfMap.Store(ip, gnbupf)
}
Loading

0 comments on commit fff34f3

Please sign in to comment.