Skip to content

Commit

Permalink
refactor and update util to use new log formatter setting (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: brianchennn <ny40ny40ny.cs11@nycu.edu.tw>
  • Loading branch information
brianchennn and brianchennn committed Apr 6, 2023
1 parent 9004d21 commit a63c83d
Show file tree
Hide file tree
Showing 21 changed files with 500 additions and 455 deletions.
83 changes: 46 additions & 37 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,94 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/asaskevich/govalidator"
"github.com/urfave/cli"

"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/util"
"github.com/free5gc/udm/pkg/factory"
"github.com/free5gc/udm/pkg/service"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/util/version"
)

var UDM = &service.UDM{}
var UDM *service.UdmApp

func main() {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.AppLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
logger.MainLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

app := cli.NewApp()
app.Name = "udm"
app.Usage = "5G Unified Data Management (UDM)"
app.Action = action
app.Flags = UDM.GetCliCmd()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
},
cli.StringSliceFlag{
Name: "log, l",
Usage: "Output NF log to `FILE`",
},
}

if err := app.Run(os.Args); err != nil {
fmt.Printf("UDM Run error: %v\n", err)
logger.MainLog.Errorf("UDM Run error: %v\n", err)
}
}

func action(c *cli.Context) error {
if err := initLogFile(c.String("log"), c.String("log5gc")); err != nil {
logger.AppLog.Errorf("%+v", err)
func action(cliCtx *cli.Context) error {
tlsKeyLogPath, err := initLogFile(cliCtx.StringSlice("log"))
if err != nil {
return err
}

if err := UDM.Initialize(c); err != nil {
switch errType := err.(type) {
case govalidator.Errors:
validErrs := err.(govalidator.Errors).Errors()
for _, validErr := range validErrs {
logger.CfgLog.Errorf("%+v", validErr)
}
default:
logger.CfgLog.Errorf("%+v", errType)
}
logger.CfgLog.Errorf("[-- PLEASE REFER TO SAMPLE CONFIG FILE COMMENTS --]")
return fmt.Errorf("Failed to initialize !!")
}
logger.MainLog.Infoln("UDM version: ", version.GetVersion())

logger.AppLog.Infoln(c.App.Name)
logger.AppLog.Infoln("UDM version: ", version.GetVersion())
cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.UdmConfig = cfg
udm, err := service.NewApp(cfg)
if err != nil {
return err
}
UDM = udm

UDM.Start()
udm.Start(tlsKeyLogPath)

return nil
}

func initLogFile(logNfPath, log5gcPath string) error {
UDM.KeyLogPath = util.UdmDefaultKeyLogPath
func initLogFile(logNfPath []string) (string, error) {
logTlsKeyPath := ""

if err := logger.LogFileHook(logNfPath, log5gcPath); err != nil {
return err
}
for _, path := range logNfPath {
if err := logger_util.LogFileHook(logger.Log, path); err != nil {
return "", err
}

if logNfPath != "" {
nfDir, _ := filepath.Split(logNfPath)
if logTlsKeyPath != "" {
continue
}

nfDir, _ := filepath.Split(path)
tmpDir := filepath.Join(nfDir, "key")
if err := os.MkdirAll(tmpDir, 0o775); err != nil {
logger.InitLog.Errorf("Make directory %s failed: %+v", tmpDir, err)
return err
return "", err
}
_, name := filepath.Split(util.UdmDefaultKeyLogPath)
UDM.KeyLogPath = filepath.Join(tmpDir, name)
_, name := filepath.Split(factory.UdmDefaultTLSKeyLogPath)
logTlsKeyPath = filepath.Join(tmpDir, name)
}

return nil
return logTlsKeyPath, nil
}
39 changes: 35 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
module github.com/free5gc/udm

go 1.14
go 1.17

require (
github.com/antihax/optional v1.0.0
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/openapi v1.0.4
github.com/free5gc/util v1.0.3
github.com/free5gc/openapi v1.0.6
github.com/free5gc/util v1.0.5-0.20230306071612-a52909216bd2
github.com/gin-gonic/gin v1.7.7
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli v1.22.5
golang.org/x/crypto v0.1.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
)

0 comments on commit a63c83d

Please sign in to comment.