-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and update util to use new log formatter setting
- Loading branch information
1 parent
e777e4e
commit 4fd029a
Showing
37 changed files
with
4,016 additions
and
2,750 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime/debug" | ||
|
||
"github.com/asaskevich/govalidator" | ||
"github.com/urfave/cli" | ||
|
||
"github.com/free5gc/n3iwf/internal/logger" | ||
"github.com/free5gc/n3iwf/pkg/factory" | ||
"github.com/free5gc/n3iwf/pkg/service" | ||
logger_util "github.com/free5gc/util/logger" | ||
"github.com/free5gc/util/version" | ||
) | ||
|
||
var N3IWF = &service.N3IWF{} | ||
var N3IWF *service.N3iwfApp | ||
|
||
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 = "n3iwf" | ||
app.Usage = "Non-3GPP Interworking Function (N3IWF)" | ||
app.Action = action | ||
app.Flags = N3IWF.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 { | ||
logger.AppLog.Errorf("N3IWF Run Error: %v\n", err) | ||
logger.MainLog.Errorf("N3IWF 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 := N3IWF.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("N3IWF version: ", version.GetVersion()) | ||
|
||
cfg, err := factory.ReadConfig(cliCtx.String("config")) | ||
if err != nil { | ||
return err | ||
} | ||
factory.N3iwfConfig = cfg | ||
|
||
logger.AppLog.Infoln(c.App.Name) | ||
logger.AppLog.Infoln("N3IWF version: ", version.GetVersion()) | ||
n3iwf, err := service.NewApp(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
N3IWF = n3iwf | ||
|
||
N3IWF.Start() | ||
n3iwf.Start(tlsKeyLogPath) | ||
|
||
return nil | ||
} | ||
|
||
func initLogFile(logNfPath, log5gcPath string) error { | ||
if err := logger.LogFileHook(logNfPath, log5gcPath); err != nil { | ||
return err | ||
func initLogFile(logNfPath []string) (string, error) { | ||
logTlsKeyPath := "" | ||
|
||
for _, path := range logNfPath { | ||
if err := logger_util.LogFileHook(logger.Log, path); err != nil { | ||
return "", err | ||
} | ||
|
||
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 | ||
} | ||
_, name := filepath.Split(factory.N3iwfDefaultTLSKeyLogPath) | ||
logTlsKeyPath = filepath.Join(tmpDir, name) | ||
} | ||
return nil | ||
|
||
return logTlsKeyPath, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.