Skip to content

Commit

Permalink
Merge pull request #6 from prashantgupta24/dev
Browse files Browse the repository at this point in the history
improve logging
  • Loading branch information
prashantgupta24 authored Mar 22, 2019
2 parents 7330e5e + b881774 commit d60ddb4
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 64 deletions.
19 changes: 12 additions & 7 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import (
"fmt"
"time"

"github.com/prashantgupta24/activity-tracker/internal/pkg/logging"
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
)

func main() {
fmt.Println("starting activity tracker")

logger := logging.New()

logger.Infof("starting activity tracker")

frequency := 5 //value always in seconds

activityTracker := &tracker.Instance{
Frequency: frequency,
LogLevel: logging.Info,
}

//This starts the tracker for all services
Expand All @@ -28,17 +33,17 @@ func main() {
select {
case heartbeat := <-heartbeatCh:
if !heartbeat.IsActivity {
fmt.Printf("no activity detected in the last %v seconds\n\n", int(frequency))
logger.Infof("no activity detected in the last %v seconds\n\n\n", int(frequency))
} else {
fmt.Printf("activity detected in the last %v seconds. ", int(frequency))
fmt.Printf("Activity type:\n")
logger.Infof("activity detected in the last %v seconds.", int(frequency))
logger.Infof("Activity type:\n")
for activity, time := range heartbeat.Activity {
fmt.Printf("%v ---> %v\n", activity.ActivityType, time)
logger.Infof("%v ---> %v\n", activity.ActivityType, time)
}
fmt.Println()
fmt.Printf("\n\n\n")
}
case <-timeToKill.C:
fmt.Println("time to kill app")
logger.Infof("time to kill app")
activityTracker.Quit()
return
}
Expand Down
20 changes: 14 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ package: github.com/prashantgupta24/activity-tracker
import:
- package: github.com/go-vgo/robotgo
version: ^0.80.0
- package: github.com/sirupsen/logrus
version: ~1.4.0
75 changes: 75 additions & 0 deletions internal/pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package logging

import (
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

const (
JSONFormat = "json"
TextFormat = "text"

Info = "info"
Debug = "debug"
)

func New() *log.Logger {
logger := log.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
logger.SetLevel(logrus.InfoLevel)
return logger
}

func NewLoggerLevel(logLevel string) *log.Logger {
logger := log.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}

switch {
case logLevel == "debug":
logger.SetLevel(logrus.DebugLevel)
default:
logger.SetLevel(logrus.InfoLevel)
}
return logger
}

func NewLoggerFormat(format string) *log.Logger {
logger := log.New()

switch {
case format == "json":
logger.Formatter = &logrus.JSONFormatter{}
default:
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
}

logger.SetLevel(logrus.InfoLevel)
return logger
}

func NewLoggerLevelFormat(logLevel string, format string) *log.Logger {
logger := log.New()
switch {
case logLevel == "debug":
logger.SetLevel(logrus.DebugLevel)
default:
logger.SetLevel(logrus.InfoLevel)
}

switch {
case format == "json":
logger.Formatter = &logrus.JSONFormatter{}
default:
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
}

return logger
}
34 changes: 19 additions & 15 deletions internal/pkg/service/mouseClickHandler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package service

import (
"log"
"time"
log "github.com/sirupsen/logrus"

"github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/activity"
Expand All @@ -12,30 +11,33 @@ type mouseClickHandler struct {
tickerCh chan struct{}
}

func (m *mouseClickHandler) Start(activityCh chan *activity.Type) {
func (m *mouseClickHandler) Start(logger *log.Logger, activityCh chan *activity.Type) {
m.tickerCh = make(chan struct{})
registrationFree := make(chan struct{})

go func() {
go addMouseClickRegistration(activityCh, registrationFree) //run once before first check
go func(logger *log.Logger) {
handlerLogger := logger.WithFields(log.Fields{
"method": "mouse-click-handler",
})
go addMouseClickRegistration(handlerLogger, activityCh, registrationFree) //run once before first check
for range m.tickerCh {
log.Printf("mouse clicker checked at : %v\n", time.Now())
handlerLogger.Debugln("mouse clicker checked")
select {
case _, ok := <-registrationFree:
if ok {
//log.Printf("registration free for mouse click \n")
go addMouseClickRegistration(activityCh, registrationFree)
handlerLogger.Debugf("registration free \n")
go addMouseClickRegistration(handlerLogger, activityCh, registrationFree)
} else {
//log.Printf("error : channel closed \n")
handlerLogger.Errorf("error : channel closed \n")
return
}
default:
//log.Printf("registration is busy for mouse click handler, do nothing\n")
handlerLogger.Debugf("registration is busy, do nothing\n")
}
}
log.Printf("stopping click handler")
handlerLogger.Infof("stopping click handler")
return
}()
}(logger)
}

func MouseClickHandler() *mouseClickHandler {
Expand All @@ -54,11 +56,13 @@ func (m *mouseClickHandler) Close() {
close(m.tickerCh)
}

func addMouseClickRegistration(activityCh chan *activity.Type, registrationFree chan struct{}) {
log.Printf("adding reg \n")
func addMouseClickRegistration(logger *log.Entry, activityCh chan *activity.Type,
registrationFree chan struct{}) {

logger.Debugf("adding mouse left click registration \n")
mleft := robotgo.AddEvent("mleft")
if mleft {
//log.Printf("mleft clicked \n")
logger.Debugf("mleft clicked \n")
activityCh <- &activity.Type{
ActivityType: activity.MOUSE_LEFT_CLICK,
}
Expand Down
30 changes: 19 additions & 11 deletions internal/pkg/service/mouseCursorHandler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package service

import (
"log"
"time"

log "github.com/sirupsen/logrus"

"github.com/prashantgupta24/activity-tracker/internal/pkg/mouse"
"github.com/prashantgupta24/activity-tracker/pkg/activity"
)
Expand All @@ -17,16 +18,19 @@ type cursorInfo struct {
currentMousePos *mouse.Position
}

func (m *mouseCursorHandler) Start(activityCh chan *activity.Type) {
func (m *mouseCursorHandler) Start(logger *log.Logger, activityCh chan *activity.Type) {

m.tickerCh = make(chan struct{})

go func() {
go func(logger *log.Logger) {
handlerLogger := logger.WithFields(log.Fields{
"method": "mouse-cursor-handler",
})
lastMousePos := mouse.GetPosition()
for range m.tickerCh {
log.Printf("mouse cursor checked at : %v\n", time.Now())
handlerLogger.Debugf("mouse cursor checked")
commCh := make(chan *cursorInfo)
go checkCursorChange(commCh, lastMousePos)
go checkCursorChange(handlerLogger, commCh, lastMousePos)
select {
case cursorInfo := <-commCh:
if cursorInfo.didCursorMove {
Expand All @@ -37,12 +41,12 @@ func (m *mouseCursorHandler) Start(activityCh chan *activity.Type) {
}
case <-time.After(timeout * time.Millisecond):
//timeout, do nothing
log.Printf("timeout happened after %vms while checking mouse cursor handler", timeout)
handlerLogger.Debugf("timeout happened after %vms while checking mouse cursor handler", timeout)
}
}
log.Printf("stopping cursor handler")
handlerLogger.Infof("stopping cursor handler")
return
}()
}(logger)
}

func MouseCursorHandler() *mouseCursorHandler {
Expand All @@ -61,20 +65,24 @@ func (m *mouseCursorHandler) Close() {
close(m.tickerCh)
}

func checkCursorChange(commCh chan *cursorInfo, lastMousePos *mouse.Position) {
func checkCursorChange(logger *log.Entry, commCh chan *cursorInfo, lastMousePos *mouse.Position) {
currentMousePos := mouse.GetPosition()
//log.Printf("current mouse position: %v\n", currentMousePos)
//log.Printf("last mouse position: %v\n", lastMousePos)
cursorLogger := logger.WithFields(log.Fields{
"current-mouse-position": currentMousePos,
"last-mouse-position": lastMousePos,
})
if currentMousePos.MouseX == lastMousePos.MouseX &&
currentMousePos.MouseY == lastMousePos.MouseY {
commCh <- &cursorInfo{
didCursorMove: false,
currentMousePos: nil,
}
cursorLogger.Debugf("cursor not moved")
} else {
commCh <- &cursorInfo{
didCursorMove: true,
currentMousePos: currentMousePos,
}
cursorLogger.Debugf("cursor moved")
}
}
Loading

0 comments on commit d60ddb4

Please sign in to comment.