summaryrefslogtreecommitdiff
path: root/cmd/rr/debug/debugger.go
blob: 533a5947119ed6af769038189332ebe2a69b5f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package debug

import (
	"github.com/sirupsen/logrus"
	"github.com/spiral/roadrunner"
	"github.com/spiral/roadrunner/cmd/rr/utils"
	"github.com/spiral/roadrunner/service/http"
	"strings"
)

// Listener creates new debug listener.
func Listener(logger *logrus.Logger) func(event int, ctx interface{}) {
	return (&debugger{logger}).listener
}

// listener provide debug callback for system events. With colors!
type debugger struct{ logger *logrus.Logger }

// listener listens to http events and generates nice looking output.
func (s *debugger) listener(event int, ctx interface{}) {
	// http events
	switch event {
	case http.EventResponse:
		log := ctx.(*http.Event)
		s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
	case http.EventError:
		log := ctx.(*http.Event)

		if _, ok := log.Error.(roadrunner.JobError); ok {
			s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
		} else {
			s.logger.Info(utils.Sprintf(
				"%s <white+hb>%s</reset> %s <red>%s</reset>",
				statusColor(log.Status),
				log.Method,
				log.URI,
				log.Error,
			))
		}
	}

	switch event {
	case roadrunner.EventWorkerKill:
		w := ctx.(*roadrunner.Worker)
		s.logger.Warning(utils.Sprintf(
			"<white+hb>worker.%v</reset> <yellow>killed</red>",
			*w.Pid,
		))
	case roadrunner.EventWorkerError:
		err := ctx.(roadrunner.WorkerError)
		s.logger.Error(utils.Sprintf(
			"<white+hb>worker.%v</reset> <red>%s</reset>",
			*err.Worker.Pid,
			err.Caused,
		))
	}

	// outputs
	switch event {
	case roadrunner.EventStderrOutput:
		s.logger.Warning(utils.Sprintf("<yellow+h>%s</reset>", strings.Trim(string(ctx.([]byte)), "\r\n")))
	}

	// rr server events
	switch event {
	case roadrunner.EventServerFailure:
		s.logger.Error(utils.Sprintf("<red>server is dead</reset>"))
	}

	// pool events
	switch event {
	case roadrunner.EventPoolConstruct:
		s.logger.Debug(utils.Sprintf("<cyan>new worker pool</reset>"))
	case roadrunner.EventPoolError:
		s.logger.Error(utils.Sprintf("<red>%s</reset>", ctx))
	}

	//s.logger.Warning(event, ctx)
}

func statusColor(status int) string {
	if status < 300 {
		return utils.Sprintf("<green>%v</reset>", status)
	}

	if status < 400 {
		return utils.Sprintf("<cyan>%v</reset>", status)
	}

	if status < 500 {
		return utils.Sprintf("<yellow>%v</reset>", status)
	}

	return utils.Sprintf("<red>%v</reset>", status)
}