Skip to content

Commit

Permalink
Small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Mar 6, 2017
1 parent a51d092 commit ba7d359
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export GO15VENDOREXPERIMENT=1
BENCH_FLAGS ?= -cpuprofile=cpu.pprof -memprofile=mem.pprof -benchmem
PKGS ?= $(shell glide novendor)
# Many Go tools take file globs or directories as arguments instead of packages.
PKG_FILES ?= *.go zapcore benchmarks buffer testutils internal/bufferpool internal/exit internal/multierror internal/observer
PKG_FILES ?= *.go zapcore benchmarks buffer testutils internal/bufferpool internal/exit internal/multierror internal/observer internal/color

# The linting tools evolve with each Go version, so run them only on the latest
# stable release.
Expand Down
54 changes: 15 additions & 39 deletions internal/coloring/coloring.go → internal/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,27 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/*
Package coloring adds coloring functionality.
*/
package coloring
// Package color adds coloring functionality for TTY output.
package color

import "fmt"

// Foreground colors.
const (
fgBlackCode colorCode = iota + 30
fgRedCode
fgGreenCode
fgYellowCode
fgBlueCode
fgMagentaCode
fgCyanCode
fgWhiteCode
Black Color = iota + 30
Red
Green
Yellow
Blue
Magenta
Cyan
White
)

var (
// FGBlack is a Color with a black foreground.
FGBlack = &Color{fgBlackCode}
// FGRed is a Color with a red foreground.
FGRed = &Color{fgRedCode}
// FGGreen is a Color with a green foreground.
FGGreen = &Color{fgGreenCode}
// FGYellow is a Color with a yellow foreground.
FGYellow = &Color{fgYellowCode}
// FGBlue is a Color with a blue foreground.
FGBlue = &Color{fgBlueCode}
// FGMagenta is a Color with a magenta foreground.
FGMagenta = &Color{fgMagentaCode}
// FGCyan is a Color with a cyan foreground.
FGCyan = &Color{fgCyanCode}
// FGWhite is a Color with a white foreground.
FGWhite = &Color{fgWhiteCode}
)

type colorCode int

// Color represents a color.
type Color struct {
colorCode colorCode
}
// Color represents a text color.
type Color uint8

// Add adds the coloring to the given string.
func (c *Color) Add(s string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.colorCode, s)
func (c Color) Add(s string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s)
}
36 changes: 36 additions & 0 deletions internal/color/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package color

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestColorFormatting(t *testing.T) {
assert.Equal(
t,
"\x1b[31mfoo\x1b[0m",
Red.Add("foo"),
"Unexpected colored output.",
)
}
20 changes: 14 additions & 6 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,33 @@ type LevelEncoder func(Level, PrimitiveArrayEncoder)
// LowercaseLevelEncoder serializes a Level to a lowercase string. For example,
// InfoLevel is serialized to "info".
func LowercaseLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToLowercaseString[l])
enc.AppendString(l.String())
}

// LowercaseColorLevelEncoder serializes a Level to a lowercase string and adds coloring.
// For example, InfoLevel is serialized to "info".
// For example, InfoLevel is serialized to "info" and colored blue.
func LowercaseColorLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToLowercaseColorString[l])
s, ok := _levelToLowercaseColorString[l]
if !ok {
s = _unknownLevelColor.Add(l.String())
}
enc.AppendString(s)
}

// CapitalLevelEncoder serializes a Level to an all-caps string. For example,
// InfoLevel is serialized to "INFO".
func CapitalLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToCapitalString[l])
enc.AppendString(l.CapitalString())
}

// CapitalColorLevelEncoder serializes a Level to an all-caps string and adds color.
// For example, InfoLevel is serialized to "INFO".
// For example, InfoLevel is serialized to "INFO" and colored blue.
func CapitalColorLevelEncoder(l Level, enc PrimitiveArrayEncoder) {
enc.AppendString(levelToCapitalColorString[l])
s, ok := _levelToCapitalColorString[l]
if !ok {
s = _unknownLevelColor.Add(l.CapitalString())
}
enc.AppendString(s)
}

// UnmarshalText unmarshals text to a LevelEncoder. "capital" is unmarshaled to
Expand Down
37 changes: 24 additions & 13 deletions zapcore/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ const (
_maxLevel = FatalLevel
)

var (
// AllLevels is an ordered slice of all Levels.
AllLevels = []Level{
DebugLevel,
InfoLevel,
WarnLevel,
ErrorLevel,
DPanicLevel,
PanicLevel,
FatalLevel,
}
)

// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
switch l {
Expand All @@ -89,6 +76,30 @@ func (l Level) String() string {
}
}

// CapitalString returns an all-caps ASCII representation of the log level.
func (l Level) CapitalString() string {
// Printing levels in all-caps is common enough that we should export this
// functionality.
switch l {
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
case DPanicLevel:
return "DPANIC"
case PanicLevel:
return "PANIC"
case FatalLevel:
return "FATAL"
default:
return fmt.Sprintf("LEVEL(%d)", l)
}
}

// MarshalText marshals the Level to text. Note that the text representation
// drops the -Level suffix (see example).
func (l *Level) MarshalText() ([]byte, error) {
Expand Down
40 changes: 15 additions & 25 deletions zapcore/level_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,27 @@

package zapcore

import (
"strings"

"go.uber.org/zap/internal/coloring"
)
import "go.uber.org/zap/internal/color"

var (
levelToColor = map[Level]*coloring.Color{
DebugLevel: coloring.FGMagenta,
InfoLevel: coloring.FGBlue,
WarnLevel: coloring.FGYellow,
ErrorLevel: coloring.FGRed,
DPanicLevel: coloring.FGRed,
PanicLevel: coloring.FGRed,
FatalLevel: coloring.FGRed,
_levelToColor = map[Level]color.Color{
DebugLevel: color.Magenta,
InfoLevel: color.Blue,
WarnLevel: color.Yellow,
ErrorLevel: color.Red,
DPanicLevel: color.Red,
PanicLevel: color.Red,
FatalLevel: color.Red,
}
_unknownLevelColor = color.Red

levelToLowercaseString = make(map[Level]string, len(AllLevels))
levelToCapitalString = make(map[Level]string, len(AllLevels))
levelToLowercaseColorString = make(map[Level]string, len(AllLevels))
levelToCapitalColorString = make(map[Level]string, len(AllLevels))
_levelToLowercaseColorString = make(map[Level]string, len(_levelToColor))
_levelToCapitalColorString = make(map[Level]string, len(_levelToColor))
)

func init() {
for _, level := range AllLevels {
levelToLowercaseString[level] = level.String()
levelToCapitalString[level] = strings.ToUpper(level.String())
for level, color := range _levelToColor {
_levelToLowercaseColorString[level] = color.Add(level.String())
_levelToCapitalColorString[level] = color.Add(level.CapitalString())
}
for level, color := range levelToColor {
levelToLowercaseColorString[level] = color.Add(level.String())
levelToCapitalColorString[level] = color.Add(strings.ToUpper(level.String()))
}

}
14 changes: 7 additions & 7 deletions zapcore/level_strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
)

func TestAllLevelsCoveredByLevelString(t *testing.T) {
assert.Equal(t, len(levelToColor), len(AllLevels), "Length of levelToColor not equal to to the length of AllLevels")
for _, level := range AllLevels {
assert.NotEmpty(t, levelToColor[level], "Level %v not covered by a color in levelToColor", level)
assert.NotEmpty(t, levelToLowercaseString[level], "Level %v not covered in levelToLowercaseString", level)
assert.NotEmpty(t, levelToCapitalString[level], "Level %v not covered in levelToCapitalString", level)
assert.NotEmpty(t, levelToLowercaseColorString[level], "Level %v not covered levelToLowercaseColorString", level)
assert.NotEmpty(t, levelToCapitalColorString[level], "Level %v not covered in levelToCapitalColorString", level)
numLevels := int((_maxLevel - _minLevel) + 1)

isComplete := func(m map[Level]string) bool {
return len(m) == numLevels
}

assert.True(t, isComplete(_levelToLowercaseColorString), "Colored lowercase strings don't cover all levels.")
assert.True(t, isComplete(_levelToCapitalColorString), "Colored capital strings don't cover all levels.")
}
3 changes: 2 additions & 1 deletion zapcore/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestLevelString(t *testing.T) {
}

for lvl, stringLevel := range tests {
assert.Equal(t, stringLevel, lvl.String())
assert.Equal(t, stringLevel, lvl.String(), "Unexpected lowercase level string.")
assert.Equal(t, strings.ToUpper(stringLevel), lvl.CapitalString(), "Unexpected all-caps level string.")
}
}

Expand Down

0 comments on commit ba7d359

Please sign in to comment.