Chore: put log in engine

This commit is contained in:
xjasonlyu
2022-03-27 14:19:36 +08:00
parent 7df522a91f
commit 620d5ac834
2 changed files with 13 additions and 15 deletions

View File

@@ -17,13 +17,17 @@ import (
var _engine = &engine{} var _engine = &engine{}
// Start starts the default engine up. // Start starts the default engine up.
func Start() error { func Start() {
return _engine.start() if err := _engine.start(); err != nil {
log.Fatalf("[ENGINE] failed to start: %v", err)
}
} }
// Stop shuts the default engine down. // Stop shuts the default engine down.
func Stop() error { func Stop() {
return _engine.stop() if err := _engine.stop(); err != nil {
log.Fatalf("[ENGINE] failed to stop: %v", err)
}
} }
// Insert loads *Key to the default engine. // Insert loads *Key to the default engine.
@@ -120,7 +124,7 @@ func (e *engine) applyStats() error {
go func() { go func() {
if err := stats.Start(addr.String(), e.Token); err != nil { if err := stats.Start(addr.String(), e.Token); err != nil {
log.Warnf("[STATS] stats start error: %v", err) log.Warnf("[STATS] failed to start: %v", err)
} }
}() }()
log.Infof("[STATS] serve at: http://%s", addr) log.Infof("[STATS] serve at: http://%s", addr)

14
main.go
View File

@@ -49,23 +49,17 @@ func main() {
if configFile != "" { if configFile != "" {
data, err := os.ReadFile(configFile) data, err := os.ReadFile(configFile)
if err != nil { if err != nil {
log.Fatalf("Failed to read config %s: %v", configFile, err) log.Fatalf("Failed to read config file '%s': %v", configFile, err)
} }
if err = yaml.Unmarshal(data, key); err != nil { if err = yaml.Unmarshal(data, key); err != nil {
log.Fatalf("Failed to unmarshal config %s: %v", configFile, err) log.Fatalf("Failed to unmarshal config file '%s': %v", configFile, err)
} }
} }
engine.Insert(key) engine.Insert(key)
assert := func(msg string, f func() error) { engine.Start()
if err := f(); err != nil { defer engine.Stop()
log.Fatalf("Failed to %s: %v", msg, err)
}
}
assert("start engine", engine.Start)
defer assert("stop engine", engine.Stop)
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)