From 88f6116a41d1822ac08f83e7cfd4e9049adc13db Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Mon, 4 Mar 2019 20:52:41 +0100 Subject: [PATCH] feat: add and remove logger --- cmd/cmd.go | 2 ++ cmd/logger/add.go | 54 ++++++++++++++++++++++++++++++++++ cmd/logger/logger.go | 18 ++++++++++++ cmd/logger/rm.go | 40 +++++++++++++++++++++++++ cmd/temperature/log.go | 1 - pkg/logger/file_logger.go | 62 ++++++++++++++++++++++++++++++--------- pkg/logger/logger.go | 45 ++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+), 15 deletions(-) create mode 100644 cmd/logger/add.go create mode 100644 cmd/logger/logger.go create mode 100644 cmd/logger/rm.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 90b5b15..dbc3385 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -5,6 +5,7 @@ import ( "os" "time" + lg "git.cryptic.systems/fh-trier/go-flucky/cmd/logger" "git.cryptic.systems/fh-trier/go-flucky/cmd/remote" "git.cryptic.systems/fh-trier/go-flucky/cmd/sensor" "git.cryptic.systems/fh-trier/go-flucky/cmd/temperature" @@ -61,6 +62,7 @@ func Execute(version string) { rootCmd.PersistentFlags().StringVar(&cfg, "config", "/etc/flucky/config.json", "Config file") + lg.InitCmd(rootCmd, cfg) // humidity.InitCmd(rootCmd, configDir) remote.InitCmd(rootCmd, cfg) sensor.InitCmd(rootCmd, cfg) diff --git a/cmd/logger/add.go b/cmd/logger/add.go new file mode 100644 index 0000000..777061b --- /dev/null +++ b/cmd/logger/add.go @@ -0,0 +1,54 @@ +package logger + +import ( + "log" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/logger" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" + "github.com/spf13/cobra" +) + +var enabled bool + +var addLoggerCmd = &cobra.Command{ + Use: "add", + Short: "Add internal logger", + Args: cobra.ExactArgs(2), + Aliases: []string{"append"}, + Example: "flucky logger add temperature /var/log/flucky/temperature.log", + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + fc, err := config.Read(cfg) + if err != nil { + log.Fatalln(err) + } + + // loggertype, err := logger.StringToLoggerType(args[0]) + // if err != nil { + // log.Fatalln(err) + // } + + logvalue, err := logger.StringToLogValue(args[0]) + if err != nil { + log.Fatalln(err) + } + + err = fc.AddLogFile(args[1], logvalue) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(fc, cfg) + if err != nil { + log.Fatalln(err) + } + + }, +} + +func init() { + loggerCmd.AddCommand(addLoggerCmd) +} diff --git a/cmd/logger/logger.go b/cmd/logger/logger.go new file mode 100644 index 0000000..e422882 --- /dev/null +++ b/cmd/logger/logger.go @@ -0,0 +1,18 @@ +package logger + +import ( + "github.com/spf13/cobra" +) + +var cfg string + +var loggerCmd = &cobra.Command{ + Use: "logger", + Short: "Manage internal logger", +} + +func InitCmd(cmd *cobra.Command, config string) { + cfg = config + + cmd.AddCommand(loggerCmd) +} diff --git a/cmd/logger/rm.go b/cmd/logger/rm.go new file mode 100644 index 0000000..0ceb7ab --- /dev/null +++ b/cmd/logger/rm.go @@ -0,0 +1,40 @@ +package logger + +import ( + "log" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" + "github.com/spf13/cobra" +) + +var rmLoggerCmd = &cobra.Command{ + Use: "rm", + Short: "Remove internal logger", + Args: cobra.ExactArgs(1), + Aliases: []string{"remove"}, + Example: "flucky logger rm /var/log/flucky/temperature.log", + Run: func(cmd *cobra.Command, args []string) { + + // read configuration + fc, err := config.Read(cfg) + if err != nil { + log.Fatalln(err) + } + + err = fc.RemoveLogFile(args[0]) + if err != nil { + log.Fatalln(err) + } + + // save new configuration + err = config.Write(fc, cfg) + if err != nil { + log.Fatalln(err) + } + + }, +} + +func init() { + loggerCmd.AddCommand(rmLoggerCmd) +} diff --git a/cmd/temperature/log.go b/cmd/temperature/log.go index da23d26..852f681 100644 --- a/cmd/temperature/log.go +++ b/cmd/temperature/log.go @@ -6,7 +6,6 @@ import ( "os" "git.cryptic.systems/fh-trier/go-flucky/pkg/cli" - "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "github.com/spf13/cobra" ) diff --git a/pkg/logger/file_logger.go b/pkg/logger/file_logger.go index 1d951a1..42c2dbd 100644 --- a/pkg/logger/file_logger.go +++ b/pkg/logger/file_logger.go @@ -15,13 +15,22 @@ type FileLogger struct { writer io.Writer } -func (fl *FileLogger) AddLogFile(logfile string, logValue LogValue) { +func (fl *FileLogger) AddLogFile(logfile string, logValue LogValue) error { + for _, logfiles := range fl.LogFiles { + for _, lf := range logfiles { + if lf == logfile { + return fmt.Errorf("Logfile %v already configured", logfile) + } + } + } fl.LogFiles[logValue] = append(fl.LogFiles[logValue], logfile) + return nil } func (fl *FileLogger) GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error) { return nil, nil } + func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error) { cachedTemperatures := []*types.Temperature{} @@ -47,19 +56,21 @@ func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorID return nil, fmt.Errorf("Can not decode temperatures from logfile %v: %v", logfile, err) } - // sort all redundant values out - for _, loggedTemperature := range loggedTemperatures { - found := false - for _, cachedTemperature := range cachedTemperatures { - if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID { - found = true - break - } - } - if !found { - cachedTemperatures = append(cachedTemperatures, loggedTemperature) - } - } + cachedTemperatures = append(cachedTemperatures, loggedTemperatures...) + + // // sort all redundant values out + // for _, loggedTemperature := range loggedTemperatures { + // found := false + // for _, cachedTemperature := range cachedTemperatures { + // if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID { + // found = true + // break + // } + // } + // if !found { + // cachedTemperatures = append(cachedTemperatures, loggedTemperature) + // } + // } } // // sort out all temperatures which are not mesured by sensor id @@ -181,6 +192,29 @@ func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) error { return nil } +func (fl *FileLogger) RemoveLogFile(logfile string) error { + + for i, logfiles := range fl.LogFiles { + cachedLogfiles := []string{} + for _, lf := range logfiles { + if lf == logfile { + continue + } + cachedLogfiles = append(cachedLogfiles, lf) + } + fl.LogFiles[i] = cachedLogfiles + } + + if _, err := os.Stat(logfile); err == nil { + err := os.Remove(logfile) + if err != nil { + return fmt.Errorf("Can not remote file %v: %v", logfile, err) + } + } + + return nil +} + func NewFileLogger(w io.Writer) *FileLogger { return &FileLogger{ writer: w, diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 46e12ed..97bb889 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "fmt" "time" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" @@ -26,3 +27,47 @@ const ( LogHumidity LogValue = "humidity" LogTemperature = "temperature" ) + +// LogTypeToString converts a logtype into a string +func LogTypeToString(loggertype LoggerType) (string, error) { + switch loggertype { + case LogFile: + return "file", nil + default: + return "", fmt.Errorf("Can not determine loggertype %v", loggertype) + } +} + +// LogValueToString converts a logvalue into a string +func LogValueToString(logvalue LogValue) (string, error) { + switch logvalue { + case LogHumidity: + return "humidity", nil + case LogTemperature: + return "temperature", nil + default: + return "", fmt.Errorf("Can not determine logvalue %v", logvalue) + } +} + +// StringToLoggerType converts a string into loggertype +func StringToLoggerType(loggertype string) (LoggerType, error) { + switch loggertype { + case "file": + return LogFile, nil + default: + return "", fmt.Errorf("Can not determine loggertype %v", loggertype) + } +} + +// StringToLogValue converts a string into logvalue +func StringToLogValue(logvalue string) (LogValue, error) { + switch logvalue { + case "humidity": + return LogHumidity, nil + case "temperature": + return LogTemperature, nil + default: + return "", fmt.Errorf("Can not determine logvalue %v", logvalue) + } +}