enable coloring by UIDs

This commit is contained in:
Dominic Breuker
2019-04-24 22:30:14 +02:00
parent d1b6518db5
commit 21a0666ff1
4 changed files with 67 additions and 41 deletions

View File

@@ -2,14 +2,20 @@ package logging
import (
"fmt"
"hash/fnv"
"log"
"os"
"strconv"
)
const (
ColorNone = iota
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorPurple
ColorTeal
)
type Logger struct {
@@ -43,14 +49,15 @@ func (l *Logger) Errorf(debug bool, format string, v ...interface{}) {
// Eventf writes an event with timestamp to stdout
func (l *Logger) Eventf(color int, format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
switch color {
case ColorRed:
msg = fmt.Sprintf("\x1b[31;1m%s\x1b[0m", msg)
case ColorGreen:
msg = fmt.Sprintf("\x1b[32;1m%s\x1b[0m", msg)
default:
if color != ColorNone {
msg = fmt.Sprintf("\x1b[%d;1m%s\x1b[0m", 30+color, msg)
}
l.eventLogger.Printf("%s", msg)
}
func GetColorByUID(uid int) int {
h := fnv.New32a()
h.Write([]byte(strconv.Itoa(uid)))
return (int(h.Sum32()) % (ColorTeal)) + 1
}