mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-26 15:29:04 +01:00
Add logfile command line option
This commit is contained in:
parent
4ee4e617b2
commit
47510099cf
@ -59,10 +59,31 @@ func LoadConfiguration(file string, config *GlobalConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadCli() string {
|
func ReadCli() map[string]string {
|
||||||
|
var m map[string]string
|
||||||
cfg := flag.String("config", "./config.json", "Path to configuration file")
|
cfg := flag.String("config", "./config.json", "Path to configuration file")
|
||||||
|
logfile := flag.String("log", "stderr", "Path for logfile")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
return *cfg
|
m = make(map[string]string)
|
||||||
|
m["configfile"] = *cfg
|
||||||
|
m["logfile"] = *logfile
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetLogging(logfile string) error {
|
||||||
|
var file *os.File
|
||||||
|
var err error
|
||||||
|
if (logfile != "stderr") {
|
||||||
|
file, err = os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
file = os.Stderr
|
||||||
|
}
|
||||||
|
log.SetOutput(file)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register an interrupt handler for Ctrl+C and similar. At signal,
|
// Register an interrupt handler for Ctrl+C and similar. At signal,
|
||||||
@ -94,12 +115,17 @@ func main() {
|
|||||||
log.Print(err)
|
log.Print(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
configfile := ReadCli()
|
clicfg := ReadCli()
|
||||||
|
err = SetLogging(clicfg["logfile"])
|
||||||
|
if (err != nil) {
|
||||||
|
log.Print("Error setting up logging system to ", clicfg["logfile"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Load and check configuration
|
// Load and check configuration
|
||||||
err = LoadConfiguration(configfile, &config)
|
err = LoadConfiguration(clicfg["configfile"], &config)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
log.Print("Error reading configuration file ", configfile)
|
log.Print("Error reading configuration file ", clicfg["configfile"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if config.Interval <= 0 || time.Duration(config.Interval)*time.Second <= 0 {
|
if config.Interval <= 0 || time.Duration(config.Interval)*time.Second <= 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user