Skip to content

Commit

Permalink
chore: optimize logging
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie0129 committed Apr 18, 2023
1 parent 610871e commit 6a6fd98
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 36 deletions.
2 changes: 1 addition & 1 deletion conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 3 additions & 30 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions sleepcallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
preSleepLoopDelaySeconds = 180
preSleepLoopDelaySeconds = 300
postSleepLoopDelaySeconds = 120
)

Expand Down Expand Up @@ -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
Expand All @@ -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")
}

Expand All @@ -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
Expand Down

0 comments on commit 6a6fd98

Please sign in to comment.