grim/convey

Add a .reviewboardrc file

2022-03-26, Gary Kramlich
8fea0c778f8e
Add a .reviewboardrc file
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package logging
import (
"bytes"
"fmt"
"os"
"time"
"github.com/mgutz/ansi"
log "github.com/sirupsen/logrus"
)
var (
baseTime time.Time = time.Now()
levelColors map[log.Level]string = map[log.Level]string{
log.PanicLevel: ansi.ColorCode("red+h"),
log.FatalLevel: ansi.ColorCode("red+b"),
log.ErrorLevel: ansi.ColorCode("red"),
log.WarnLevel: ansi.ColorCode("yellow"),
log.InfoLevel: ansi.ColorCode("green"),
log.DebugLevel: ansi.ColorCode("cyan"),
log.TraceLevel: ansi.ColorCode("white+h"),
}
)
type Formatter struct {
disableColors bool
}
func miniTimestamp() int {
return int(time.Since(baseTime) / time.Second)
}
func NewFormatter(color bool) log.Formatter {
disableColors := false
if color {
switch os.Getenv("TERM") {
case "win":
fallthrough
case "dumb":
disableColors = true
}
} else {
disableColors = true
}
return &Formatter{
disableColors: disableColors,
}
}
func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
fmt.Fprintf(b, "[%04d] ", miniTimestamp())
id := entry.Data["id"]
if id != nil {
idColor := entry.Data["idColor"]
if f.disableColors && idColor != nil {
fmt.Fprintf(b, "%s: ", id)
} else {
fmt.Fprintf(b, "%s%s%s: ", idColor, id, ansi.Reset)
}
}
if entry.Message != "" {
if f.disableColors {
fmt.Fprintf(b, "%s", entry.Message)
} else {
fmt.Fprintf(b, "%s%s%s", levelColors[entry.Level], entry.Message, ansi.Reset)
}
}
b.WriteByte('\n')
return b.Bytes(), nil
}