Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sync): fix ipversion_prefer; add logfile; support binding to wg #76

Merged
merged 10 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ daed
# vendor
vendor/
go-mod/

# log
*.log
28 changes: 24 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/natefinch/lumberjack.v2"
)

func init() {
runCmd.PersistentFlags().StringVarP(&cfgDir, "config", "c", filepath.Join("/etc", db.AppName), "config directory")
runCmd.PersistentFlags().StringVarP(&listen, "listen", "l", "0.0.0.0:2023", "listening address")
runCmd.PersistentFlags().BoolVar(&apiOnly, "api-only", false, "run graphql backend without dae")
runCmd.PersistentFlags().StringVar(&logFile, "logfile", "", "Log file to write. Empty means writing to stdout and stderr.")
runCmd.PersistentFlags().IntVar(&logFileMaxSize, "logfile-maxsize", 30, "Unit: MB. The maximum size in megabytes of the log file before it gets rotated.")
runCmd.PersistentFlags().IntVar(&logFileMaxBackups, "logfile-maxbackups", 3, "The maximum number of old log files to retain.")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
}

Expand All @@ -47,10 +51,13 @@ func errorExit(err error) {
}

var (
cfgDir string
disableTimestamp bool
listen string
apiOnly bool
cfgDir string
logFile string
logFileMaxSize int
logFileMaxBackups int
disableTimestamp bool
listen string
apiOnly bool

runCmd = &cobra.Command{
Use: "run",
Expand All @@ -74,6 +81,19 @@ var (
}

// Run dae.
var logOpts *lumberjack.Logger
if logFile != "" {
logOpts = &lumberjack.Logger{
Filename: logFile,
MaxSize: logFileMaxSize,
MaxAge: 0,
MaxBackups: logFileMaxBackups,
LocalTime: true,
Compress: true,
}
logrus.SetOutput(logOpts)
db.SetOutput(logOpts)
}
go func() {
if err := dae.Run(
logrus.StandardLogger(),
Expand Down
3 changes: 2 additions & 1 deletion dae/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ loop:
newConf := newReloadMsg.Config
/* dae-wing end */
// New logger.
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp)
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp, nil)
logrus.SetLevel(log.Level)
log.SetOutput(logrus.StandardLogger().Out)

// New control plane.
obj := c.EjectBpf()
Expand Down
14 changes: 13 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"

"github.com/daeuniverse/dae-wing/pkg/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

const (
Expand All @@ -27,7 +31,7 @@ var (
func InitDatabase(configDir string) (err error) {
path := filepath.Join(configDir, filename)
db, err = gorm.Open(sqlite.Open(path), &gorm.Config{
//Logger: logger.Default.LogMode(logger.Info),
// Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
return fmt.Errorf("%w: %v", err, path)
Expand Down Expand Up @@ -60,6 +64,14 @@ func InitDatabase(configDir string) (err error) {
func DB(ctx context.Context) *gorm.DB {
return db.WithContext(ctx)
}
func SetOutput(writer io.Writer) {
db.Logger = logger.New(log.New(writer, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Warn,
IgnoreRecordNotFoundError: false,
Colorful: false,
})
}
func BeginTx(ctx context.Context) *gorm.DB {
return DB(ctx).Begin(&sql.TxOptions{
Isolation: sql.LevelSerializable,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down