mirror of
https://github.com/prashantgupta24/automatic-mouse-mover.git
synced 2024-12-22 16:34:11 +00:00
added logfile option
This commit is contained in:
parent
0c525b8243
commit
708f4623e5
@ -1,9 +1,12 @@
|
|||||||
package mousemover
|
package mousemover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/go-vgo/robotgo"
|
"github.com/go-vgo/robotgo"
|
||||||
@ -17,10 +20,12 @@ type MouseMover struct {
|
|||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
runningStatus bool
|
runningStatus bool
|
||||||
|
logFile *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
timeout = 100 //ms
|
timeout = 100 //ms
|
||||||
|
logFileName = "logFile"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Start the main app
|
//Start the main app
|
||||||
@ -31,7 +36,7 @@ func (m *MouseMover) Start() {
|
|||||||
m.quit = make(chan struct{})
|
m.quit = make(chan struct{})
|
||||||
|
|
||||||
heartbeatInterval := 60 //value always in seconds
|
heartbeatInterval := 60 //value always in seconds
|
||||||
workerInterval := 15
|
workerInterval := 10
|
||||||
|
|
||||||
activityTracker := &tracker.Instance{
|
activityTracker := &tracker.Instance{
|
||||||
HeartbeatInterval: heartbeatInterval,
|
HeartbeatInterval: heartbeatInterval,
|
||||||
@ -42,8 +47,11 @@ func (m *MouseMover) Start() {
|
|||||||
heartbeatCh := activityTracker.Start()
|
heartbeatCh := activityTracker.Start()
|
||||||
|
|
||||||
go func(m *MouseMover) {
|
go func(m *MouseMover) {
|
||||||
|
logger := getLogger(m, false) //set writeToFile=true only for debugging
|
||||||
m.updateRunningStatus(true)
|
m.updateRunningStatus(true)
|
||||||
movePixel := 10
|
movePixel := 10
|
||||||
|
var lastMoved time.Time
|
||||||
|
didNotMoveTimes := 0
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case heartbeat := <-heartbeatCh:
|
case heartbeat := <-heartbeatCh:
|
||||||
@ -53,29 +61,36 @@ func (m *MouseMover) Start() {
|
|||||||
select {
|
select {
|
||||||
case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
|
case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
|
||||||
if wasMouseMoveSuccess {
|
if wasMouseMoveSuccess {
|
||||||
log.Infof("moved mouse at : %v\n\n", time.Now())
|
lastMoved = time.Now()
|
||||||
|
logger.Infof("moved mouse at : %v\n\n", lastMoved)
|
||||||
movePixel *= -1
|
movePixel *= -1
|
||||||
|
didNotMoveTimes = 0
|
||||||
} else {
|
} else {
|
||||||
msg := "Mouse pointer cannot be moved. See README for details."
|
didNotMoveTimes++
|
||||||
log.Errorf(msg)
|
msg := fmt.Sprintf("Mouse pointer cannot be moved at %v. Last moved at %v. Happened %v times. See README for details.",
|
||||||
|
time.Now(), lastMoved, didNotMoveTimes)
|
||||||
|
logger.Errorf(msg)
|
||||||
|
if didNotMoveTimes >= 3 {
|
||||||
|
go func() {
|
||||||
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg)
|
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case <-time.After(timeout * time.Millisecond):
|
case <-time.After(timeout * time.Millisecond):
|
||||||
//timeout, do nothing
|
//timeout, do nothing
|
||||||
log.Errorf("timeout happened after %vms while trying to move mouse", timeout)
|
logger.Errorf("timeout happened after %vms while trying to move mouse", timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else { //uncomment to see all activities received
|
||||||
//uncomment just for reference
|
// logger.Printf("activity detected in the last %v seconds.", int(heartbeatInterval))
|
||||||
// log.Printf("activity detected in the last %v seconds.", int(heartbeatInterval))
|
// logger.Printf("Activity type:\n")
|
||||||
// log.Printf("Activity type:\n")
|
|
||||||
// for activityType, times := range heartbeat.ActivityMap {
|
// for activityType, times := range heartbeat.ActivityMap {
|
||||||
// log.Printf("activityType : %v times: %v\n", activityType, len(times))
|
// logger.Printf("activityType : %v times: %v\n", activityType, len(times))
|
||||||
// }
|
// }
|
||||||
// log.Printf("\n\n\n")
|
// logger.Printf("\n\n\n")
|
||||||
}
|
}
|
||||||
case <-m.quit:
|
case <-m.quit:
|
||||||
log.Infof("stopping mouse mover")
|
logger.Infof("stopping mouse mover")
|
||||||
m.updateRunningStatus(false)
|
m.updateRunningStatus(false)
|
||||||
activityTracker.Quit()
|
activityTracker.Quit()
|
||||||
return
|
return
|
||||||
@ -117,6 +132,7 @@ func (m *MouseMover) Quit() {
|
|||||||
if m != nil && m.isRunning() {
|
if m != nil && m.isRunning() {
|
||||||
m.quit <- struct{}{}
|
m.quit <- struct{}{}
|
||||||
}
|
}
|
||||||
|
m.logFile.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetInstance gets the singleton instance for mouse mover app
|
//GetInstance gets the singleton instance for mouse mover app
|
||||||
@ -126,3 +142,21 @@ func GetInstance() *MouseMover {
|
|||||||
}
|
}
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger {
|
||||||
|
logger := log.New()
|
||||||
|
logger.Formatter = &logrus.TextFormatter{
|
||||||
|
FullTimestamp: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if doWriteToFile {
|
||||||
|
logFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error opening file: %v", err)
|
||||||
|
}
|
||||||
|
logger.SetOutput(logFile)
|
||||||
|
m.logFile = logFile
|
||||||
|
}
|
||||||
|
|
||||||
|
return logger
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user