feat: add and remove logger

This commit is contained in:
Markus Pesch 2019-03-04 20:52:41 +01:00
parent d988735817
commit 88f6116a41
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
7 changed files with 207 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"time" "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/remote"
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor" "git.cryptic.systems/fh-trier/go-flucky/cmd/sensor"
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature" "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") rootCmd.PersistentFlags().StringVar(&cfg, "config", "/etc/flucky/config.json", "Config file")
lg.InitCmd(rootCmd, cfg)
// humidity.InitCmd(rootCmd, configDir) // humidity.InitCmd(rootCmd, configDir)
remote.InitCmd(rootCmd, cfg) remote.InitCmd(rootCmd, cfg)
sensor.InitCmd(rootCmd, cfg) sensor.InitCmd(rootCmd, cfg)

54
cmd/logger/add.go Normal file
View File

@ -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)
}

18
cmd/logger/logger.go Normal file
View File

@ -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)
}

40
cmd/logger/rm.go Normal file
View File

@ -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)
}

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"git.cryptic.systems/fh-trier/go-flucky/pkg/cli" "git.cryptic.systems/fh-trier/go-flucky/pkg/cli"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -15,13 +15,22 @@ type FileLogger struct {
writer io.Writer 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) 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) { func (fl *FileLogger) GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error) {
return nil, nil return nil, nil
} }
func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error) { func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error) {
cachedTemperatures := []*types.Temperature{} 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) return nil, fmt.Errorf("Can not decode temperatures from logfile %v: %v", logfile, err)
} }
// sort all redundant values out cachedTemperatures = append(cachedTemperatures, loggedTemperatures...)
for _, loggedTemperature := range loggedTemperatures {
found := false // // sort all redundant values out
for _, cachedTemperature := range cachedTemperatures { // for _, loggedTemperature := range loggedTemperatures {
if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID { // found := false
found = true // for _, cachedTemperature := range cachedTemperatures {
break // if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID {
} // found = true
} // break
if !found { // }
cachedTemperatures = append(cachedTemperatures, loggedTemperature) // }
} // if !found {
} // cachedTemperatures = append(cachedTemperatures, loggedTemperature)
// }
// }
} }
// // sort out all temperatures which are not mesured by sensor id // // 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 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 { func NewFileLogger(w io.Writer) *FileLogger {
return &FileLogger{ return &FileLogger{
writer: w, writer: w,

View File

@ -1,6 +1,7 @@
package logger package logger
import ( import (
"fmt"
"time" "time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/types"
@ -26,3 +27,47 @@ const (
LogHumidity LogValue = "humidity" LogHumidity LogValue = "humidity"
LogTemperature = "temperature" 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)
}
}