From 6a6fd98e30c9b4edef29f3166c891440298f502c Mon Sep 17 00:00:00 2001 From: Charlie Chiang Date: Tue, 18 Apr 2023 19:43:06 +0800 Subject: [PATCH] chore: optimize logging --- conf.go | 2 +- loop.go | 33 +++------------------------------ sleepcallback.go | 9 ++++----- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/conf.go b/conf.go index 1eb73b6..e529964 100644 --- a/conf.go +++ b/conf.go @@ -43,7 +43,7 @@ func saveConfig() error { func loadConfig() error { // Check if config file exists if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) { - logrus.Warnf("config file %s does not exist, using default config %#v", configPath, defaultConfig) + logrus.Infof("config file %s does not exist, using default config %#v", configPath, defaultConfig) config = defaultConfig err := saveConfig() if err != nil { diff --git a/loop.go b/loop.go index f30c329..c838f32 100644 --- a/loop.go +++ b/loop.go @@ -11,13 +11,14 @@ var ( maintainedChargingInProgress = false maintainLoopLock = &sync.Mutex{} // mg is used to skip several loops when system woke up or before sleep - wg = &sync.WaitGroup{} + wg = &sync.WaitGroup{} + loopInterval = time.Duration(20) * time.Second ) func loop() { for { maintainLoop() - time.Sleep(time.Duration(20) * time.Second) + time.Sleep(loopInterval) } } @@ -96,35 +97,7 @@ func maintainLoop() bool { return true } -var ( - lastBatteryCharge = -1 - lastLimit = -1 - lastIsChargingEnabled = false - lastIsPluggedIn = false - lastMaintainedChargingInProgress = false - lastTriedPrintTime = time.Now() -) - func printStatus(batteryCharge int, limit int, isChargingEnabled bool, isPluggedIn bool, maintainedChargingInProgress bool) { - lastTriedPrintTime = time.Now() - - if batteryCharge == lastBatteryCharge && - limit == lastLimit && - isChargingEnabled == lastIsChargingEnabled && - isPluggedIn == lastIsPluggedIn && - maintainedChargingInProgress == lastMaintainedChargingInProgress && // All values are the same as last time - time.Since(lastTriedPrintTime) < time.Second*30 && // And it's been less than 30 seconds since last tried print - !logrus.IsLevelEnabled(logrus.TraceLevel) { // Trace level is not enabled. If trace level is enabled, we want to print the status every time. - // So we don't want to print the status every time. - return - } - - lastBatteryCharge = batteryCharge - lastLimit = limit - lastIsChargingEnabled = isChargingEnabled - lastIsPluggedIn = isPluggedIn - lastMaintainedChargingInProgress = maintainedChargingInProgress - logrus.Debugf("batteryCharge=%d, limit=%d, chargingEnabled=%t, isPluggedIn=%t, maintainedChargingInProgress=%t", batteryCharge, limit, diff --git a/sleepcallback.go b/sleepcallback.go index 2ace27a..8d27354 100644 --- a/sleepcallback.go +++ b/sleepcallback.go @@ -14,7 +14,7 @@ import ( ) var ( - preSleepLoopDelaySeconds = 180 + preSleepLoopDelaySeconds = 300 postSleepLoopDelaySeconds = 120 ) @@ -74,11 +74,10 @@ func systemWillSleepCallback() { // By always disabling charging before sleep (if charge limit is enabled), we can prevent // some rare cases. if config.Limit < 100 { - logrus.Info("system is going to sleep, charge limit is enabled, disabling charging just before sleep") + logrus.Infof("system is going to sleep but charge limit is enabled, disabling charging just before sleep, and delaying next loop by %d seconds", preSleepLoopDelaySeconds) // Delay next loop to prevent charging to be re-enabled after we disabled it. // macOS will wait 30s before going to sleep, so we delay more than that, just to be sure. // No need to prevent duplicated runs. - logrus.Debugf("delaying next loop by %d seconds", preSleepLoopDelaySeconds) wg.Add(1) go func() { // Use sleep instead of time.After because when the computer sleeps, we @@ -100,7 +99,7 @@ func systemWillSleepCallback() { //export systemWillPowerOnCallback func systemWillPowerOnCallback() { - // System has started the wake up process... + // System has started the wake-up process... logrus.Traceln("received kIOMessageSystemWillPowerOn notification") } @@ -124,7 +123,7 @@ func systemHasPoweredOnCallback() { // Use sleep instead of time.After or time.Sleep because when the computer sleeps, we // actually want the sleep to prolong as well. func sleep(seconds int) { - tl := 250 + tl := 250 // ms t := time.NewTicker(time.Duration(tl) * time.Millisecond) ticksWanted := seconds * 1000 / tl ticksElapsed := 0