diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index a8f5158b1..f20b6d9bc 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -8,8 +8,8 @@ "Deps": [ { "ImportPath": "github.com/Sirupsen/logrus", - "Comment": "v0.9.0", - "Rev": "be52937128b38f1d99787bb476c789e2af1147f1" + "Comment": "v0.10.0-16-gcd7d1bb", + "Rev": "cd7d1bbe41066b6c1f19780f895901052150a575" }, { "ImportPath": "github.com/appc/spec/aci", diff --git a/examples/configs/snap-config-sample.json b/examples/configs/snap-config-sample.json index e12c7fcc9..0a1428913 100644 --- a/examples/configs/snap-config-sample.json +++ b/examples/configs/snap-config-sample.json @@ -1,6 +1,7 @@ { "log_level": 2, "log_path": "/some/log/dir", + "log_truncate": false, "gomaxprocs": 2, "control": { "auto_discover_path": "/some/directory/with/plugins", diff --git a/examples/configs/snap-config-sample.yaml b/examples/configs/snap-config-sample.yaml index dedf9f0e4..c8bdbaf9b 100644 --- a/examples/configs/snap-config-sample.yaml +++ b/examples/configs/snap-config-sample.yaml @@ -10,6 +10,11 @@ log_level: 2 # the provided directory. log_path: /some/log/dir +# log_truncate specifies how the log file with be opened +# false => append +# true => truncate +log_truncate: false + # Gomaxprocs sets the number of cores to use on the system # for snapd to use. Default for gomaxprocs is 1 gomaxprocs: 2 diff --git a/snapd.go b/snapd.go index 3806d250b..e68828d1c 100644 --- a/snapd.go +++ b/snapd.go @@ -60,6 +60,10 @@ var ( Usage: "Path for logs. Empty path logs to stdout.", EnvVar: "SNAP_LOG_PATH", } + flLogTruncate = cli.BoolFlag{ + Name: "log-truncate", + Usage: "Log file truncating mode. Default is false => append (true => truncate).", + } flLogLevel = cli.IntFlag{ Name: "log-level, l", Usage: "1-5 (Debug, Info, Warning, Error, Fatal)", @@ -93,10 +97,11 @@ var ( // default configuration values const ( - defaultLogLevel int = 3 - defaultGoMaxProcs int = 1 - defaultLogPath string = "" - defaultConfigPath string = "/etc/snap/snapd.conf" + defaultLogLevel int = 3 + defaultGoMaxProcs int = 1 + defaultLogPath string = "" + defaultLogTruncate bool = false + defaultConfigPath string = "/etc/snap/snapd.conf" ) // holds the configuration passed in through the SNAP config file @@ -104,13 +109,14 @@ const ( // UnmarshalJSON method in this same file needs to be modified to // match the field mapping that is defined here type Config struct { - LogLevel int `json:"log_level"yaml:"log_level"` - GoMaxProcs int `json:"gomaxprocs"yaml:"gomaxprocs"` - LogPath string `json:"log_path"yaml:"log_path"` - Control *control.Config `json:"control"yaml:"control"` - Scheduler *scheduler.Config `json:"scheduler"yaml:"scheduler"` - RestAPI *rest.Config `json:"restapi"yaml:"restapi"` - Tribe *tribe.Config `json:"tribe"yaml:"tribe"` + LogLevel int `json:"log_level,omitempty"yaml:"log_level,omitempty"` + GoMaxProcs int `json:"gomaxprocs,omitempty"yaml:"gomaxprocs,omitempty"` + LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"` + LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"` + Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"` + Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"` + RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"` + Tribe *tribe.Config `json:"tribe,omitempty"yaml:"tribe,omitempty"` } const ( @@ -129,6 +135,10 @@ const ( "description": "path to log file for snapd to use", "type": "string" }, + "log_truncate": { + "description": "truncate log file default is false", + "type": "boolean" + }, "gomaxprocs": { "description": "value to be used for gomaxprocs", "type": "integer", @@ -179,6 +189,7 @@ func main() { app.Flags = []cli.Flag{ flLogLevel, flLogPath, + flLogTruncate, flMaxProcs, flConfig, } @@ -227,8 +238,11 @@ func action(ctx *cli.Context) { if !f.IsDir() { log.Fatal("log path provided must be a directory") } - - file, err := os.OpenFile(fmt.Sprintf("%s/snapd.log", logPath), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + aMode := os.O_APPEND + if cfg.LogTruncate { + aMode = os.O_TRUNC + } + file, err := os.OpenFile(fmt.Sprintf("%s/snapd.log", logPath), os.O_RDWR|os.O_CREATE|aMode, 0666) if err != nil { log.Fatal(err) } @@ -435,13 +449,14 @@ func action(ctx *cli.Context) { // get the default snapd configuration func getDefaultConfig() *Config { return &Config{ - LogLevel: defaultLogLevel, - GoMaxProcs: defaultGoMaxProcs, - LogPath: defaultLogPath, - Control: control.GetDefaultConfig(), - Scheduler: scheduler.GetDefaultConfig(), - RestAPI: rest.GetDefaultConfig(), - Tribe: tribe.GetDefaultConfig(), + LogLevel: defaultLogLevel, + GoMaxProcs: defaultGoMaxProcs, + LogPath: defaultLogPath, + LogTruncate: defaultLogTruncate, + Control: control.GetDefaultConfig(), + Scheduler: scheduler.GetDefaultConfig(), + RestAPI: rest.GetDefaultConfig(), + Tribe: tribe.GetDefaultConfig(), } } @@ -560,6 +575,7 @@ func applyCmdLineFlags(cfg *Config, ctx *cli.Context) { cfg.GoMaxProcs = setIntVal(cfg.GoMaxProcs, ctx, "max-procs") cfg.LogLevel = setIntVal(cfg.LogLevel, ctx, "log-level") cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path") + cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate") // next for the flags related to the control package cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins") cfg.Control.PluginTrust = setIntVal(cfg.Control.PluginTrust, ctx, "plugin-trust") @@ -680,6 +696,10 @@ func (c *Config) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(v, &(c.LogPath)); err != nil { return fmt.Errorf("%v (while parsing 'log_path')", err) } + case "log_truncate": + if err := json.Unmarshal(v, &(c.LogTruncate)); err != nil { + return fmt.Errorf("%v (while parsing 'log_truncate')", err) + } case "control": if err := json.Unmarshal(v, c.Control); err != nil { return err