Skip to content

Commit

Permalink
feat: optimize logging
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie0129 committed Apr 16, 2023
1 parent cfaac30 commit 393f670
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ var (
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.Now().Sub(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
}

Expand Down

0 comments on commit 393f670

Please sign in to comment.