Create function that tracks critical system errors and issues them only once.

#692
pull/609/merge
cyoung 2018-01-09 09:57:00 -05:00
rodzic 31b3e83ea6
commit df6f844738
1 zmienionych plików z 20 dodań i 0 usunięć

Wyświetl plik

@ -1172,6 +1172,21 @@ func addSystemError(err error) {
globalStatus.Errors = append(globalStatus.Errors, err.Error())
}
var systemErrsMutex *sync.Mutex
var systemErrs map[string]string
func addSingleSystemErrorf(ident string, format string, a ...interface{}) {
systemErrsMutex.Lock()
if _, ok := systemErrs[ident]; !ok {
// Error hasn't been thrown yet.
systemErrs[ident] = fmt.Sprintf(format, a...)
globalStatus.Errors = append(globalStatus.Errors, systemErrs[ident])
log.Printf("Added critical system error: %s\n", systemErrs[ident])
}
// Do nothing on this call if the error has already been thrown.
systemErrsMutex.Unlock()
}
func saveSettings() {
fd, err := os.OpenFile(configLocation, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0644))
if err != nil {
@ -1385,6 +1400,11 @@ func main() {
mySituation.muBaro = &sync.Mutex{}
mySituation.muSatellite = &sync.Mutex{}
// Set up system error tracking.
systemErrsMutex = &sync.Mutex{}
systemErrs = make(map[string]string)
// Set up status.
globalStatus.Version = stratuxVersion
globalStatus.Build = stratuxBuild