New config field LogTarget. Simplified function backend.LogError.

This commit is contained in:
Kleissner
2022-01-12 22:18:34 +01:00
parent 5c8b8eb94b
commit 6a5312d834
17 changed files with 83 additions and 65 deletions

View File

@@ -8,6 +8,7 @@ package core
import (
_ "embed" // Required for embedding default Config file
"fmt"
"io/ioutil"
"log"
"os"
@@ -29,6 +30,9 @@ type Config struct {
SearchIndex string `yaml:"SearchIndex"` // Local search index of blockchain records. Empty to disable.
GeoIPDatabase string `yaml:"GeoIPDatabase"` // GeoLite2 City database to provide GeoIP information.
// Target for the log messages: 0 = Log file, 1 = Stdout, 2 = Log file + Stdout, 3 = None
LogTarget int `yaml:"LogTarget"`
// Listen settings
Listen []string `yaml:"Listen"` // IP:Port combinations
ListenWorkers int `yaml:"ListenWorkers"` // Count of workers to process incoming raw packets. Default 2.
@@ -102,7 +106,7 @@ func SaveConfig(Filename string, Config interface{}) (err error) {
// SaveConfig stores the current runtime config to file. Any foreign settings not present in the Config structure will be deleted.
func (backend *Backend) SaveConfig() {
if err := SaveConfig(backend.ConfigFilename, *backend.Config); err != nil {
backend.Filters.LogError("SaveConfig", "writing config '%s': %v\n", backend.ConfigFilename, err.Error())
backend.LogError("SaveConfig", "writing config '%s': %v\n", backend.ConfigFilename, err.Error())
}
}
@@ -139,3 +143,23 @@ func (backend *Backend) initLog() (err error) {
return nil
}
// Logs an error message.
func (backend *Backend) LogError(function, format string, v ...interface{}) {
switch backend.Config.LogTarget {
case 0:
log.Printf("["+function+"] "+format, v...)
case 1:
fmt.Fprintf(backend.Stdout, "["+function+"] "+format, v...)
case 2:
log.Printf("["+function+"] "+format, v...)
fmt.Fprintf(backend.Stdout, "["+function+"] "+format, v...)
case 3: // None
}
backend.Filters.LogError(function, format, v)
}