Skip to content
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
39 changes: 39 additions & 0 deletions internal/configmgr/configmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package configmgr defines AdGuard Home on-disk configuration entities.
package configmgr

import (
"github.com/AdguardTeam/golibs/container"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/validate"
)

// Config is the top-level on-disk configuration structure.
//
// TODO(d.kolyshev): Use.
type Config struct {
// Log is a block with log configuration settings.
Log *LogConfig `yaml:"log"`
}

// type check
var _ validate.Interface = (*Config)(nil)

// Validate implements the [validate.Interface] interface for *Config.
func (c *Config) Validate() (err error) {
if c == nil {
return errors.ErrNoValue
}

// Keep this in the same order as the fields in the config.
validators := container.KeyValues[string, validate.Interface]{{
Key: "log",
Value: c.Log,
}}

var errs []error
for _, kv := range validators {
errs = validate.Append(errs, kv.Key, kv.Value)
}

return errors.Join(errs...)
}
53 changes: 53 additions & 0 deletions internal/configmgr/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package configmgr

import (
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/validate"
)

// LogConfig is the on-disk logging configuration.
type LogConfig struct {
// File is the path to the log file. If empty, logs are written to stdout.
// If "syslog", logs are written to syslog.
File string `yaml:"file"`

// MaxAge is the maximum duration for retaining old log files, in days.
MaxAge int `yaml:"max_age"`

// MaxBackups is the maximum number of old log files to retain.
//
// NOTE: MaxAge may still cause them to get deleted.
MaxBackups int `yaml:"max_backups"`

// MaxSize is the maximum size of the log file before it gets rotated, in
// megabytes.
MaxSize int `yaml:"max_size"`

// Compress determines, if the rotated log files should be compressed using
// gzip.
Compress bool `yaml:"compress"`

// Enabled indicates whether logging is enabled.
Enabled bool `yaml:"enabled"`

// LocalTime determines, if the time used for formatting the timestamps in
// is the computer's local time.
LocalTime bool `yaml:"local_time"`

// Verbose determines, if verbose (aka debug) logging is enabled.
Verbose bool `yaml:"verbose"`
}

// type check
var _ validate.Interface = (*LogConfig)(nil)

// Validate implements the [validate.Interface] interface for *LogConfig.
//
// TODO(d.kolyshev): Add more validations.
func (c *LogConfig) Validate() (err error) {
if c == nil {
return errors.ErrNoValue
}

return nil
}
48 changes: 4 additions & 44 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/aghtls"
"github.com/AdguardTeam/AdGuardHome/internal/configmgr"
"github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
Expand Down Expand Up @@ -42,39 +43,6 @@ const (
userFilterDataDir = "userfilters"
)

// logSettings are the logging settings part of the configuration file.
type logSettings struct {
// Enabled indicates whether logging is enabled.
Enabled bool `yaml:"enabled"`

// File is the path to the log file. If empty, logs are written to stdout.
// If "syslog", logs are written to syslog.
File string `yaml:"file"`

// MaxBackups is the maximum number of old log files to retain.
//
// NOTE: MaxAge may still cause them to get deleted.
MaxBackups int `yaml:"max_backups"`

// MaxSize is the maximum size of the log file before it gets rotated, in
// megabytes. The default value is 100 MB.
MaxSize int `yaml:"max_size"`

// MaxAge is the maximum duration for retaining old log files, in days.
MaxAge int `yaml:"max_age"`

// Compress determines, if the rotated log files should be compressed using
// gzip.
Compress bool `yaml:"compress"`

// LocalTime determines, if the time used for formatting the timestamps in
// is the computer's local time.
LocalTime bool `yaml:"local_time"`

// Verbose determines, if verbose (aka debug) logging is enabled.
Verbose bool `yaml:"verbose"`
}

// osConfig contains OS-related configuration.
type osConfig struct {
// Group is the name of the group which AdGuard Home must switch to on
Expand Down Expand Up @@ -109,6 +77,8 @@ type clientSourcesConfig struct {
//
// Field ordering is important, YAML fields better not to be reordered, if it's
// not absolutely necessary.
//
// TODO(d.kolyshev): Use [configmgr.Config].
type configuration struct {
// Raw file data to avoid re-reading of configuration file
// It's reset after config is parsed
Expand Down Expand Up @@ -158,7 +128,7 @@ type configuration struct {
Clients *clientsConfig `yaml:"clients"`

// Log is a block with log configuration settings.
Log logSettings `yaml:"log"`
Log *configmgr.LogConfig `yaml:"log"`

OSConfig *osConfig `yaml:"os"`

Expand Down Expand Up @@ -576,16 +546,6 @@ var config = &configuration{
HostsFile: true,
},
},
Log: logSettings{
Enabled: true,
File: "",
MaxBackups: 0,
MaxSize: 100,
MaxAge: 3,
Compress: false,
LocalTime: false,
Verbose: false,
},
OSConfig: &osConfig{},
SchemaVersion: configmigrate.LastSchemaVersion,
Theme: ThemeAuto,
Expand Down
2 changes: 1 addition & 1 deletion internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Main(clientBuildFS fs.FS) {

confPath := initConfigFilename(ctx, l, opts, workDir)

ls := getLogSettings(ctx, l, opts, workDir, confPath)
ls := newLogSettings(ctx, l, opts, workDir, confPath)

// TODO(a.garipov): Use slog everywhere.
baseLogger := newSlogLogger(ls)
Expand Down
Loading
Loading